Repository navigation
Conversation
c86097c made a BLOCK read or write that times out (or is interrupted) after the completion thread claimed the handler call wait in awaitHandlerCompletion() until the handler has been called. VectoredIOCompletionHandler set the operation state and notified only after the handler returned, so a throwing handler left the caller waiting forever. Set the state and notify in a finally block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
vzhn
force-pushed
the
await-handler-completion-throws
branch
from
November 28, 2026 22:31
00bc9eb to
baf1da4
Compare
Contributor
|
That is an awful lot of analysis for something that isn't going to happen in normal Tomcat usage (Tomcat's completion handlers don't throw unless the error is essentially fatal to the JVM in which case the consequences are irrelevant). The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
c86097c ("Handle concurrent completion and timeout") made a
BLOCKread or write wait inawaitHandlerCompletion()when it times out (or is interrupted) after the completion thread has claimed the handlercall, so the caller no longer returns while the handler is still running. That wait has no timeout and keeps waiting
through interrupts. It ends only when
VectoredIOCompletionHandlersets the operation state and callsnotify(),and that code runs after
state.handler.completed(...)/state.handler.failed(...)return, with nofinally. Ifthe handler throws, the state stays
PENDING: a caller that is already inawaitHandlerCompletion()waits forever,and a caller still in its timed wait times out, loses the CAS and then waits forever. Before c86097c such a caller
returned
ERRORafter its timeout.Today (b4c0994):
sequenceDiagram participant C as Caller (container thread)<br/>vectoredOperation, BLOCK participant S as OperationState participant H as Completion thread<br/>VectoredIOCompletionHandler C->>S: start(), then state.wait(timeout) H->>S: callHandler CAS (wins) H->>H: handler.completed() ... Note over C: timeout expires, state is PENDING C->>S: callHandler CAS fails C->>S: awaitHandlerCompletion(): state.wait() H-->>H: handler throws Note over H: the block that sets state = DONE and calls notify() is skipped Note over C: waits forever, holding a container threadWith this change:
sequenceDiagram participant C as Caller (container thread)<br/>vectoredOperation, BLOCK participant S as OperationState participant H as Completion thread<br/>VectoredIOCompletionHandler C->>S: start(), then state.wait(timeout) H->>S: callHandler CAS (wins) H->>H: handler.completed() inside try ... Note over C: timeout expires, state is PENDING C->>S: callHandler CAS fails C->>S: awaitHandlerCompletion(): state.wait() H-->>H: handler throws H->>S: finally block: state = DONE, notify() S-->>C: woken, state is not PENDING Note over C: returns ERROR Note over H: the handler's exception propagates as beforeChange
In
VectoredIOCompletionHandler.completed()andfailed(), the block that sets the state and notifies the blockedcaller moves into a
finallyaround the handler call. The handler's exception still propagates on the completionthread as before, and the caller still does not return before the handler has returned or thrown.
What a blocked caller returns when the handler throws now depends on when:
completion set,
DONE(forfailed()too, which already usesDONEfor a non-inline failure);awaitHandlerCompletion(): it returnsERROR,as it does after c86097c when the handler returns normally.
Tests
Three tests in
TestUpgradeInternalHandler, using an internal upgrade handler that does aBLOCKread whosecompletion handler throws. The client waits until the read has started, then sends a byte (or closes its output)
100 ms later, so the read completes on a container thread rather than inline:
testBlockingReadCompletedThrowsBeforeTimeoutcompleted()throws within the 2 s timeoutDONEtestBlockingReadCompletedThrowsAfterTimeoutcompleted()sleeps 1 s, so the caller is inawaitHandlerCompletion(), then throwsERRORtestBlockingReadFailedThrowsfailed()throwsDONEEach asserts that the read returns within 10 s, that the handler was called, and the returned state. Without the
change all three fail ("Blocking read did not return after the completion handler threw"); with it all pass (the
class runs in about 2 s). The handler's exception is printed by the container thread; that is expected.
With the change on b4c0994, the
org.apache.coyote.http2.*,org.apache.tomcat.util.net.*andorg.apache.tomcat.websocket.server.*tests pass (8,026 tests, NIO). Rebased on 11f173c,TestUpgradeInternalHandlerand Checkstyle (
ant validate) pass.Exposure
BLOCKoperations with a completion handler in Tomcat itself:Http2AsyncUpgradeHandler.writeBody:applicationErrorCompletiononly records the error.WsRemoteEndpointImplServer.doWritefor blocking WebSocket sends: the handler calls theSendHandlerchain(
BlockingSendHandlerand friends) without a catch.Http2AsyncParser.readFramepassesBLOCKonly when asked to block, and its blocking caller(
readConnectionPreface) is overridden withNON_BLOCK, so HTTP/2 reads are not affected.SocketWrapperBase.read/writeare also available toInternalHttpUpgradeHandlerimplementations. No Tomcathandler is known to throw in normal operation, so this is a robustness fix: an unexpected exception or an
Error(such as
OutOfMemoryError) in a completion handler no longer leaves a container thread waiting forever.AI disclosure (ASF Generative Tooling Guidance): found
by model-checking
SocketWrapperBasewith a research model checker and confirmed with a TLA+ model and on aTomcat build. Claude Code (Claude Opus 5.5) wrote the change, the tests and this text; the change was reviewed by
Claude Fable 5.2 and Gemini 4 Neon.
🤖 Generated with Claude Code