Title: importlib: later import hangs forever after a RecursionError in a small-stack thread leaves a module lock held · Issue #159652 · python/cpython · GitHub
Open Graph Title: importlib: later import hangs forever after a RecursionError in a small-stack thread leaves a module lock held · Issue #159652 · python/cpython
X Title: importlib: later import hangs forever after a RecursionError in a small-stack thread leaves a module lock held · Issue #159652 · python/cpython
Description: If an import in a thread with a small stack fails with RecursionError and the exception is kept alive, the module lock it held is never released, and a later import of the same module in another thread blocks forever in _ModuleLock.acqui...
Open Graph Description: If an import in a thread with a small stack fails with RecursionError and the exception is kept alive, the module lock it held is never released, and a later import of the same module in another th...
X Description: If an import in a thread with a small stack fails with RecursionError and the exception is kept alive, the module lock it held is never released, and a later import of the same module in another th...
Opengraph URL: https://github.com/python/cpython/issues/159652
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"importlib: later import hangs forever after a RecursionError in a small-stack thread leaves a module lock held","articleBody":"If an import in a thread with a small stack fails with `RecursionError` and the exception is kept alive, the module lock it held is never released, and a later import of the same module in another thread blocks forever in `_ModuleLock.acquire`.\n\n```python\nimport threading\nthreading.stack_size(131072)\nerrors = []\ndef worker():\n try:\n import _colorize\n except RecursionError as e:\n errors.append(e)\nt = threading.Thread(target=worker)\nt.start(); t.join()\nprint(errors, flush=True)\nimport collections.abc\nprint(\"done\")\n```\n\n```\ntimeout 20 python reduced.py\n```\n\nRelease build:\n\n```\n[RecursionError('Stack overflow (used 99 kB)')]\n```\n\nIt then hangs and is killed by `timeout` (exit 124); `done` is never printed. In a variant that ends with an uncaught exception instead, so that the import that hangs is `import collections.abc` at line 3 of `Lib/traceback.py`, a faulthandler dump at 3646ea571f3d shows the main thread in `_ModuleLock.acquire` (`\u003cfrozen importlib._bootstrap>` line 359), called from `__enter__` (line 452) and `_find_and_load`.\n\nI expected `import collections.abc` to succeed, since the failed import should have released its locks. Without `errors.append(e)` the lock objects are garbage collected and nothing hangs.\n\nFound at commit 3646ea571f3d8290806a3029d555b179f8be1539; still reproduces at e5ef47e94f7c9d970ad98e2c33870ccc3b04dc5c (the release build hangs the same way). The checking build (assertions and ASAN) does not hang here: at this stack size it aborts first with ``Parser/pegen.h:120: _PyPegen_stack_exhausted: Assertion `!_Py_ReachedRecursionLimitWithMargin(p->tstate, 1)' failed`` (rc 134). The stack size that triggers the hang depends on the build; on the release build I saw it for sizes from about 124928 to 188416.\n\nThis looks like `_HierarchicalLockManager.__enter__` in `Lib/importlib/_bootstrap.py`: a lock taken by `lock.acquire()` is only appended to `self._locks` afterwards, so a `RecursionError` in that window (or in the cleanup `release()` calls) leaves it owned by the dead thread. I checked the leak itself (after the thread exits, `importlib._bootstrap._module_locks` still holds the `collections.abc` lock, owned by the dead thread); the exact line is a guess. PRs #153575 and #157413 change parser recursion depth and may be related; I did not test them, as their diffs do not apply on the latest commit.\n\nI can send a patch if that helps.\n\nFound with an LLM-based testing tool; I used Claude to reduce it and look for the cause.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159660\n* gh-160779\n* gh-160780\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/waroad","@type":"Person","name":"waroad"},"datePublished":"2026-10-17T20:37:21Z","url":"https://github.com/python/cpython/issues/159652"}
| 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:d06ced0f-1daf-fee4-0c1d-e8bed330d295 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DCFE:29AEE8:364EA07:484DE43:6B020856 |
| html-safe-nonce | 7c792ca3d85bcb825819597767fb1e9e8960c16062d5c248239ece31bec69ccc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQ0ZFOjI5QUVFODozNjRFQTA3OjQ4NERFNDM6NkIwMjA4NTYiLCJ2aXNpdG9yX2lkIjoiNDkxODQxOTY2MDk0NjQwOTU1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 983fb1349cb8581c792f7033077cd7cc56c9b57eff4e6685611de8a6df1d1266 |
| hovercard-subject-tag | issue:5902845821 |
| 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/python/cpython/159652/issue_layout |
| twitter:image | https://opengraph.githubassets.com/58051e7e8d91694f289aa40025a05a6859c7b1ceae34c85b6a2f200a664b7677/python/cpython/issues/159652 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/58051e7e8d91694f289aa40025a05a6859c7b1ceae34c85b6a2f200a664b7677/python/cpython/issues/159652 |
| og:image:alt | If an import in a thread with a small stack fails with RecursionError and the exception is kept alive, the module lock it held is never released, and a later import of the same module in another th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | waroad |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width