René's URL Explorer Experiment


Title: fix(batcher): wait for in-flight batches in flush() by tony1233200 · Pull Request #8340 · getsentry/sentry-python · GitHub

Open Graph Title: fix(batcher): wait for in-flight batches in flush() by tony1233200 · Pull Request #8340 · getsentry/sentry-python

X Title: fix(batcher): wait for in-flight batches in flush() by tony1233200 · Pull Request #8340 · getsentry/sentry-python

Description: Fixes #8300 Problem Client.flush() can return before a batch reaches the transport. The flusher thread removes the batch from the buffer under _lock and only then calls _capture_func, after releasing the lock. A flush() from another thread in that window finds an empty buffer and returns at once, while the batch is still on its way to the transport. Reproduction (from the issue, span streaming with a transport that sleeps 0.5s in the handoff): after flush(): [] # before: span arrives later after flush(): ['span'] # after The same window applies to logs and metrics (Batcher._flush) and to streamed spans (SpanBatcher._flush). It also affects shutdown: close() flushes and then sets transport = None, so a batch still being handed off can find no transport and be dropped silently. Result Batcher now tracks in-flight batches: _flush marks a batch in-flight atomically with taking it from the buffer (under _lock), and clears the mark once _capture_func returns or raises. flush() drains the buffer, then waits for the batches that were in flight when it started, and drains once more for anything that arrived while waiting. Batches taken after the flush started are new work and are not covered by that flush. The wait is bounded to the in-flight batches present at entry: each completing flush advances a counter, so a continuously producing app cannot keep a flush() waiting forever. A re-entrant flush() from inside batcher code (for example a transport calling flush() from within _capture_func) skips the wait, using the existing _active thread-local guard, so it cannot deadlock on its own handoff. SpanBatcher gets the same treatment: it has its own _flush and __init__, so the counters live in both classes. LogBatcher and MetricsBatcher use the base Batcher._flush and are covered by the base-class change. close() benefits without a code change: it flushes before killing the batchers and clearing the transport, so the in-flight batch is now delivered first. Compatibility No public API change. flush() may now take longer when a slow transport is mid-handoff — that is the documented contract ("Wait for the current events to be sent"). No behavior change when nothing is in flight: the snapshot shows zero in-flight batches and flush() returns without waiting. Validation Issue reproduction run before and after (span streaming, SlowTransport sleeping 0.5s in the handoff): before, after flush(): []; after, after flush(): ['span']. New regression test test_flush_waits_for_in_flight_span_batch in tests/test_client.py: fails on the base code, passes with the fix. It builds the client directly instead of using the sentry_init fixture, because that fixture installs _install_flush_completion_handshake, which would mask the race. Test runs (local venv, Python 3.12): pytest tests/test_client.py::test_flush_waits_for_in_flight_span_batch — 1 passed. pytest tests/test_client.py — 69 failed, 149 passed, 1 skipped. The 69 failures are identical to the base code (same test names, verified by diffing the failure lists): async-transport tests failing because aiohttp is not installed in this environment, so Client falls back to HttpTransport. No new failures. pytest tests/test_logs.py tests/test_metrics.py — 58 passed. ruff check and ruff format --check on the changed files: clean. mypy sentry_sdk/_batcher.py sentry_sdk/_span_batcher.py: 20 errors, all in untouched files (transport.py, utils.py, missing optional-dependency stubs in this environment); 0 errors in the changed files, and the same 20 errors on the base code. Not run: the full test suite and the Docker-based integration tests (no Docker in this environment); CI covers those. Note: open PR #7776 also touches Batcher._flush (moves serialization outside the lock). It does not address this race; the two changes touch the same function and may need a rebase if #7776 lands first.

Open Graph Description: Fixes #8300 Problem Client.flush() can return before a batch reaches the transport. The flusher thread removes the batch from the buffer under _lock and only then calls _capture_func, after releasi...

X Description: Fixes #8300 Problem Client.flush() can return before a batch reaches the transport. The flusher thread removes the batch from the buffer under _lock and only then calls _capture_func, after releasi...

Opengraph URL: https://github.com/getsentry/sentry-python/pull/8340

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:b30f7a00-de3f-9650-25af-5ac569fda5ac
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id89C8:16EED3:AB833F:DB2FD1:6B00F671
html-safe-nonceb71648982a895d67ec3c5472de39c5ed26aba2bbdd2539eb2fc34fed0a25abb7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OUM4OjE2RUVEMzpBQjgzM0Y6REIyRkQxOjZCMDBGNjcxIiwidmlzaXRvcl9pZCI6IjUyNDkxOTU2MzUyMzk4NzYyMDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac79d49fff0affedc31ef4a197cc4a46970e1db2f45b484b3db9145831c79fbfc9
hovercard-subject-tagpull_request:5413136235
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/getsentry/sentry-python/pull/8340/files
twitter:imagehttps://avatars.githubusercontent.com/u/127579663?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/127579663?s=400&v=4
og:image:altFixes #8300 Problem Client.flush() can return before a batch reaches the transport. The flusher thread removes the batch from the buffer under _lock and only then calls _capture_func, after releasi...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git
octolytics-dimension-user_id1396951
octolytics-dimension-user_logingetsentry
octolytics-dimension-repository_id137814779
octolytics-dimension-repository_nwogetsentry/sentry-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id137814779
octolytics-dimension-repository_network_root_nwogetsentry/sentry-python
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release9983dd5da06943f833c971dd5b6bc988e2858be5
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/getsentry/sentry-python/pull/8340/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fpull%2F8340%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fpull%2F8340%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=getsentry%2Fsentry-python
Reloadhttps://github.com/getsentry/sentry-python/pull/8340/files
Reloadhttps://github.com/getsentry/sentry-python/pull/8340/files
Reloadhttps://github.com/getsentry/sentry-python/pull/8340/files
getsentryhttps://github.com/getsentry
sentry-pythonhttps://github.com/getsentry/sentry-python
Codehttps://github.com/getsentry/sentry-python
Issues345 (345)https://github.com/getsentry/sentry-python/issues
Pull requests42 (42)https://github.com/getsentry/sentry-python/pulls
Discussionshttps://github.com/getsentry/sentry-python/discussions
Actionshttps://github.com/getsentry/sentry-python/actions
Security and quality2 (2)https://github.com/getsentry/sentry-python/security
Insightshttps://github.com/getsentry/sentry-python/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fgetsentry%2Fsentry-python%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fgetsentry%2Fsentry-python%2Fissues%2Fnew%2Fchoose
tony1233200https://github.com/tony1233200
getsentry:masterhttps://github.com/getsentry/sentry-python/tree/master
tony1233200:fix/8300-flush-in-flighthttps://github.com/tony1233200/sentry-python/tree/fix/8300-flush-in-flight
Conversation 0 https://github.com/getsentry/sentry-python/pull/8340
Commits 1 https://github.com/getsentry/sentry-python/pull/8340/commits
Checks 3 https://github.com/getsentry/sentry-python/pull/8340/checks
Files changed https://github.com/getsentry/sentry-python/pull/8340/files
Please reload this pagehttps://github.com/getsentry/sentry-python/pull/8340/files
fix(batcher): wait for in-flight batches in flush() https://github.com/getsentry/sentry-python/pull/8340/files#top
Show all changes 1 commit https://github.com/getsentry/sentry-python/pull/8340/files
950d62b fix(batcher): wait for in-flight batches in flush() tony1233200 Nov 20, 2026 https://github.com/getsentry/sentry-python/pull/8340/commits/950d62be91f1195c667cdd0a517c94df53e218d1
Clear filters https://github.com/getsentry/sentry-python/pull/8340/files
Please reload this pagehttps://github.com/getsentry/sentry-python/pull/8340/files
Please reload this pagehttps://github.com/getsentry/sentry-python/pull/8340/files
_batcher.py https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
_span_batcher.py https://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
test_client.py https://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
https://github.com/getsentry/sentry-python/blob/master/.github/CODEOWNERS#L1
sentry_sdk/_batcher.pyhttps://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
View file https://github.com/getsentry/sentry-python/blob/950d62be91f1195c667cdd0a517c94df53e218d1/sentry_sdk/_batcher.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/getsentry/sentry-python/pull/8340/{{ revealButtonHref }}
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/pull/8340/files#diff-7dbecac92865baa900639d0a3286da7b5efc6403923ebc49ab7ed8815fc1fe03
https://github.com/getsentry/sentry-python/blob/master/.github/CODEOWNERS#L1
sentry_sdk/_span_batcher.pyhttps://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
View file https://github.com/getsentry/sentry-python/blob/950d62be91f1195c667cdd0a517c94df53e218d1/sentry_sdk/_span_batcher.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/getsentry/sentry-python/pull/8340/{{ revealButtonHref }}
https://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
https://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
https://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
https://github.com/getsentry/sentry-python/pull/8340/files#diff-f400f2b5275be953a87e1a2fa25659221ab46d1f696f7d1104694c55ea621189
https://github.com/getsentry/sentry-python/blob/master/.github/CODEOWNERS#L1
tests/test_client.pyhttps://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
View file https://github.com/getsentry/sentry-python/blob/950d62be91f1195c667cdd0a517c94df53e218d1/tests/test_client.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/getsentry/sentry-python/pull/8340/{{ revealButtonHref }}
https://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
https://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
https://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
https://github.com/getsentry/sentry-python/pull/8340/files#diff-0d92063e88430a02df61616c5f16b148b64ac4539d9cb9b8d883d5a23351b110
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.