Skip to content

Guard Tomcat CoyoteAdapter advice against null-scope NPE (quick fix) - #12375

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
masterfrom
dougqh/fix-tomcat-coyoteadapter-npe
Oct 8, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
masterfrom
dougqh/fix-tomcat-coyoteadapter-npe

Conversation

@dougqh

@dougqh dougqh commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Guards the Tomcat CoyoteAdapter server advice against a null-scope NullPointerException that can occur if the paired enter-advice fails before assigning its @Advice.Local ContextScope.

TomcatServerInstrumentation.ContextTrackingAdvice.closeScope and TomcatServerInstrumentation.ServiceAdvice.closeScope both called .close() unconditionally on an @Advice.Local ContextScope. Both are guarded with if (scope != null) now.

Motivation

Error Tracking issue: https://app.datadoghq.com/error-tracking/issue/5ce30218-b43b-11f0-ac08-da7ad0900002

java.lang.NullPointerException
  at (redacted: 6 frames)
  at java.base/java.lang.VirtualThread.run(VirtualThread.java:460)

Logged via the agent's own suppressed-exception mechanism as:

Failed to handle exception in instrumentation for org.apache.catalina.connector.CoyoteAdapter

ContextTrackingAdvice.extractParent and ServiceAdvice.onService are both @Advice.OnMethodEnter(suppress = Throwable.class). If either throws before assigning its @Advice.Local ContextScope (e.g. DECORATE.extract(req), DECORATE.startSpan(...), or a request-attribute call throwing inside an app's custom Request/attribute wrapper), the exception is silently swallowed by ByteBuddy's suppression bytecode. CoyoteAdapter.service() then proceeds normally, and the paired @Advice.OnMethodExit closeScope unconditionally calls .close() on the still-null local — a second NPE that is also swallowed and logged, fully masking the original failure. Because both exceptions are handled by generated advice bytecode rather than real application frames, the visible stack trace is almost entirely redacted, which is why this surfaces as a bare VirtualThread.run frame with no other context.

A related but narrower instance of this same failure mode (parentContext == null specifically) was already fixed in #11968 (rootContext() fallback), but the underlying structural issue — closeScope not tolerating a null local — was never addressed, so any other exception in the enter-advice reproduces the same NPE.

Additional Notes

Added a direct JUnit 5 unit test (TomcatServerInstrumentationTest) calling the two closeScope advice methods with null and with a real ContextScope, since both are plain public static methods and ContextScope is an interface — no bytecode instrumentation harness or mocking framework needed.

Contributor Checklist

  • Add an entry in docs/release_notes.md if this change affects the user-facing functionality. — N/A, internal fix.
  • Verify code coverage of new lines
  • Confirm this change does not add flakiness in tests

Jira ticket: [PROJ-IDENT]

If extractParent()/onService() throws before assigning its @Advice.Local
ContextScope, the throwable is swallowed (suppress = Throwable.class), and
the paired closeScope() then NPEs on the still-null local, masking the real
failure and producing a misleading "Failed to handle exception in
instrumentation for CoyoteAdapter" log entry.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dougqh dougqh added type: bug fix Bug fix inst: others All other instrumentations tag: ai generated Largely based on code generated by an AI or LLM labels Sep 2, 2026
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

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

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

@dd-octo-sts

dd-octo-sts Bot commented Sep 2, 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 13.96 s 13.95 s [-0.8%; +1.0%] (no difference)
startup:insecure-bank:tracing:Agent 12.84 s 12.97 s [-1.8%; -0.2%] (maybe better)
startup:petclinic:appsec:Agent 17.68 s 17.68 s [-0.8%; +0.8%] (no difference)
startup:petclinic:iast:Agent 17.46 s 17.62 s [-1.6%; -0.2%] (maybe better)
startup:petclinic:profiling:Agent 17.33 s 17.42 s [-1.7%; +0.7%] (no difference)
startup:petclinic:sca:Agent 17.82 s 17.63 s [+0.3%; +1.9%] (maybe worse)
startup:petclinic:tracing:Agent 16.66 s 16.73 s [-1.4%; +0.6%] (no difference)

Commit: 827831f5 · 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.

@dougqh dougqh changed the title Guard Tomcat CoyoteAdapter advice against null-scope NPE Guard Tomcat CoyoteAdapter advice against null-scope NPE (quick fix) Sep 15, 2026
@dougqh

dougqh commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 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-10-07T20:49:34.683214Z 827831f 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 827831f5ea

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@dougqh
dougqh marked this pull request as ready for review October 7, 2026 20:47
@dougqh
dougqh requested review from a team as code owners October 7, 2026 20:47
@dougqh
dougqh requested review from ValentinZakharov and amarziali and removed request for a team October 7, 2026 20:47

@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

Both CoyoteAdapter exit-advice methods tolerate a null scope after suppressed enter-advice failures, while preserving cleanup for non-null scopes.

Was this helpful? React 👍 or 👎

Open Bits AI session

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

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

thanks

@dougqh
dougqh 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 12:58:06 UTC ℹ️ Start processing command /merge


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

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


2026-10-08 14:19:29 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 8, 2026
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit 1209bf5 into master Oct 8, 2026
612 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the dougqh/fix-tomcat-coyoteadapter-npe branch October 8, 2026 14:19
@github-actions github-actions Bot added this to the 1.68.0 milestone Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: others All other instrumentations 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