Skip to content

Fix Apache HttpClient 5 async spans reporting another request's response (quick fix) - #13468

Open
dougqh wants to merge 2 commits into
masterfrom
dougqh/httpclient5-own-response
Open

dougqh wants to merge 2 commits into
masterfrom
dougqh/httpclient5-own-response

Conversation

@dougqh

@dougqh dougqh commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Makes Apache HttpClient 5 async client spans take their status code and response header tags from their own response. Before, when the application shared one HttpContext across concurrent requests, a span could report another in-flight request's response.

Motivation

The async callback read the response from the application-supplied HttpContext first, and used the future's own result only as a fallback. A context reused across concurrent execute calls holds whichever exchange most recently set HTTP_RESPONSE, so:

  • spans could silently get another request's status code and response header tags
  • with response header tags configured, reading headers while the other exchange was still modifying them throws IndexOutOfBoundsException from HeaderGroup.getFirstHeader, reported as "Failed to decorate span on response"

The read order dates back to the original async instrumentation. It only became visible in instrumentation telemetry once #12017 made onResponse exception-safe and logged the failure. Before that, the exception escaped the callback, so the span was never finished and the application's callback never ran.

Additional Notes

  • The response now comes from the future result first: an HttpResponse (e.g. SimpleHttpResponse), or the head of a Message<HttpResponse, ?> (e.g. BasicResponseConsumer). The context is only a fallback when the result carries no response, such as on failure, cancellation or custom consumers.
  • Properly finish spans and support latest apache httpclient5 #8272 added the result fallback for newer clients that don't populate the context. It kept context-first only to preserve the existing behavior, with no reason given for that order.
  • Message exists since httpcore5 5.0, so the muzzle range is unchanged.
  • The new unit tests cover the selection order. The two shared-context cases fail on the previous order. The existing async client tests pass on both 5.0 and latest.

Contributor Checklist

🤖 Generated with Claude Code

dougqh and others added 2 commits November 16, 2026 08:47
The async callback read the response from the application's HttpContext
before the future result. An application can share one context across
concurrent requests, so that attribute can hold another in-flight
exchange's response: the span got the wrong status and header tags, and
reading headers while the other exchange modified them threw
IndexOutOfBoundsException.

Prefer the response carried by the future result (an HttpResponse or the
head of a Message), and fall back to the context only when the result
carries none, as on failure or cancellation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh added type: bug fix Bug fix inst: apache httpcomponents Apache HttpComponents tag: ai generated Largely based on code generated by an AI or LLM labels Nov 16, 2026
@dougqh
dougqh marked this pull request as ready for review November 16, 2026 13:51
@dougqh
dougqh requested review from a team as code owners November 16, 2026 13:51
@dougqh
dougqh requested review from jandro996 and ygree and removed request for a team November 16, 2026 13:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T13:54:06.786276Z 9e21f70 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dougqh dougqh changed the title Fix Apache HttpClient 5 async spans reporting another request's response Fix Apache HttpClient 5 async spans reporting another request's response (quick fix) Nov 16, 2026

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Direct responses and Message response heads take precedence over a shared HttpContext, preventing another exchange’s response from overriding them while retaining fallback for results without a response.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 9e21f70

@datadog-datadog-prod-us1-2

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 68.82% (+8.59%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 9e21f70 | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.06 s 14.04 s [-0.7%; +1.0%] (no difference)
startup:insecure-bank:tracing:Agent 12.94 s 12.99 s [-1.2%; +0.5%] (no difference)
startup:petclinic:appsec:Agent 17.18 s 17.05 s [-0.2%; +1.6%] (no difference)
startup:petclinic:iast:Agent 16.51 s 16.67 s [-6.9%; +5.1%] (unstable)
startup:petclinic:profiling:Agent 16.60 s 17.04 s [-3.9%; -1.3%] (significantly better)
startup:petclinic:sca:Agent 17.14 s 16.98 s [-0.1%; +2.0%] (no difference)
startup:petclinic:tracing:Agent 16.20 s 16.08 s [-0.0%; +1.5%] (no difference)

Commit: 9e21f70f · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

Comment on lines +56 to +60
private static HttpContext contextWith(HttpResponse response) {
HttpContext context = new BasicHttpContext();
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
return context;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, optional, can be in follow up: maybe make sense to have a builder to create context in fluent way: retrun builder.setA().setB().setC().build(); ?

@Nullable
static HttpResponse extractHttpResponse(
@Nullable Object futureResult, @Nullable HttpContext context) {
if (futureResult instanceof HttpResponse) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A custom AsyncResponseConsumer<HttpResponse> can return a synthesized response. For example, if the server sends 404 and the consumer returns a new BasicHttpResponse(200), this branch makes the span report 200 even when the request has its own context containing the original 404. I verified that the selector returns 404 on the base and 200 on this head. Could we capture the exchange response before the consumer transforms it and add a regression test for this case?

--- found with Codex

* Returns the response of this exchange, preferring the one carried by the future result.
*
* <p>The context is supplied by the application and may be shared across concurrent requests, in
* which case its response attribute can belong to another in-flight exchange. It is only used

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we qualify the PR summary to cover results that carry an HTTP response? This fallback still reads the shared context on cancellation, failure, or a body-only result. For example, if A is cancelled before receiving a response and B has already stored a 503 in the shared context, A's span can still report B's 503.

--- found with Codex

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left couple of comments found by me and codex

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: apache httpcomponents Apache HttpComponents tag: ai generated Largely based on code generated by an AI or LLM type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants