Skip to content

Prevent context retention in OpenJDK virtual pollers and HttpClient selector - #12626

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
masterfrom
andrea.marziali/fix-jdk-poller-context
Oct 12, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
masterfrom
andrea.marziali/fix-jdk-poller-context

Conversation

@amarziali

@amarziali amarziali commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Prevent request context retention in two specific OpenJDK virtual-thread infrastructure paths:

  • Create virtual I/O pollers under root context, then restore the caller's context. Cover sun.nio.ch.Poller$Pollers.start() on JDK 22–26, plus Poller$VThreadsPollerGroup.start() and lazy per-carrier read-poller creation through Poller$PollerPerCarrierPollerGroup.startReadPoller() on JDK 27.
  • Exclude jdk.internal.net.http.HttpClientImpl$SelectorManager from virtual-thread context capture, covering the virtual HTTP client selector introduced in JDK 26.
  • Preserve context propagation for ordinary application virtual threads, including threads created with inheritable thread locals disabled.

Motivation

The smoke-test continuation diagnostics enforced by #12609 exposed request retention when long-lived infrastructure virtual threads were created under an active request. First-use I/O poller initialization could capture that request in process-lifetime SubPoller threads; creating a JDK 26 HttpClient could retain it in the client's virtual selector.

Capturing a request continuation in these threads can delay trace publication until the threads terminate. Disabling async capture alone is insufficient because virtual-thread instrumentation also stores the raw context independently. Poller startup and lazy per-carrier read-poller creation therefore swap in root context directly, including when the caller has reached the scope-depth limit, and restores the previous context on exit.

Additional Notes

Scope and limitations

This change targets the named internal classes in HotSpot-based OpenJDK implementations. Compatibility with other JVM implementations, such as OpenJ9, has not been established.

The following path remains outside this change:

  • JDK 26 HTTP/3 / QUIC: the QUIC virtual selector and endpoint read worker identified by Brice are outside this change. The read worker reaches virtual-thread construction wrapped in ThreadPerTaskExecutor$ThreadBoundFuture, so extending the task-name exclusion alone would not cover both paths. See the review finding.

Regression coverage added

  • First-use virtual poller initialization under an active request on JDK 22–27: live pollers retain neither context nor continuation, and the caller's context is restored. The test uses numeric polling mode 2 and reads POLLERS on older JDKs or POLLER_GROUP on JDK 27.
  • JDK 27 per-carrier polling mode 3: blocking socket I/O under an active request creates live virtual read pollers without retaining request context or continuation, and restores the requesting virtual thread's context.
  • JDK 26 HTTP client creation under an active request: the live selector retains no virtual-thread state, and the request trace can publish before the client is closed.
  • Ordinary application virtual threads continue to inherit the active span with inheritable thread locals enabled or disabled.

Validation

  • Full virtual-thread module test and forkedTest suites pass on JDK 21, 25, and 27; all 28 tests run and pass on JDK 27.
  • Both JDK 27 poller regression tests fail with retained virtual-thread state when poller advice is temporarily disabled, and pass with the advice enabled.
  • Formatting checks pass for the virtual-thread module and agent tooling.

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@amarziali
amarziali requested review from a team as code owners September 24, 2026 07:40
@amarziali amarziali added the type: bug fix Bug fix label Sep 24, 2026
@amarziali
amarziali removed the request for review from a team September 24, 2026 07:40
@amarziali amarziali added the inst: java Core Java language instrumentation label Sep 24, 2026
@amarziali
amarziali requested a review from bric3 September 24, 2026 07:40
@amarziali amarziali added the tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors label Sep 24, 2026
@amarziali
amarziali requested review from jordan-wong and removed request for a team September 24, 2026 07:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T07:44:30.364300Z 99ccea5 PR opened
🔒 Security Review ✅ Completed 2026-09-24T07:44:26.740297Z 99ccea5 PR opened
ℹ️ 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.

@datadog-datadog-prod-us1

This comment has been minimized.

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 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: FAIL

The two most critical issues are a production memory-retention bug in PollerInstrumentation where request context leaks into long-lived poller threads when the scope-depth limit is reached, and a test compatibility failure in PollerContextForkedTest that throws NoSuchFieldException on JDK 22 due to a missing POLLERS field.

Open Bits AI session

🤖 Bits Code Review · Commit 99ccea5 · @DataDog review to ask questions

@dd-octo-sts

dd-octo-sts Bot commented Sep 24, 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.71 s 14.57 s [+0.1%; +1.9%] (maybe worse)
startup:insecure-bank:tracing:Agent 13.54 s 13.66 s [-1.7%; +0.0%] (no difference)
startup:petclinic:appsec:Agent 17.64 s 17.56 s [-0.4%; +1.4%] (no difference)
startup:petclinic:iast:Agent 16.74 s 17.33 s [-7.5%; +0.6%] (no difference)
startup:petclinic:profiling:Agent 16.79 s 17.06 s [-6.0%; +2.8%] (no difference)
startup:petclinic:sca:Agent 17.64 s 17.49 s [-0.3%; +2.0%] (no difference)
startup:petclinic:tracing:Agent 16.26 s 16.63 s [-6.7%; +2.2%] (no difference)

Commit: 47a4b179 · 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.

@amarziali

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 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

The JDK poller and HTTP selector paths no longer retain a request context. Application virtual threads still propagate the request context.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit b5dcae4 · @DataDog review to ask questions

@bric3 bric3 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.

I think the PR title deserves to be more specific, it's about Java's HttpClient and VTs.

Moreover this change already appears outdated as OpenJDK 27 change a few things. Finally since it touches Java's HttpClient on the surface, but actually it's specific to Hotspot based JDKs, that could remain so, but I'll clarify this explicitly in code comments and PR description.

@PerfectSlayer PerfectSlayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Between the narrow focus of JDK 26 + HttpClient + HotSpot, the PR brings too much complexity compared to the problem it tries to solve 🤔

@amarziali

Copy link
Copy Markdown
Contributor Author

Between the narrow focus of JDK 26 + HttpClient + HotSpot, the PR brings too much complexity compared to the problem it tries to solve 🤔

@PerfectSlayer which complexity do you mean? This PR only narrows the propagation boundary for one specific case that showed up in a smoke test, and it adds a single extra instrumentation. It isn't meant to cover every case, so I'd rather not widen the scope of this review.

We're not running JDK 27 yet, and we'll fix it when we get to test with that. In case the instrumentation won't apply with this JDK it will be harmless.

We're applying the same kind of boundary containment we already do for classic platform threads, and in my view we didn't go as in depth as we should when instrumenting virtual threads. All the machinery around observing continuation usage is what's helping us understand and fill in these missing cases.

@amarziali amarziali changed the title Prevent JDK virtual I/O threads from retaining request context Prevent context retention in OpenJDK virtual pollers and HttpClient selector Oct 8, 2026
@amarziali
amarziali enabled auto-merge October 8, 2026 07:55
@amarziali
amarziali added this pull request to the merge queue Oct 8, 2026
@dd-octo-sts

dd-octo-sts Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-08 08:35:46 UTC ℹ️ Start processing command /merge


2026-10-08 08:36:00 UTC ℹ️ MergeQueue: Pull request is not mergeable yet

It will be processed automatically as soon as GitHub reports it as mergeable. View in MergeQueue UI.

  • Run /code blockers to see what is blocking it.
  • Run /remove to cancel it.

2026-10-08 08:37:10 UTC ℹ️ MergeQueue: merge request added to the queue

The expected merge time in master is approximately 1h (p90).


2026-10-08 09:16:25 UTC ❌ MergeQueue: The build pipeline contains failing jobs for this merge request

Build pipeline has failing jobs for 58cec04:

⚠️ Do NOT retry failed jobs directly (why?).

What to do next?

  • Investigate the failures and when ready, re-add your pull request to the queue!
  • If your PR checks are green, try to rebase/merge. It might be because the CI run is a bit old.
  • Any question, go check the FAQ.
Details

Since those jobs are not marked as being allowed to fail, the pipeline will most likely fail.
Therefore, and to allow other builds to be processed, this merge request has been rejected and the pipeline got canceled.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 8, 2026
@amarziali
amarziali added this pull request to the merge queue Oct 9, 2026
@dd-octo-sts

dd-octo-sts Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-09 08:09:21 UTC ℹ️ Start processing command /merge


2026-10-09 08:09:25 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 1h (p90).


2026-10-09 08:11:34 UTC ⚠️ MergeQueue: This merge request build was cancelled

andrea.marziali@datadoghq.com cancelled this merge request build

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 9, 2026
@amarziali

Copy link
Copy Markdown
Contributor Author

/merge -c

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-09 08:11:21 UTC ℹ️ Start processing command /merge -c

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-09 08:11:22 UTC ℹ️ Start processing command /merge -c

@bric3 bric3 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, but I believe the code owner is odd.

Comment thread .github/CODEOWNERS Outdated
@amarziali
amarziali enabled auto-merge October 12, 2026 06:59
@amarziali
amarziali added this pull request to the merge queue Oct 12, 2026
@dd-octo-sts

dd-octo-sts Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-12 07:06:15 UTC ℹ️ Start processing command /merge


2026-10-12 07:06:19 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 1h (p90).


2026-10-12 08:21:06 UTC ℹ️ MergeQueue: This merge request was merged

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 12, 2026
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit d72c58d into master Oct 12, 2026
823 of 825 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the andrea.marziali/fix-jdk-poller-context branch October 12, 2026 08:21
@github-actions github-actions Bot added this to the 1.68.0 milestone Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: java Core Java language instrumentation tag: concurrency Virtual Threads, Coroutines, Async, RX, Executors type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants