fix(spring-jakarta): [Queue Instrumentation 9] Clean up stale ThreadLocal context in Kafka consumer interceptor#5279
Draft
adinauer wants to merge 1 commit intofix/queue-instrumentation-otel-dedupfrom
Conversation
…umer interceptor Implement clearThreadState() and defensive cleanup in intercept() to prevent ThreadLocal leaks of SentryRecordContext. Spring Kafka calls clearThreadState() in the poll loop's finally block, making it the most reliable cleanup hook for edge cases where success()/failure() callbacks are skipped (e.g. Error thrown by listener). Also add defensive cleanup at the start of intercept() to handle any stale context from a previous record that was not properly cleaned up. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. This PR will not appear in the changelog. 🤖 This preview updates automatically when you update the PR. |
Sentry Build Distribution
|
This was referenced Apr 10, 2026
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 482e951 | 317.11 ms | 375.46 ms | 58.35 ms |
| d5c7c78 | 367.02 ms | 442.91 ms | 75.89 ms |
| a0ce355 | 308.90 ms | 355.65 ms | 46.75 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 482e951 | 0 B | 0 B | 0 B |
| d5c7c78 | 0 B | 0 B | 0 B |
| a0ce355 | 0 B | 0 B | 0 B |
This was referenced Apr 10, 2026
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.
PR Stack (Queue Instrumentation)
📜 Description
Implement
clearThreadState()and defensive cleanup inintercept()onSentryKafkaRecordInterceptorto prevent ThreadLocal leaks ofSentryRecordContext(lifecycle token + transaction).Spring Kafka's
RecordInterceptorextendsThreadStateProcessor, which providesclearThreadState()— called in the poll loop'sfinallyblock, making it the most reliable cleanup hook. This catches edge cases wheresuccess()/failure()callbacks are skipped (e.g.Errorthrown by listener).Also adds defensive cleanup at the start of
intercept()to handle any stale context from a previous record that was not properly cleaned up.💡 Motivation and Context
Review finding F-001 identified that
SentryRecordContextstored in ThreadLocal is only cleaned insuccess()/failure(). While the realistic leak surface is narrow for synchronous listeners (Spring Kafka reliably calls these callbacks in most scenarios), implementingclearThreadState()is a trivially cheap safety net that Spring already calls on our instance.💚 How did you test it?
clearThreadState cleans up stale contextclearThreadState is no-op when no context existsintercept cleans up stale context from previous record📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
Nothing — this is a standalone defensive fix.
#skip-changelog