test(spring-jakarta): [Queue Instrumentation 30] Cover Kafka record interceptor lifecycle edge cases#5327
Open
adinauer wants to merge 1 commit intofix/queue-instrumentation-spring-kafka-body-sizefrom
Conversation
…nterceptor lifecycle edge cases Add three regression tests for SentryKafkaRecordInterceptor that pin down the lifecycle contract around clearThreadState cleanup: - full lifecycle intercept -> success -> clearThreadState closes the lifecycle token exactly once and does not double-finish the transaction - when a delegating interceptor returns null from intercept (filtering the record), the safety net in clearThreadState still finishes the transaction and closes the token - when a delegating interceptor throws from intercept, clearThreadState still finishes the transaction and closes the token after the exception has propagated Addresses review finding R6-F001. Co-Authored-By: Claude <noreply@anthropic.com>
This was referenced Apr 23, 2026
Open
Open
8 tasks
📲 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
Add three regression tests for
SentryKafkaRecordInterceptorthat pin down the lifecycle contract aroundclearThreadStatecleanup.Scenarios covered:
setupThreadState → intercept → success → clearThreadStatecloses the forked scope's lifecycle token exactly once and does not double-finish the transaction. Also verifies all three delegate hooks are invoked.interceptreturnsnull— per Spring Kafka's contract,success/failureare not invoked when the record is filtered. The safety net inclearThreadState→finishStaleContextstill finishes the transaction (withUNKNOWNstatus) and closes the lifecycle token, and the delegating interceptor'sclearThreadStateis still called.interceptthrows — the exception propagates out ofSentryKafkaRecordInterceptor.intercept. A subsequentclearThreadStatecall still finishes the transaction and closes the lifecycle token via the same safety net. (Note: in the real Spring Kafka container,KafkaMessageListenerContainer.doInvokeRecordListeneralso catches this and invokesfailure— this test focuses on the interceptor's own fallback path.)No source changes. The existing
clearThreadStatesafety net already guarantees cleanup on all three paths; these tests lock the guarantee in place against future refactors.💡 Motivation and Context
Addresses review finding R6-F001: prior tests verified
setupThreadStateandclearThreadStatedelegation in isolation, but nothing exercised the full Spring Kafka listener lifecycle end to end. A future refactor offinishSpanorfinishStaleContextcould silently introduce a double-finish (or break the safety-net cleanup whensuccess/failureare not invoked) without any test failing.These tests make the "intercept-exits-abnormally still gets cleaned up exactly once" contract explicit.
💚 How did you test it?
./gradlew :sentry-spring-jakarta:test --tests "*SentryKafkaRecordInterceptorTest*"— all 26 tests pass (23 prior + 3 new)../gradlew spotlessApply apiDump— clean, no API changes.📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
Remaining open review findings from review6.md:
KafkaOtelCoexistenceSystemTestnegative assertion not anchored to observable activityclearThreadStatewhen both Sentry cleanup and delegate cleanup throw (P3)#skip-changelog