fix: Prevent an AsyncRequest timeout race from wedging HTTP connections #20821
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #20821"
general
weight: 0.35
gianm
Fixes #20817.
Description
KafkaMultiSupervisorTest.test_ingestIntoSingleDatasource_fromDifferentTopicsdidn't fail because ingestion was slow. It failed because the Overlord'sassignTaskrequest to the Indexer hung for 60 seconds. The hang is caused by a race in the sharedAsyncRequesthelper added in #20784, which leaves an HTTP connection that the server never reads from again.Fixed a race between an async response and its timeout
The race happens when the future of a long-polling request completes on another thread just as Jetty's async timeout fires:
1. The future's thread wins
AsyncRequest's at-most-once guard and writes the response. Jetty then rejects itsAsyncContext.complete(), because while a request is timing out, Jetty only allows it to be completed from the thread running theonTimeoutlisteners (HttpChannelState.complete()).2. The timeout listener sees that the request was already completed and returns without completing it.
3. Jetty calls
sendError(500)on the committed response, which throwsIllegalStateException: COMMITTED, and the exchange is never finished.4. The client received the whole response, so it reuses the keep-alive connection. Jetty never reads from that connection again, and the next request sent on it hangs until the client's read timeout.
In the failing CI run, this happened on the Indexer's worker-sync endpoint (
TaskManagementResource), which the embedded tests poll with a 1s timeout:The
assignTaskrequest was sent on the stuck connection and hung for the 60sassignRequestHttpTimeout.HttpRemoteTaskRunnerhas one pending-task thread and skips workers that have an unacknowledged assignment, so no task was assigned for those 60 seconds, and the test's 60s wait ran out.Before #20784, the timeout listeners in
TaskManagementResourceandSegmentListerResourcealways completed the request.OverlordResourceandMessageRelayResourcecompleted it only if they won the guard, so they already had this latent issue. This change fixes allAsyncRequestcallers.The fix is in
AsyncRequest:event.getAsyncContext().complete()in afinallyafter its guarded completion. Jetty allows this on theonTimeoutthread, and it does nothing if the request is already complete.complete()is nowsynchronized, so if the future's thread is still writing its response, the timeout listener waits for the write to finish instead of cutting the response off.Concurrency notes, per the concurrency checklist:
AsyncRequestinstance. Onlycomplete()takes it, andcompletedis@GuardedBy("this").complete()calls the caller'sonSuccess/onFailurefunction and writes the response while holding the lock. This is intentional: it is what makes the timeout listener wait for a write in progress. Only that request's timeout listener contends for the lock. The Javadocs ofonSuccess/onFailurenow say they must not block.onTimeoutlisteners after releasing itsHttpChannelStatelock.future.cancel(true)in the listener can re-entercomplete()throughonFailureon the same thread, which the reentrant monitor allows.Alternatives considered:
TaskManagementResourceto its ownAsyncContexthandling would leave the other callers affected.Not changed in this PR:
multi-stage-query'sWorkerResource(channel-data long poll) has a similar pattern. Its timeout listener completes the request only if it winsresponseResolved, andonSuccesscallsasyncContext.complete()in afinallywithout handling the exception. This is worth a follow-up.Request timed out or is closed alreadyat WARN, asOverlordResourcedid before #20784.No release note is needed: the regression from #20784 has not been released.
Tests
AsyncRequestTest: two new unit tests.-
test_start_completesRequestOnTimeout_whenFutureRespondedButCouldNotComplete: the mockAsyncContextrejectscomplete()the way Jetty does, and the test asserts that the timeout listener still completes the request without changing the response.-
test_start_timeoutWaitsForResponseBeingWrittenByAnotherThread: asserts that the timeout listener blocks while another thread is writing the response.AsyncRequestJettyTest(new): reproduces the CI failure against a real Jetty server. It uses Druid'sHttpClientwith a single connection, followingDirectDruidClientAbortHttpTest. Latches force the order seen in CI, so the test does not depend on timing. It asserts that a second request on the same connection is answered.Verification (in a checkout of this branch):
AsyncRequestTest16/16,AsyncRequestJettyTest1/1,OverlordResourceTest52/52. Static checks pass.AsyncRequest.java(new tests kept): both newAsyncRequestTesttests fail.AsyncRequestJettyTestfails with aReadTimeoutExceptionon the second request, and Jetty logs the sameIllegalStateException: COMMITTEDfromHttpChannelState.sendError(HttpChannelState.java:810)as in CI.KafkaMultiSupervisorTest(embedded) passed 2/2 with an earlier revision of the fix that had the same logic, before thefinallyand the Javadoc notes were added. The race did not occur in that run, so this only shows that nothing regressed.<hr>
##### Key changed/added classes in this PR
AsyncRequestAsyncRequestTest*
AsyncRequestJettyTest<hr>
This PR has:
- [x] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)
Generative AI usage
Generated by: Claude Opus 5.5
Root cause, fix, tests, PR description by Claude. All code manually reviewed by @ykisana.