Title: Memory Leak Observed: SentryScheduleHook + Reactor Kafka Interaction · Issue #5051 · getsentry/sentry-java · GitHub
Open Graph Title: Memory Leak Observed: SentryScheduleHook + Reactor Kafka Interaction · Issue #5051 · getsentry/sentry-java
X Title: Memory Leak Observed: SentryScheduleHook + Reactor Kafka Interaction · Issue #5051 · getsentry/sentry-java
Description: Integration sentry-spring-boot-starter-jakarta Java Version 21 Version 8.29.0 Steps to Reproduce Summary We observed a memory leak when using Sentry Java SDK with Reactor Kafka. The leak occurs even when idle (no messages), leading to Ou...
Open Graph Description: Integration sentry-spring-boot-starter-jakarta Java Version 21 Version 8.29.0 Steps to Reproduce Summary We observed a memory leak when using Sentry Java SDK with Reactor Kafka. The leak occurs eve...
X Description: Integration sentry-spring-boot-starter-jakarta Java Version 21 Version 8.29.0 Steps to Reproduce Summary We observed a memory leak when using Sentry Java SDK with Reactor Kafka. The leak occurs eve...
Opengraph URL: https://github.com/getsentry/sentry-java/issues/5051
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memory Leak Observed: SentryScheduleHook + Reactor Kafka Interaction","articleBody":"### Integration\n\nsentry-spring-boot-starter-jakarta\n\n### Java Version\n\n21\n\n### Version\n\n8.29.0\n\n### Steps to Reproduce\n\n## Summary\n\nWe observed a memory leak when using Sentry Java SDK with Reactor Kafka. The leak occurs even when idle (no messages), leading to OutOfMemoryError. The issue appears to stem from an interaction between the two libraries rather than either one alone.\n\n**Versions tested:** Sentry 8.29.0 and 8.30.0 (same behavior)\n\n## Environment\n\n- Java: OpenJDK 21.0.1 LTS\n- Spring Boot: 3.5.9\n- Reactor Kafka: 1.3.25\n- OS: macOS\n\n## Our Analysis\n\nBased on our investigation, we believe the issue arises from the following interaction:\n\n### The Mechanism\n\nReactor Kafka's `ConsumerEventLoop.PollEvent` reschedules itself infinitely on a **dedicated single-threaded scheduler**:\n\n```java\n// reactor-kafka ConsumerEventLoop.java:310-412\nclass PollEvent implements Runnable {\n public void run() {\n consumer.poll(pollTimeout);\n if (isActive.get()) {\n schedule(); // Reschedules itself infinitely\n }\n }\n void schedule() {\n eventScheduler.schedule(this); // Triggers SentryScheduleHook\n }\n}\n```\n\nEach `schedule()` call triggers `SentryScheduleHook.apply()`, which creates a forked scope with a parent reference:\n\n```java\n// SentryScheduleHook.java\npublic Runnable apply(Runnable runnable) {\n final IScopes newScopes = Sentry.getCurrentScopes().forkedCurrentScope(\"spring.scheduleHook\");\n return () -> { /* ... */ };\n}\n\n// Scopes.java:119 - each fork captures the parent\npublic IScopes forkedCurrentScope(String creator) {\n return new Scopes(scope.clone(), isolationScope, globalScope, this, creator);\n // ^^^^ parent reference\n}\n```\n\n**Result:** The dedicated scheduler thread is a permanent GC root, retaining an unbounded chain of `parentScopes`.\n\n### Why We Observed This Only with Reactor Kafka\n\nWe tested with `Flux.interval()` and other standard Reactor operations - no leak occurred. These use **pooled schedulers** where tasks complete and threads return to the pool, allowing scopes to be garbage collected.\n\nReactor Kafka differs by using a **dedicated single-threaded scheduler** (`Schedulers.newSingle()`) that:\n- Never terminates while the receiver is active\n- Reschedules the same `PollEvent` infinitely\n- Acts as a permanent GC root retaining all forked scopes\n\n### GC Root Path (from heap dump)\n\n```\nThread \"reactive-kafka-{groupId}-1\" [permanent GC root]\n └─ SchedulerTask (82,915 instances after 17 min)\n └─ ReentrantLock$NonfairSync\n └─ io.sentry.Scope\n └─ io.sentry.Scopes\n └─ parentScopes → Scopes → parentScopes → ... (unbounded chain)\n```\n\n## Test Results\n\n| Scenario | Duration | ReentrantLock Growth | Rate/min | Heap Growth |\n|----------|----------|---------------------|----------|-------------|\n| App + Sentry 8.29.0 | 19 min | +84,349 | ~4,439 | +34 MB |\n| Minimal repro + Sentry | 17 min | +69,758 | ~4,103 | +30 MB |\n| App without Sentry (*) | 15 min | **0** | **0** | **0 MB** |\n\n(*) Manual GC performed before heap dumps to eliminate transient objects.\n\n## Reproduction\n\nA minimal Spring Boot application is provided in the attached ZIP. See `README.md` for setup instructions.\n\n```bash\n./run-test.sh # Interactive test runner with 3 scenarios\n```\n\n## Workaround\n\nWe disable `SentryScheduleHook` after Sentry initializes (see `SentryWorkaroundConfiguration.kt`):\n\n```kotlin\n@EventListener(ApplicationReadyEvent::class) // Must run AFTER Sentry's ApplicationRunner\nfun disableSentryScheduleHook() {\n Schedulers.onScheduleHook(\"sentry\") { runnable -> runnable }\n}\n```\n\n**Trade-off:** Context propagation in scheduler tasks is disabled, but error reporting continues to work.\n\n---\n\n**Sentry Version:** 8.29.0, 8.30.0\n**Reactor Kafka Version:** 1.3.25\n**Spring Boot Version:** 3.5.9\n\n---\n\nWe hope this analysis is helpful. We may have misunderstood something, so please let us know if you need additional information or clarification. Happy to provide heap dumps or run additional tests if needed.\n\n[sentry-reactor-kafka-leak-reproduction.zip](https://github.com/user-attachments/files/24791402/sentry-reactor-kafka-leak-reproduction.zip)\n\n### Expected Result\n\n**Expected:** Memory should remain stable when the application is idle with no Kafka messages.\n\n### Actual Result\n\n**Actual:** Continuous growth of these objects (observable in heap dumps):\n- `java.util.concurrent.locks.ReentrantLock$NonfairSync`\n- `io.sentry.Scope` / `io.sentry.Scopes`\n- `io.sentry.PropagationContext`","author":{"url":"https://github.com/harrypennont","@type":"Person","name":"harrypennont"},"datePublished":"2026-01-22T09:47:09Z","url":"https://github.com/getsentry/sentry-java/issues/5051"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c0dc25ea-a7f6-72d7-2140-290e4d2c01a8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6DA:2F671E:728B6E:97269A:6B04C874 |
| html-safe-nonce | 2516ab01e53c7e103d3584e85ace3bd436a87f0c519c19e4e1de5c9fde211346 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkRBOjJGNjcxRTo3MjhCNkU6OTcyNjlBOjZCMDRDODc0IiwidmlzaXRvcl9pZCI6Ijg2MzQzMjU4NzY4MjMzNDUyNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f0c1a06d88916df951e9f77c5bb0a48a6ba5d29baa067dc126a806808a284c69 |
| hovercard-subject-tag | issue:3842110240 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-java/5051/issue_layout |
| twitter:image | https://opengraph.githubassets.com/81146ba17b7f60abca7eec5aae619e14435668e17743751c870d29b487ba93e5/getsentry/sentry-java/issues/5051 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/81146ba17b7f60abca7eec5aae619e14435668e17743751c870d29b487ba93e5/getsentry/sentry-java/issues/5051 |
| og:image:alt | Integration sentry-spring-boot-starter-jakarta Java Version 21 Version 8.29.0 Steps to Reproduce Summary We observed a memory leak when using Sentry Java SDK with Reactor Kafka. The leak occurs eve... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | harrypennont |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85dd75377eb77313206b8fc108bcee6a3a029286d94bf32bf9b4426e4d92e0c4 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-java git https://github.com/getsentry/sentry-java.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3368190 |
| octolytics-dimension-repository_nwo | getsentry/sentry-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3368190 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0d64fa748a2bab9a5b587a967ac4da1cfa8da6dd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width