vzhn opened a new pull request, #1276:
URL: https://github.com/apache/tomcat/pull/1276
c86097c57c ("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 c86097c57c such a caller
returned `ERROR` after its timeout.
Today (b4c09942b3):
```mermaid
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
```
With this change:
```mermaid
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
```
## 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 c86097c57c 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 b4c09942b3, the `org.apache.coyote.http2.*`,
`org.apache.tomcat.util.net.*` and
`org.apache.tomcat.websocket.server.*` tests pass (8,026 tests, NIO).
Rebased on 11f173c17b, `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](https://www.apache.org/legal/generative-tooling.html)): 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](https://claude.com/claude-code)
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]