Guard Tomcat CoyoteAdapter advice against null-scope NPE - #12375
Draft
dougqh wants to merge 1 commit into
Draft
Conversation
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>
3 tasks
Contributor
|
🎯 Code Coverage (details) 🔗 Commit SHA: bcad0c3 | Docs | View more details | Give us feedback! |
Contributor
🟢 Java Benchmark SLOs — All performance SLOs passed
PR vs. master results
Commit: Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Does This Do
Guards the Tomcat
CoyoteAdapterserver advice against a null-scopeNullPointerExceptionthat can occur if the paired enter-advice fails before assigning its@Advice.LocalContextScope.TomcatServerInstrumentation.ContextTrackingAdvice.closeScopeandTomcatServerInstrumentation.ServiceAdvice.closeScopeboth called.close()unconditionally on an@Advice.LocalContextScope. Both are guarded withif (scope != null)now.Motivation
Error Tracking issue: https://app.datadoghq.com/error-tracking/issue/5ce30218-b43b-11f0-ac08-da7ad0900002
Logged via the agent's own suppressed-exception mechanism as:
ContextTrackingAdvice.extractParentandServiceAdvice.onServiceare both@Advice.OnMethodEnter(suppress = Throwable.class). If either throws before assigning its@Advice.LocalContextScope(e.g.DECORATE.extract(req),DECORATE.startSpan(...), or a request-attribute call throwing inside an app's customRequest/attribute wrapper), the exception is silently swallowed by ByteBuddy's suppression bytecode.CoyoteAdapter.service()then proceeds normally, and the paired@Advice.OnMethodExitcloseScopeunconditionally 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 bareVirtualThread.runframe with no other context.A related but narrower instance of this same failure mode (
parentContext == nullspecifically) was already fixed in #11968 (rootContext()fallback), but the underlying structural issue —closeScopenot 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 twocloseScopeadvice methods withnulland with a realContextScope, since both are plain public static methods andContextScopeis an interface — no bytecode instrumentation harness or mocking framework needed.Contributor Checklist
docs/release_notes.mdif this change affects the user-facing functionality. — N/A, internal fix.Jira ticket: [PROJ-IDENT]