ffang commented on code in PR #3701:
URL: https://github.com/apache/cxf/pull/3701#discussion_r4531946356
##########
rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpClientHTTPConduit.java:
##########
@@ -1161,22 +1161,21 @@ public CompletableFuture<HttpResponse<InputStream>>
run() throws IOException {
future = cl.sendAsync(request, handler);
}
future.exceptionally(ex -> {
- // Record the failure on the message so the close() invoked
below
- // does not treat this as an un-sent request and resend it: the
- // shared WrappedOutputStream#close() already skips
- // handleHeadersTrustCaching() once outMessage carries an
Exception
- // (CXF-9250).
- outMessage.setContent(Exception.class,
- ex instanceof Exception ? (Exception) ex : new
IOException(ex));
- if (pout != null) {
- synchronized (pout) {
- pout.notifyAll();
+ // Only release a writer waiting for the connection or blocked
on the body. The
+ // caller still closes the stream, and the failure is reported
from there, by the
+ // response handling getting it from the future. Closing the
whole stream here
+ // would send a request without a body again, and recording
the failure on the
+ // message would keep the caller's close() from reporting it.
+ final OutputStream out = pout;
+ if (out != null) {
+ synchronized (out) {
+ out.notifyAll();
+ }
+ try {
+ out.close();
Review Comment:
Hi @reta,
I'd keep it as is. The second close is a no-op, and setting `pout` to `null`
from the callback wouldn't prevent it, but it could change what the caller does:
- **The second close is harmless:** `HttpClientPipedOutputStream.close()`
only calls `PipedOutputStream.close()`, which marks the pipe as closed by the
writer and notifies the reader, so it can be repeated. It then closes the
publisher only if it's still set, and clears it.
`CloseableByteArrayOutputStream.close()` only sets `closed` and notifies. The
new tests already go through it: the callback closes the stream, then the
caller's `close()` closes it again, both with chunking (the pipe) and without
(the buffer).
- **It wouldn't skip that close:** once `setupWrappedStream()` has run, the
same stream is also `wrappedStream` (or the flow-through stream of the
`CacheAndWriteOutputStream`), and the caller's `close()` closes it from there,
in `AbstractWrappedOutputStream.close()`, whatever `pout` is. `pout` is also
written by the caller's thread and isn't volatile, so a `null` written by the
HttpClient thread isn't guaranteed to be seen there anyway.
- **It could break things, depending on timing:** the callback can run
before `setupWrappedStream()`, e.g. when the connection is refused during the
trust decision (`testConnectionRefusedWhileSendingOnClose`). With `pout` set to
`null`, `wrappedStream` would be `null` and the body silently dropped, or the
cache would be created with a `null` flow-through stream, and
`retransmitStream()` passes `pout` to `writeCacheTo()` without checking it.
Today `pout` always points to a closed stream, which those paths already handle.
The caller's `close()` already sets `pout` to `null` in its `finally` block.
If the callback runs after that, it reads `null` into its local copy and
doesn't close anything.
Best Regards
Freeman
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]