fix: Jersey 204 no longer overrides the status of asynchronous requests #20961
All Vouch review confidence checks passed
Residual risk 0.33 = Change Risk (0.57) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 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.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #20961"
general
weight: 0.35
FrankChen021
Note from the human
I was chasing down a flaky test that turned out to be an actual prod issue an not a bad test. Claude researched it and found this to be responsible. The fix looks ok to me but I want to verify that the extension point this uses is ok or not.
Description
A resource method that answers asynchronously (through
AsyncRequest, or its ownAsyncContext) can have the status of its response replaced by 204, and the client then gets204 No Contentwith no body in place of the real answer.This was seen as an intermittent task failure with
MismatchedInputException: No content to map due to end-of-inputfromRemoteTaskActionClient.submit: the Overlord had answeredPOST /druid/indexer/v1/actionwith 204 and an empty body. No existing test is known to fail from it, and I found no existing issue.Mechanism
Jersey 1 knows nothing of servlet asynchronous processing. When a resource method returns
void(ornull),WebComponent.Writer.finish()callssetStatus(204)on the servlet response, on the request thread, after the method has returned.OverlordResource.performTaskAction(asynchronous since #20505) callsrequest.startAsync()throughAsyncRequest, returnsvoid, and the response is written when the future of the action completes. Two things go wrong:1. Status-only answers made on the request thread, every time.
AsyncRequest.completed(request, jsonMapper, AsyncResponse.status(503)), the "not the leader, try again soon" answer, sets 503 and completes theAsyncContextinside the resource method. A response with no body is not committed at that point, so the 204 that Jersey sets right after replaces the 503.2. Answers written by another thread, as a race. When the future completes on another thread before the request thread is back in Jersey, both threads work on the same response. If Jersey's
setStatus(204)lands afterAsyncRequesthas set 200 and before the body commits the response, the response goes out as 204, and Jetty sends no body for a 204.Who is affected
Every resource method that starts asynchronous processing and returns without a response:
| Endpoint | Client | Effect of a 204 |
|---|---|---|
|
OverlordResource.performTaskAction(POST /druid/indexer/v1/action) |RemoteTaskActionClient| 204 is a success for the service client, so nothing is retried: the task action fails withNo content to map due to end-of-input. The 503 of a non-leader Overlord, which the client would retry, is lost the same way. ||
SegmentListerResource(getSegments,applyDataSegmentChangeRequests) |HttpServerInventoryView,HttpLoadQueuePeon|ChangeRequestHttpSyncerandHttpLoadQueuePeontake 204 for the answer of a timed-out long poll and ask again, so a lost answer costs one polling round. ||
TaskManagementResource.getWorkerState|HttpRemoteTaskRunner(WorkerHolder, throughChangeRequestHttpSyncer) | Same as above. ||
MessageRelayResource.httpGetMessagesFromOutbox|MessageRelayClientImpl| 204 is read as an empty batch at the same watermark (it is also the answer of a timed-out long poll), so a lost answer costs one polling round. || MSQ
WorkerResource.httpGetChannelData|BaseWorkerClientImpl| Not analysed beyond the shape of the resource method: the status and the data are written from a callback of a future, with the samevoidreturn. |QueryResource/QueryResultPusheralso start asynchronous processing, but they write the response on the request thread before the resource method returns, so the response is committed by the time Jersey sets 204.I have only observed the failure on the first endpoint. The others are listed because they have the same shape; I have not reproduced a failure on them.
Fixed the bug
DruidGuiceContainernow hands Jersey anHttpServletResponseWrapperthat ignoressetStatus(204)whilerequest.isAsyncStarted(). Jersey sets 204 only for a method that returned no response, and a method that did so after starting asynchronous processing has handed the response to whoever completes theAsyncContext.Why this is safe:
ForbiddenExceptionthrown afterQueryResultPusherstarted asynchronous processing is still answered by Jersey with 403.request.startAsync()gives anAsyncContextwhose response is the original one, not the wrapper, so a 204 that it sets itself (for example the timeout of the long polls above) still goes out.Alternative considered: making each affected resource method return a
Response, or committing the response insideAsyncRequestbefore returning. The first does not fit an answer that is not known when the method returns, and both would have to be repeated in every resource that uses anAsyncContext, now and later. The wrapper fixes it in the one place where Jersey meets the servlet response.Tests
JettyAsyncRequestTestrunsAsyncRequestbehind Jersey on a Jetty server, set up throughGuiceFilteras in a Druid server. One case holds the thread that completes the future inside the serialization of the body until the request thread has left the filter chain, which forces the interleaving of (2); the other answers 503 with no body from the request thread, as in (1). Without the change toDruidGuiceContainerboth get 204 on every run (expected: <200> but was: <204>,expected: <503> but was: <204>); with it they get 200 with the body, and 503.Release note
Fixed a bug where an HTTP endpoint that answers asynchronously, such as the Overlord's task action endpoint, could respond with
204 No Contentand an empty body in place of its real response. Tasks could fail from it withNo content to map due to end-of-input.<hr>
##### Key changed/added classes in this PR
DruidGuiceContainerJettyAsyncRequestTest<hr>
This PR has:
- [x] using the concurrency checklist