fix(spring-jakarta): [Queue Instrumentation 38] Close leaked Kafka interceptor scope#5345
Draft
adinauer wants to merge 1 commit intofix/kafka-otel-agent-autoconfigfrom
Draft
Conversation
Store the lifecycle token in the thread-local before trace continuation or transaction startup can throw. This keeps the cleanup path reachable and closes the forked scopes even when interceptor preparation fails. Also log the preparation failure instead of letting the interceptor break customer processing.
This was referenced Apr 29, 2026
Open
Open
This was referenced Apr 29, 2026
📲 Install BuildsAndroid
|
Contributor
Performance metrics 🚀
|
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
Ensure
SentryKafkaRecordInterceptordoes not leak its forked scopes when interceptor setup throws.The interceptor now stores the lifecycle token in
currentContextimmediately aftermakeCurrent(), beforecontinueTrace()orstartTransaction()can fail. If setup fails, the existing cleanup path can still close the token instead of leaving the forked scopes current on the consumer thread.💡 Motivation and Context
A review comment pointed out that
continueTrace()orstartTransaction()could throw aftermakeCurrent()succeeded but before the lifecycle token was saved into the thread-local. In that casefailure()andclearThreadState()would see no context to finish, and the lifecycle token would never be closed.This change keeps the cleanup path intact and logs setup failures without breaking Kafka consumer processing.
💚 How did you test it?
./gradlew spotlessApply apiDump./gradlew :sentry-spring-jakarta:test --tests='*SentryKafkaRecordInterceptorTest*'📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
Continue tightening the remaining Queue Instrumentation error paths.
#skip-changelog