Title: Possible deadlock when using Batcher and `os.fork()` · Issue #6149 · getsentry/sentry-python · GitHub
Open Graph Title: Possible deadlock when using Batcher and `os.fork()` · Issue #6149 · getsentry/sentry-python
X Title: Possible deadlock when using Batcher and `os.fork()` · Issue #6149 · getsentry/sentry-python
Description: Problem Statement Calling sentry_sdk.init can possibly start background threads, such as the batcher for logs/metrics/spans. The batcher implementation uses a lock. If the lock is acquired by one of the background threads at the moment w...
Open Graph Description: Problem Statement Calling sentry_sdk.init can possibly start background threads, such as the batcher for logs/metrics/spans. The batcher implementation uses a lock. If the lock is acquired by one o...
X Description: Problem Statement Calling sentry_sdk.init can possibly start background threads, such as the batcher for logs/metrics/spans. The batcher implementation uses a lock. If the lock is acquired by one o...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/6149
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Possible deadlock when using Batcher and `os.fork()`","articleBody":"### Problem Statement\n\nCalling `sentry_sdk.init` can possibly start background threads, such as the batcher for logs/metrics/spans.\nThe batcher implementation uses a lock. \nIf the lock is acquired by one of the background threads at the moment when `os.fork()` is called, a deadlock will happen in the child process, because the thread that acquired the lock doesn't exist anymore.\n\nHere's a reproduction using celery.\n\n```python\n\"\"\"Run with celery -A repro worker\"\"\"\n\nimport sentry_sdk\nfrom sentry_sdk import logger\nfrom sentry_sdk.transport import Transport\nfrom celery import Celery\nfrom celery.signals import worker_init\n\n\nclass NoopTransport(Transport):\n def capture_envelope(self, envelope):\n pass\n\n\nsentry_sdk.init(\n dsn=\"https://public@example.invalid/1\",\n transport=NoopTransport,\n enable_logs=True,\n default_integrations=False, // the celery integration redirects some logs which would mess this up iirc\n)\n\nheavy_attrs = {f\"k{i}\": \"v\" for i in range(200_000)}\n\napp = Celery(\"demo\", broker=\"memory://\")\n\n\n@app.task\ndef task():\n print(\"task start\", flush=True)\n logger.info(\"inside child\")\n print(\"task end\", flush=True)\n\n\n@worker_init.connect\ndef connect(**_):\n for _ in range(100):\n logger.info(\"inside parent\", **heavy_attrs)\n // the batcher thread is now serializing the logs envelope, which takes a lot of time due to heavy attrs, so it's holding the lock at this point\n task.apply_async()\n```\n\nThis uses celery and the sdk's logger for the sake of the example, but this can happen in more general cases.\n\n### Solution Brainstorm\n\n1. Instruct people to call `init` only after forking (might be tricky if the library is the one that forks. However only celery is the only practical case I know of that still uses fork, so it might be fine to specify this only for celery)\n2. Use `os.register_at_fork` to wait for the lock to be released before forking (I think this is sufficient)","author":{"url":"https://github.com/lcian","@type":"Person","name":"lcian"},"datePublished":"2026-04-27T11:00:58Z","url":"https://github.com/getsentry/sentry-python/issues/6149"}
| 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:06b0df98-debd-e811-be0b-cf5d8993bbc2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBEA:85EA5:4B8F5F:65EE12:6B008F94 |
| html-safe-nonce | 359fe8f1f8fea66b1db8c904e57f0a7ceb9879e87fc19a81595cfbe73eeb6738 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkVBOjg1RUE1OjRCOEY1Rjo2NUVFMTI6NkIwMDhGOTQiLCJ2aXNpdG9yX2lkIjoiODE0MTY2MDMzNTg5NjU2MzYwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c3f1e266b835f3ab4ddfd2b94c00fb989ed6f0ab0b43249d364ab926a72a1888 |
| hovercard-subject-tag | issue:4335187450 |
| 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/6149/issue_layout |
| twitter:image | https://opengraph.githubassets.com/725e694b3090c9a9dde0d2acd2f024d6d6e588aa314af83a74341eace66ba66c/getsentry/sentry-python/issues/6149 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/725e694b3090c9a9dde0d2acd2f024d6d6e588aa314af83a74341eace66ba66c/getsentry/sentry-python/issues/6149 |
| og:image:alt | Problem Statement Calling sentry_sdk.init can possibly start background threads, such as the batcher for logs/metrics/spans. The batcher implementation uses a lock. If the lock is acquired by one o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lcian |
| hostname | github.com |
| expected-hostname | github.com |
| None | c710a5cb5c47be60abfe6e8410436d409906979dbed614ef09f29c60f5535b08 |
| 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 | be616a2a6ec1c609d6e002896dff5352d5c5f28b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width