Skip to content

fix(spring-jakarta): [Queue Instrumentation 38] Close leaked Kafka interceptor scope#5345

Draft
adinauer wants to merge 1 commit intofix/kafka-otel-agent-autoconfigfrom
fix/kafka-interceptor-lifecycle-token
Draft

fix(spring-jakarta): [Queue Instrumentation 38] Close leaked Kafka interceptor scope#5345
adinauer wants to merge 1 commit intofix/kafka-otel-agent-autoconfigfrom
fix/kafka-interceptor-lifecycle-token

Conversation

@adinauer
Copy link
Copy Markdown
Member

@adinauer adinauer commented Apr 29, 2026

PR Stack (Queue Instrumentation)


📜 Description

Ensure SentryKafkaRecordInterceptor does not leak its forked scopes when interceptor setup throws.

The interceptor now stores the lifecycle token in currentContext immediately after makeCurrent(), before continueTrace() or startTransaction() 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() or startTransaction() could throw after makeCurrent() succeeded but before the lifecycle token was saved into the thread-local. In that case failure() and clearThreadState() 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?

  • Ran ./gradlew spotlessApply apiDump
  • Ran ./gradlew :sentry-spring-jakarta:test --tests='*SentryKafkaRecordInterceptorTest*'

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Continue tightening the remaining Queue Instrumentation error paths.

#skip-changelog

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

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
@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 29, 2026

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.37.1 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions
Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 291.52 ms 362.06 ms 70.54 ms
Size 0 B 0 B 0 B

Baseline results on branch: fix/kafka-otel-agent-autoconfig

Startup times

Revision Plain With Sentry Diff
f1a44de 339.15 ms 390.35 ms 51.19 ms

App size

Revision Plain With Sentry Diff
f1a44de 0 B 0 B 0 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant