Skip to content

THRIFT-5081: Avoid flushing Java socket output during close - #4163

Open
HTHou wants to merge 1 commit into
apache:masterfrom
HTHou:THRIFT-5081
Open

HTHou wants to merge 1 commit into
apache:masterfrom
HTHou:THRIFT-5081

Conversation

@HTHou

@HTHou HTHou commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes THRIFT-5081.

Closing a Java TSocket can log an output-close warning with Conscrypt even after a successful RPC flush: closing input closes the shared socket, then closing buffered output attempts another flush. Socket cleanup must also remain able to interrupt a write blocked on a peer that does not read.

Close the underlying Socket directly and clear the buffering-wrapper references without closing or flushing those wrappers. The socket owns both stream endpoints. This preserves cancellation of blocked socket I/O and avoids the extra flush against a closed TLS socket. Pending ordinary socket output is discarded, consistent with the behavior before this PR; normal callers must flush explicitly before closing.

Complete and flush zlib output in TTransportOutputStream.close(), after DeflaterOutputStream.close() finishes compression and before its owner closes the underlying transport. Use finally in TZlibTransport.close() to attempt underlying cleanup even after unchecked failures. Zlib close remains a normal finishing operation that can wait on output; cancellation closes its underlying transport directly.

Generic TIOStreamTransport remains unchanged from the base branch. No new public APIs or dependencies.

Validation:

  • All 20 focused socket, stream, and zlib tests pass on JDK 17 with the repository's JUnit 5.9.1, Mockito 5.3.0, and SLF4J 1.7.36 versions, compiling with the project's Java 8 release target and warning flags. The blocked-write regression test fails against the previous PR implementation and passes with this change.

  • The same 20 focused tests also pass on JDK 21. Three real TCP blocked-write probes confirm that close returns and the writer receives Socket closed.

  • Standalone real TLS probes cover Conscrypt 2.6.0 and 2.9.0 with TLS 1.2 and 1.3 on JDK 21: empty output, a flushed request/response, and empty/flushed/unflushed zlib output. All 20 cases pass with complete application data and no close warnings. Forty control cases against the pre-PR and previous PR implementations confirm the expected warning and zlib-trailer behavior.

  • Google Java Format and git diff --check pass. Full make style cannot run in this unconfigured checkout (No rule to make target 'style'); the full Gradle Java suite has not been rerun.

  • JIRA ticket linked and PR title starts with its identifier.

  • One commit, including regression tests.

  • No breaking public API changes or new dependencies.

Generated-by: OpenAI Codex (GPT-6)

@HTHou
HTHou requested review from fishy and jimexist as code owners November 9, 2026 10:55
Copilot AI balanced review requested due to automatic review settings November 9, 2026 10:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mergeable mergeable Bot added the java Pull requests that update Java code label Nov 9, 2026
Copilot AI balanced review requested due to automatic review settings November 9, 2026 11:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 11:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@HTHou
HTHou requested a balanced review from Copilot November 10, 2026 09:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@HTHou
HTHou requested a lite review from Copilot November 10, 2026 10:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues

Review effort: Lite

@Jens-G

Jens-G commented Nov 13, 2026

Copy link
Copy Markdown
Member

Code review

Found 1 issue:

  1. TSocket.close() from another thread no longer ends a write that is blocked on a peer that does not read. A writer blocked in write() holds the BufferedOutputStream lock. close() now calls closeOutputStream() first, and BufferedOutputStream.close() flushes, so it waits for that lock and does not get to socket_.close(). Before, close() closed the input stream first, which closes the socket, so the writer failed with SocketException: Socket closed and close() returned. Since java.net sockets have no write timeout, closing from another thread was the way to end such a write. I ran it against this branch and its merge base, on JDK 17 and 11. The peer accepted and never read, one thread wrote in 64 KB chunks, and a second thread called close(). On the base, close() returned within 2 ms in every run. On this branch it was still blocked after 5 s, in TSocket.close → closeOutputStream → FilterOutputStream.close → flush, waiting on that monitor until the peer side was closed.

/** Closes the socket. */
public void close() {
// Closing the input stream also closes the socket, so flush and close output first.
try {
closeOutputStream();
} finally {
try {
super.close();
} finally {
// Close the socket
if (socket_ != null) {
try {
socket_.close();
} catch (IOException iox) {
LOGGER.warn("Could not close socket.", iox);
}
socket_ = null;
}
}
}
}

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings November 13, 2026 15:42
@HTHou

HTHou commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. I reproduced the blocked-write regression and pushed 2f7fcc1 to address it.

TSocket.close() now closes the underlying socket directly, without flushing or closing the buffering wrappers. Ordinary socket callers flush explicitly. Zlib completion and flushing happen in the compression adapter before its owner closes the underlying transport.

The blocked-write regression test fails against the previous implementation and passes with this change. All 20 focused tests pass on JDK 17 with the project's dependency versions. Real TCP probes confirm that close interrupts blocked writes; Conscrypt 2.6.0 and 2.9.0 probes with TLS 1.2/1.3 also preserve warning-free normal close and complete zlib streams.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
2 resolved since last review

Review effort: Lite

@HTHou HTHou changed the title THRIFT-5081: Close Java socket output before input THRIFT-5081: Avoid flushing Java socket output during close Nov 13, 2026
Copilot AI lite review requested due to automatic review settings November 13, 2026 16:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
2 resolved since last review

Review effort: Lite

Client: java

Close the underlying socket directly to interrupt blocked I/O and avoid
flushing Conscrypt output after the shared socket has closed. Discard
unflushed socket buffers and let normal callers flush explicitly.

Finish and flush compressed output in the zlib output adapter before its
owner closes the underlying transport. Attempt cleanup after unchecked
failures, preserving the first failure and suppressing subsequent cleanup
failures. Leave generic stream transport cleanup unchanged.

Cover cancellation of blocked writes, repeated close, socket-close and
flush failures, complete zlib streams, and combined unchecked failures,
including a shared exception object and cleanup-only failures.

Generated-by: OpenAI Codex (GPT-6)
Copilot AI lite review requested due to automatic review settings November 19, 2026 03:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The change spans low-level socket/TLS interruption and compression-finalization behavior, with only focused regressions reported and no full Gradle suite run, leaving broader provider and transport interactions for human validation.

No open issues 🎉

3 resolved since last review

Review effort: Lite

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants