Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython · GitHub
Open Graph Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython
X Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython
Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 checks sys.modules, finds module X, takes...
Open Graph Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c...
X Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c...
Opengraph URL: https://github.com/python/cpython/issues/143650
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"importlib race condition allows stale module ref to escape on failure","articleBody":"When a module import fails, other threads can receive a stale module reference that is no longer in `sys.modules`.\n\n## Race condition\n\n1. Thread 1 starts importing module X, adds it to `sys.modules`\n2. Thread 2 checks `sys.modules`, finds module X, takes the \"skip lock\" fast path\n3. Thread 1's import fails, removes module X from `sys.modules`\n4. Thread 2 returns the stale module reference (not in `sys.modules`)\n\n## Affected code paths\n\n- `Lib/importlib/_bootstrap.py:_find_and_load()` lines 1268-1282\n- `Python/import.c:PyImport_GetModule()` \n- `Python/import.c:PyImport_ImportModuleLevelObject()`\n\nAll three have an optimization that skips locking when a module appears already loaded, but none verify the module is still valid after checking `_initializing`.\n\n## Reproducer\n\n```python\nimport sys\nimport threading\nimport tempfile\nimport os\n\nwith tempfile.TemporaryDirectory() as tmpdir:\n sys.path.insert(0, tmpdir)\n \n # Module that partially initializes then fails\n with open(os.path.join(tmpdir, \"failing_mod.py\"), \"w\") as f:\n f.write(\"import time; time.sleep(0.05); raise RuntimeError('fail')\")\n \n results = []\n def do_import(delay):\n import time; time.sleep(delay)\n try:\n mod = __import__(\"failing_mod\")\n if \"failing_mod\" not in sys.modules:\n results.append(\"RACE: got module not in sys.modules\")\n except RuntimeError:\n results.append(\"ok\")\n \n t1 = threading.Thread(target=do_import, args=(0,))\n t2 = threading.Thread(target=do_import, args=(0.01,))\n t1.start(); t2.start(); t1.join(); t2.join()\n print(results) # May show \"RACE\" on affected builds\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-143651\n* gh-144662\n* gh-144697\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-01-10T10:57:32Z","url":"https://github.com/python/cpython/issues/143650"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:a2e7e6b6-4fcf-7061-a106-b6ff0e51c194 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA2E:31D44E:5E9F8A:8105F2:6AED2541 |
| html-safe-nonce | ec8987746778b17b15dd1c8f265517a11d9ab602f8750cd3de06aea8cc1d0df9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTJFOjMxRDQ0RTo1RTlGOEE6ODEwNUYyOjZBRUQyNTQxIiwidmlzaXRvcl9pZCI6IjMwNzE1NjA0NzA3MTIxMDAxNjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 44c3e97a481c25f5b228261717c4363475915a42c494b57e2128562b104c0d32 |
| hovercard-subject-tag | issue:3799517091 |
| 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/143650/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2a76e4639f1f8b8ba203836a4d5379f79050483989badfae7a8e7b673ed19406/python/cpython/issues/143650 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2a76e4639f1f8b8ba203836a4d5379f79050483989badfae7a8e7b673ed19406/python/cpython/issues/143650 |
| og:image:alt | When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9d95365dda0576efe9b71f45807595e396bd0ca9286fba5367a05048e6ddf8f2 |
| 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 | 98b41fa4b5635ceb45a4c54320fced73817ff290 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width