http-client-branch: fix rare race condition uncovered by SmallTimeout.java http-client-branch
authordfuchs
Tue, 24 Apr 2018 10:14:51 +0100
branchhttp-client-branch
changeset 56476 df94b3e52c78
parent 56474 fe2bf7b369b8
child 56477 46c04919ee5c
http-client-branch: fix rare race condition uncovered by SmallTimeout.java
src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java
--- a/src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java	Mon Apr 23 15:45:40 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http1Exchange.java	Tue Apr 24 10:14:51 2018 +0100
@@ -259,8 +259,19 @@
         if (!connection.connected()) {
             if (debug.on()) debug.log("initiating connect async");
             connectCF = connection.connectAsync();
+            Throwable cancelled;
             synchronized (lock) {
-                operations.add(connectCF);
+                if ((cancelled = failed) == null) {
+                    operations.add(connectCF);
+                }
+            }
+            if (cancelled != null) {
+                if (client.isSelectorThread()) {
+                    executor.execute(() ->
+                        connectCF.completeExceptionally(cancelled));
+                } else {
+                    connectCF.completeExceptionally(cancelled);
+                }
             }
         } else {
             connectCF = new MinimalFuture<>();
@@ -403,6 +414,9 @@
             if ((error = failed) == null) {
                 failed = error = cause;
             }
+            if (debug.on()) {
+                debug.log(request.uri() + ": " + error);
+            }
             if (requestAction != null && requestAction.finished()
                     && response != null && response.finished()) {
                 return;
@@ -447,7 +461,7 @@
                 exec.execute(() -> {
                     if (cf.completeExceptionally(x)) {
                         if (debug.on())
-                            debug.log("completed cf with %s", (Object) x);
+                            debug.log("%s: completed cf with %s", request.uri(), x);
                     }
                 });
             }