Title: `flush()` can return before a batch taken by the flusher thread reaches the transport · Issue #8300 · getsentry/sentry-python · GitHub
Open Graph Title: `flush()` can return before a batch taken by the flusher thread reaches the transport · Issue #8300 · getsentry/sentry-python
X Title: `flush()` can return before a batch taken by the flusher thread reaches the transport · Issue #8300 · getsentry/sentry-python
Description: How do you use Sentry? Self-hosted/on-premise Version 2.75.0 (Python 3.15.2, only sentry-sdk installed); also reproduces on 2.72.0, and master at e9c62e5 has the same code Steps to Reproduce Client.flush() can return before a batch is de...
Open Graph Description: How do you use Sentry? Self-hosted/on-premise Version 2.75.0 (Python 3.15.2, only sentry-sdk installed); also reproduces on 2.72.0, and master at e9c62e5 has the same code Steps to Reproduce Client...
X Description: How do you use Sentry? Self-hosted/on-premise Version 2.75.0 (Python 3.15.2, only sentry-sdk installed); also reproduces on 2.72.0, and master at e9c62e5 has the same code Steps to Reproduce Client...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/8300
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`flush()` can return before a batch taken by the flusher thread reaches the transport","articleBody":"### How do you use Sentry?\n\nSelf-hosted/on-premise\n\n### Version\n\n2.75.0 (Python 3.15.2, only `sentry-sdk` installed); also reproduces on 2.72.0, and `master` at e9c62e53 has the same code\n\n### Steps to Reproduce\n\n`Client.flush()` can return before a batch is delivered to the transport. The batcher's flusher thread removes the batch from the buffer under `_lock` and calls `_capture_func` only after releasing it (`Batcher._flush`, `SpanBatcher._flush`). A `flush()` from another thread that runs in this window finds the buffer empty and returns at once, while the batch is still on its way to the transport.\n\nThe same window applies to logs and metrics (`Batcher._flush`) and to streamed spans (`SpanBatcher._flush`). For spans it opens right after a segment ends, since the segment end wakes the flusher thread immediately.\n\nThe transport below only widens the window so the race reproduces every time:\n\n```python\nimport threading\nimport time\n\nimport sentry_sdk\nfrom sentry_sdk.transport import Transport\n\nreceived = []\ntaken = threading.Event()\n\n\nclass SlowTransport(Transport):\n def capture_envelope(self, envelope):\n if threading.current_thread() is not threading.main_thread():\n taken.set()\n time.sleep(0.5) # widen the window: batch already removed from the buffer\n received.extend(item.type for item in envelope.items)\n\n\nsentry_sdk.init(\n dsn=\"https://public@example.invalid/1\",\n transport=SlowTransport,\n trace_lifecycle=\"stream\",\n traces_sample_rate=1.0,\n)\n\nwith sentry_sdk.traces.start_span(name=\"segment\"):\n pass\n\ntaken.wait() # the flusher thread has taken the batch and is delivering it\nsentry_sdk.flush()\nprint(\"after flush():\", received)\n```\n\n### Expected Result\n\n`after flush(): ['span']` — `flush()` is documented as \"Wait for the current events to be sent\".\n\n### Actual Result\n\n`after flush(): []`; the span arrives later.\n\nTwo consequences:\n- Tests that capture envelopes with an in-memory transport and assert after `flush()` are flaky.\n- `Client.close()` calls `flush()` and then sets `self.transport = None`. A batch in the window then reaches `_capture_envelope` with no transport and is dropped silently, so the last spans or logs of a shutting-down process can be lost.\n\nThe SDK's own test suite already works around this: `_install_flush_completion_handshake` in `tests/conftest.py`, added in #7170, makes `flush()` wait for the flusher thread, and #7170 describes the race. User code and user test suites have no such hook without patching private batcher internals.\n\nA possible fix: a separate flush lock held for the whole `_flush`, including the `_capture_func` call. `_lock` stays short, so it doesn't interfere with the deadlock fix in #5684, the open PR #7776, or the change proposed in #8023. A `flush()` from the user's thread then waits for an in-flight flush of the flusher thread before draining the rest. On its own this lock isn't enough, though: a reentrant `flush()` from a transport would deadlock on it (see the comments below).\n\n","author":{"url":"https://github.com/Flamefork","@type":"Person","name":"Flamefork"},"datePublished":"2026-11-16T14:40:36Z","url":"https://github.com/getsentry/sentry-python/issues/8300"}
| 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:cc2ec12a-5d53-ed0e-f912-2dbcb4890708 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E804:172B92:874A4E:B6B29B:6B00E2FF |
| html-safe-nonce | b3823f8c5d80b84c31c0f30ffa5a9163aa1ccd90ce8c65cbbf2ec4c03bde50b3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODA0OjE3MkI5Mjo4NzRBNEU6QjZCMjlCOjZCMDBFMkZGIiwidmlzaXRvcl9pZCI6IjgzNTY3NTg4MjY0MzA1NTQ4NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 807e5a07078fc90c8a98510a74b4110ce45ae1d6afa0b79879f3e8a2fc3633f6 |
| hovercard-subject-tag | issue:6423653777 |
| 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/8300/issue_layout |
| twitter:image | https://opengraph.githubassets.com/122c63a08ac8716ee5faa378c283063ec1e22df6448f6c426bd2876a06b5d45d/getsentry/sentry-python/issues/8300 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/122c63a08ac8716ee5faa378c283063ec1e22df6448f6c426bd2876a06b5d45d/getsentry/sentry-python/issues/8300 |
| og:image:alt | How do you use Sentry? Self-hosted/on-premise Version 2.75.0 (Python 3.15.2, only sentry-sdk installed); also reproduces on 2.72.0, and master at e9c62e5 has the same code Steps to Reproduce Client... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Flamefork |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width