Skip to content

Commit

Permalink
8309305: sun/security/ssl/SSLSocketImpl/BlockedAsyncClose.java fails …
Browse files Browse the repository at this point in the history
…with jtreg test timeout

Reviewed-by: djelinski
  • Loading branch information
Matthew Donovan committed Jul 21, 2023
1 parent 84b325b commit 8042a50
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions test/jdk/sun/security/ssl/SSLSocketImpl/BlockedAsyncClose.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,7 +29,8 @@
/*
* @test
* @bug 8224829
* @summary AsyncSSLSocketClose.java has timing issue
* @summary AsyncSSLSocketClose.java has timing issue.
* @library /javax/net/ssl/templates
* @run main/othervm BlockedAsyncClose
*/

Expand All @@ -38,52 +39,41 @@
import java.net.SocketException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BlockedAsyncClose implements Runnable {
/*
* To manually verify that the write thread was blocked when socket.close() is called,
* run the test with -Djavax.net.debug=ssl. You should see the message
* "SSLSocket output duplex close failed: SO_LINGER timeout, close_notify message cannot be sent."
*/
public class BlockedAsyncClose extends SSLContextTemplate implements Runnable {
SSLSocket socket;
SSLServerSocket ss;

// Is the socket ready to close?
private final CountDownLatch closeCondition = new CountDownLatch(1);

// Where do we find the keystores?
static String pathToStores = "../../../../javax/net/ssl/etc";
static String keyStoreFile = "keystore";
static String trustStoreFile = "truststore";
static String passwd = "passphrase";
private final Lock writeLock = new ReentrantLock();

public static void main(String[] args) throws Exception {
String keyFilename =
System.getProperty("test.src", "./") + "/" + pathToStores +
"/" + keyStoreFile;
String trustFilename =
System.getProperty("test.src", "./") + "/" + pathToStores +
"/" + trustStoreFile;

System.setProperty("javax.net.ssl.keyStore", keyFilename);
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
System.setProperty("javax.net.ssl.trustStore", trustFilename);
System.setProperty("javax.net.ssl.trustStorePassword", passwd);

new BlockedAsyncClose();
new BlockedAsyncClose().runTest();
}

public BlockedAsyncClose() throws Exception {
SSLServerSocketFactory sslssf =
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
public void runTest() throws Exception {
SSLServerSocketFactory sslssf = createServerSSLContext().getServerSocketFactory();
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = (SSLServerSocket)sslssf.createServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));

SSLSocketFactory sslsf =
(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocketFactory sslsf = createClientSSLContext().getSocketFactory();
socket = (SSLSocket)sslsf.createSocket(loopback, ss.getLocalPort());
SSLSocket serverSoc = (SSLSocket)ss.accept();
ss.close();

(new Thread(this)).start();
new Thread(this).start();
serverSoc.startHandshake();

boolean closeIsReady = closeCondition.await(90L, TimeUnit.SECONDS);
Expand All @@ -94,23 +84,22 @@ public BlockedAsyncClose() throws Exception {
}

socket.setSoLinger(true, 10);
System.out.println("Calling Socket.close");

// Sleep for a while so that the write thread blocks by hitting the
// output stream buffer limit.
Thread.sleep(1000);
// if the writeLock is not released by the other thread within 10
// seconds it is probably blocked, and we can try to close the socket
while (writeLock.tryLock(10, TimeUnit.SECONDS)) {
writeLock.unlock();
}

System.out.println("Calling socket.close()");
socket.close();
System.out.println("ssl socket get closed");
System.out.flush();
}

// block in write
public void run() {
byte[] ba = new byte[1024];
for (int i = 0; i < ba.length; i++) {
ba[i] = 0x7A;
}
Arrays.fill(ba, (byte) 0x7A);

try {
OutputStream os = socket.getOutputStream();
Expand All @@ -128,8 +117,16 @@ public void run() {
// write more
while (true) {
count += ba.length;

System.out.println(count + " bytes to be written");

writeLock.lock();
os.write(ba);
// This isn't in a try/finally. If an exception is thrown
// and the lock is released, the main thread will
// loop until the test times out. So don't release it.
writeLock.unlock();

System.out.println(count + " bytes written");
}
} catch (SocketException se) {
Expand All @@ -144,4 +141,3 @@ public void run() {
}
}
}

5 comments on commit 8042a50

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8042a50 Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8042a50 Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch backport-GoeLin-8042a50b in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8042a50b from the openjdk/jdk repository.

The commit being backported was authored by Matthew Donovan on 21 Jul 2023 and was reviewed by Daniel Jeliński.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-8042a50b:backport-GoeLin-8042a50b
$ git checkout backport-GoeLin-8042a50b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-8042a50b

@sendaoYan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8042a50 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sendaoYan the backport was successfully created on the branch backport-sendaoYan-8042a50b in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8042a50b from the openjdk/jdk repository.

The commit being backported was authored by Matthew Donovan on 21 Jul 2023 and was reviewed by Daniel Jeliński.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev.git backport-sendaoYan-8042a50b:backport-sendaoYan-8042a50b
$ git checkout backport-sendaoYan-8042a50b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev.git backport-sendaoYan-8042a50b

⚠️ @sendaoYan You are not yet a collaborator in my fork openjdk-bots/jdk11u-dev. An invite will be sent out and you need to accept it before you can proceed.

Please sign in to comment.