Title: Deadlock between log batcher lock and logging handler lock when GC runs a finalizer that logs · Issue #7775 · getsentry/sentry-python · GitHub
Open Graph Title: Deadlock between log batcher lock and logging handler lock when GC runs a finalizer that logs · Issue #7775 · getsentry/sentry-python
X Title: Deadlock between log batcher lock and logging handler lock when GC runs a finalizer that logs · Issue #7775 · getsentry/sentry-python
Description: Environment: sentry-sdk 2.66.1 and 2.71.0, Python 3.14, enable_logs=True with the default LoggingIntegration. Batcher._flush holds self._lock while _add_to_envelope serializes the buffered items. If a cyclic GC runs during that serializa...
Open Graph Description: Environment: sentry-sdk 2.66.1 and 2.71.0, Python 3.14, enable_logs=True with the default LoggingIntegration. Batcher._flush holds self._lock while _add_to_envelope serializes the buffered items. I...
X Description: Environment: sentry-sdk 2.66.1 and 2.71.0, Python 3.14, enable_logs=True with the default LoggingIntegration. Batcher._flush holds self._lock while _add_to_envelope serializes the buffered items. I...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/7775
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deadlock between log batcher lock and logging handler lock when GC runs a finalizer that logs","articleBody":"**Environment:** sentry-sdk 2.66.1 and 2.71.0, Python 3.14, `enable_logs=True` with the default `LoggingIntegration`.\n\n`Batcher._flush` holds `self._lock` while `_add_to_envelope` serializes the buffered items. If a cyclic GC runs during that serialization, and it collects an object whose `__del__` calls `logging`, the flush thread goes through `Handler.handle` and blocks acquiring the `SentryLogsHandler` handler lock. `add()`'s thread-local `_active` guard (#5684) doesn't help here, because the handler lock is taken before `add()` is ever reached. If another thread is logging at the same moment, it already holds that handler lock inside `emit` and waits for `Batcher._lock` in `add()`. The result is a lock-order inversion: every later log call in the process blocks, forever. We hit this in production: Celery tasks froze until they were hard-killed.\n\n**Minimal repro** (deadlocks in ~5s, 7/7 runs across both versions):\n\n```python\nimport gc, logging, sys, threading, time, traceback\nimport sentry_sdk\nfrom sentry_sdk.transport import Transport\n\nclass DropTransport(Transport):\n def capture_envelope(self, envelope):\n pass\n\nsentry_sdk.init(dsn=\"https://k@o0.ingest.sentry.io/0\", transport=DropTransport, enable_logs=True)\nlogger = logging.getLogger(\"repro\")\nlogger.setLevel(logging.INFO)\n\nclass Leaky:\n def __init__(self):\n self.cycle = self\n def __del__(self):\n logger.warning(\"finalizer logging\")\n\nprogress = [0]\ndef log_forever():\n while True:\n logger.info(\"hello\")\n progress[0] += 1\n\ndef make_garbage():\n while True:\n Leaky()\n time.sleep(0.0005)\n\ngc.set_threshold(200, 2, 2)\nfor fn in (log_forever, log_forever, log_forever, make_garbage):\n threading.Thread(target=fn, daemon=True).start()\n\nlast = -1\nwhile True:\n time.sleep(5)\n if progress[0] == last:\n print(\"DEADLOCK\")\n for tid, frame in sys._current_frames().items():\n traceback.print_stack(frame)\n break\n last = progress[0]\n```\n\nThe stacks show `_flush_loop → _flush → _add_to_envelope → _to_transport_format → __del__ → logger.warning → Handler.handle` blocked on the handler lock, and a logging thread at `Handler.handle → emit → Batcher.add` blocked on `_lock`.\n\n**Suggested fix:** in `Batcher._flush`, swap the buffer out under the lock, then serialize outside it:\n\n```python\nwith self._lock:\n if not self._buffer:\n return None\n items, self._buffer = self._buffer, []\n# build the envelope from `items` here, outside the lock\n```\n\nWith that patch applied, the repro above ran 0 deadlocks in 3×60s. Happy to open a PR.\n","author":{"url":"https://github.com/NicoHinderling","@type":"Person","name":"NicoHinderling"},"datePublished":"2026-09-29T22:00:49Z","url":"https://github.com/getsentry/sentry-python/issues/7775"}
| 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:c4641525-c51c-49b3-90ff-b11f5c7bc284 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8014:2AAD2E:85D621:B4A45F:6B00E35A |
| html-safe-nonce | d0f1e83b117830223c1040dfb5d7768ecb92e8f9b48f37d0176b7dfbbc1ded14 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDE0OjJBQUQyRTo4NUQ2MjE6QjRBNDVGOjZCMDBFMzVBIiwidmlzaXRvcl9pZCI6IjQwNjc3NDc1NTA1NTg0NzkxOTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e6280d185d41e30c8458ef869696e8e611089aaefefa106d2bfe5621f6284777 |
| hovercard-subject-tag | issue:5637663586 |
| 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/7775/issue_layout |
| twitter:image | https://opengraph.githubassets.com/33e1569226832eb35f7c9089e75aeb98470df83b4207afcf927c6f0e940c847e/getsentry/sentry-python/issues/7775 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/33e1569226832eb35f7c9089e75aeb98470df83b4207afcf927c6f0e940c847e/getsentry/sentry-python/issues/7775 |
| og:image:alt | Environment: sentry-sdk 2.66.1 and 2.71.0, Python 3.14, enable_logs=True with the default LoggingIntegration. Batcher._flush holds self._lock while _add_to_envelope serializes the buffered items. I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | NicoHinderling |
| 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