Title: SpanBatcher serializes spans while holding its buffer lock · Issue #8023 · getsentry/sentry-python · GitHub
Open Graph Title: SpanBatcher serializes spans while holding its buffer lock · Issue #8023 · getsentry/sentry-python
X Title: SpanBatcher serializes spans while holding its buffer lock · Issue #8023 · getsentry/sentry-python
Description: Problem SpanBatcher._flush() holds _lock while calling _dynamic_sampling_context() and serializing spans with _to_transport_format(). This blocks concurrent additions for the entire serialization step and leaves a possible re-entrant loc...
Open Graph Description: Problem SpanBatcher._flush() holds _lock while calling _dynamic_sampling_context() and serializing spans with _to_transport_format(). This blocks concurrent additions for the entire serialization s...
X Description: Problem SpanBatcher._flush() holds _lock while calling _dynamic_sampling_context() and serializing spans with _to_transport_format(). This blocks concurrent additions for the entire serialization s...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/8023
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SpanBatcher serializes spans while holding its buffer lock","articleBody":"### Problem\n\n`SpanBatcher._flush()` holds `_lock` while calling `_dynamic_sampling_context()` and serializing spans with `_to_transport_format()`. This blocks concurrent additions for the entire serialization step and leaves a possible re-entrant lock-order hazard. The related fix in #7776 explicitly excludes `SpanBatcher`.\n\n### Reproduction\n\nPatch `_to_transport_format` to pause on a `threading.Event`, then try `batcher._lock.acquire(blocking=False)` from another thread. It fails while serialization is in progress on `master`.\n\n### Proposed fix\n\nDetach selected per-trace buffers under the lock, serialize outside the lock, and restore detached spans ahead of newly arrived spans if serialization fails. Keep the existing per-trace envelope boundaries.\n\nA small candidate patch and three regression tests are on [this branch](https://github.com/mhatipog/sentry-python/tree/fix/span-batcher-flush-outside-lock). Local focused tests against SDK 2.61.0: **3 failed on baseline, 3 passed with the patch**. Full upstream CI has not been run.\n\nWould this approach be acceptable? I'll hold the draft PR until a maintainer has weighed in, in line with CONTRIBUTING.md.","author":{"url":"https://github.com/mhatipog","@type":"Person","name":"mhatipog"},"datePublished":"2026-10-13T23:38:28Z","url":"https://github.com/getsentry/sentry-python/issues/8023"}
| 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:d2013d9a-7fb0-ed41-d48b-4ab7a7efbcfc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E36:134361:BC2C99:F90A37:6B00F097 |
| html-safe-nonce | 79c941797a8eb6c9158bc3ba2180ab3470390bc64e4fc24cd58f5201d4327c4c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTM2OjEzNDM2MTpCQzJDOTk6RjkwQTM3OjZCMDBGMDk3IiwidmlzaXRvcl9pZCI6IjEyNTE5OTMxMTc1ODY5NDQxNTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7868c129c872513c1bdd639df63b22b96e6cbfa9c79bbef8c7e018c6f1106054 |
| hovercard-subject-tag | issue:5839379060 |
| 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-python/8023/issue_layout |
| twitter:image | https://opengraph.githubassets.com/da6cc28d6043342d367470dc266c5ca3e4dabf5f46a818a38a15e6ad224aad5e/getsentry/sentry-python/issues/8023 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/da6cc28d6043342d367470dc266c5ca3e4dabf5f46a818a38a15e6ad224aad5e/getsentry/sentry-python/issues/8023 |
| og:image:alt | Problem SpanBatcher._flush() holds _lock while calling _dynamic_sampling_context() and serializing spans with _to_transport_format(). This blocks concurrent additions for the entire serialization s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mhatipog |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 137814779 |
| octolytics-dimension-repository_nwo | getsentry/sentry-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 137814779 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-python |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width