Skip to content

Release a blocked caller when a vectored I/O completion handler throws - #1276

Closed
vzhn wants to merge 1 commit into
apache:mainfrom
vzhn:await-handler-completion-throws
Closed

vzhn wants to merge 1 commit into
apache:mainfrom
vzhn:await-handler-completion-throws

Conversation

@vzhn

@vzhn vzhn commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

c86097c ("Handle concurrent completion and timeout") made a BLOCK read or write wait in
awaitHandlerCompletion() when it times out (or is interrupted) after the completion thread has claimed the handler
call, 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 VectoredIOCompletionHandler sets the operation state and calls notify(),
and that code runs after state.handler.completed(...) / state.handler.failed(...) return, with no finally. If
the handler throws, the state stays PENDING: a caller that is already in awaitHandlerCompletion() 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 ERROR after 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 thread
Loading

With 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 before
Loading

Change

In VectoredIOCompletionHandler.completed() and failed(), the block that sets the state and notifies the blocked
caller moves into a finally around the handler call. The handler's exception still propagates on the completion
thread 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:

  • the handler throws while the caller is still in its timed wait: the caller is woken and returns the state the
    completion set, DONE (for failed() too, which already uses DONE for a non-inline failure);
  • the caller has already timed out (or been interrupted) and is in awaitHandlerCompletion(): it returns ERROR,
    as it does after c86097c when the handler returns normally.

Tests

Three tests in TestUpgradeInternalHandler, using an internal upgrade handler that does a BLOCK read whose
completion 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:

test case expected
testBlockingReadCompletedThrowsBeforeTimeout completed() throws within the 2 s timeout DONE
testBlockingReadCompletedThrowsAfterTimeout 200 ms timeout; completed() sleeps 1 s, so the caller is in awaitHandlerCompletion(), then throws ERROR
testBlockingReadFailedThrows end of stream; failed() throws DONE

Each 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.* and
org.apache.tomcat.websocket.server.* tests pass (8,026 tests, NIO). Rebased on 11f173c, TestUpgradeInternalHandler
and Checkstyle (ant validate) pass.

Exposure

BLOCK operations with a completion handler in Tomcat itself:

  • Http2AsyncUpgradeHandler.writeBody: applicationErrorCompletion only records the error.
  • WsRemoteEndpointImplServer.doWrite for blocking WebSocket sends: the handler calls the SendHandler chain
    (BlockingSendHandler and friends) without a catch.
  • Http2AsyncParser.readFrame passes BLOCK only when asked to block, and its blocking caller
    (readConnectionPreface) is overridden with NON_BLOCK, so HTTP/2 reads are not affected.

SocketWrapperBase.read / write are also available to InternalHttpUpgradeHandler implementations. No Tomcat
handler 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 SocketWrapperBase with a research model checker and confirmed with a TLA+ model and on a
Tomcat 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

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
vzhn force-pushed the await-handler-completion-throws branch from 00bc9eb to baf1da4 Compare November 28, 2026 22:31
@markt-asf

Copy link
Copy Markdown
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 try/finally isn't an unreasonable hardening but the chances of it ever being needed are extremely low.

@markt-asf markt-asf closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants