← Back to Pull requests

fix: Jersey 204 no longer overrides the status of asynchronous requests #20961

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 24, 2026 05:51 UTC
Author

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 own AsyncContext) can have the status of its response replaced by 204, and the client then gets 204 No Content with 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-input from RemoteTaskActionClient.submit: the Overlord had answered POST /druid/indexer/v1/action with 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 (or null), WebComponent.Writer.finish() calls setStatus(204) on the servlet response, on the request thread, after the method has returned.

OverlordResource.performTaskAction (asynchronous since #20505) calls request.startAsync() through AsyncRequest, returns void, 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 the AsyncContext inside 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 after AsyncRequest has 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 with No 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 | ChangeRequestHttpSyncer and HttpLoadQueuePeon take 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, through ChangeRequestHttpSyncer) | 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 same void return. |

QueryResource / QueryResultPusher also 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

DruidGuiceContainer now hands Jersey an HttpServletResponseWrapper that ignores setStatus(204) while request.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 the AsyncContext.

Why this is safe:

  • Every other status passes through, and so does 204 on a request that is not asynchronous. A ForbiddenException thrown after QueryResultPusher started asynchronous processing is still answered by Jersey with 403.

  • The asynchronous side is not affected: request.startAsync() gives an AsyncContext whose 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.

  • The outcome no longer depends on which thread is first.
  • Alternative considered: making each affected resource method return a Response, or committing the response inside AsyncRequest before 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 an AsyncContext, now and later. The wrapper fixes it in the one place where Jersey meets the servlet response.

    Tests

    JettyAsyncRequestTest runs AsyncRequest behind Jersey on a Jetty server, set up through GuiceFilter as 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 to DruidGuiceContainer both 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 Content and an empty body in place of its real response. Tasks could fail from it with No content to map due to end-of-input.

    <hr>

    ##### Key changed/added classes in this PR
    DruidGuiceContainer
    JettyAsyncRequestTest

    <hr>

    This PR has:

  • been self-reviewed.

  • - [x] using the concurrency checklist
  • a release note entry in the PR description.

  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: FrankChen021 Review duration: 10196m
    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
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.5% · Safe to merge
    32.5%
    Repository