Add null-safe ContextScope.close(scope) helper - #12377
Draft
dougqh wants to merge 1 commit into
Draft
Conversation
Many advice classes attach a ContextScope on enter and close it unconditionally on exit; if the enter advice throws before assigning the scope (its exception typically suppressed by suppress = Throwable.class), the exit advice NPEs on the null scope, masking the real failure. This adds a static helper that call sites can use instead of scope.close() to avoid that class of bug without a null check at every site. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
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. |
Contributor
|
+1 for adding this helper, and for consistency across instrumentations |
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
Adds a static, null-safe
ContextScope.close(ContextScope scope)helper that no-ops onnullinstead of throwing.Motivation
While fixing #12375 (a
NullPointerExceptionin Tomcat'sCoyoteAdapteradvice), I found the underlying shape of that bug is a widespread, copy-pasted pattern across instrumentation advice:@Advice.OnMethodEnter(suppress = Throwable.class)method attaches aContextScope(or the olderAgentScope) and assigns it to an@Advice.Local/@Advice.Entervariable, but the attach happens after other code that can throw (a decorator call, a request-attribute lookup, etc).null.@Advice.OnMethodExitthen calls.close()on that (still-null) local unconditionally, producing a second, maskingNullPointerExceptionthat's also swallowed and logged, hiding the real root cause entirely.A repo-wide search turned up 40+ advice classes across 25+ instrumented libraries with this exact unguarded shape — including all 5 Jetty server versions, Spring MVC's
RenderAdvice, Spring WebFlux'sDispatcherHandlerAdvice, and the generic@Trace/OpenTelemetry@WithSpanannotation advice used across user-code method tracing. Some advice classes already null-check correctly (Undertow, Servlet, gRPC, Play, Liberty, and — inconsistently — some sibling advice classes in the very same files as the unguarded ones), so this isn't a case of the pattern being unknown, just inconsistently applied.Additional Notes
This PR only adds the shared helper, as a starting point for discussion — it does not sweep the 40+ call sites yet. That's intentionally left for follow-up PRs (either a phased sweep, or converting sites opportunistically as they're touched) pending team input on the right rollout approach.
ContextScopelives incomponents/context, a low-level module shared across the whole tracer, so this fix (and any future call-site conversions) benefits every instrumentation, not just one.Contributor Checklist
docs/release_notes.mdif this change affects the user-facing functionality. — N/A, internal helper, not yet adopted anywhere.Jira ticket: [PROJ-IDENT]