Title: Segfault when a finalizer declares or resolves a lazy import during shutdown · Issue #158820 · python/cpython · GitHub
Open Graph Title: Segfault when a finalizer declares or resolves a lazy import during shutdown · Issue #158820 · python/cpython
X Title: Segfault when a finalizer declares or resolves a lazy import during shutdown · Issue #158820 · python/cpython
Description: Crash report Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault. This is a corner case where both of the following must exist: a lazy object whose __import__ still works at shutdown; a holder tha...
Open Graph Description: Crash report Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault. This is a corner case where both of the following must exist: a lazy object whose __import...
X Description: Crash report Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault. This is a corner case where both of the following must exist: a lazy object whose __import...
Opengraph URL: https://github.com/python/cpython/issues/158820
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault when a finalizer declares or resolves a lazy import during shutdown","articleBody":"### Crash report\n\n**Bug:** If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault.\n\nThis is a corner case where both of the following must exist:\n- a lazy object whose `__import__` still works at shutdown;\n- a holder that outlives the lazy-import state, such as a `ContextVar`, the codec registry, fork callbacks, audit hooks or warnings filters.\n\n**Why:** Shutdown frees `lazy_modules` and `lazy_pending_submodules` and sets them to NULL, but finalizers can still run after they have been freed and we do not have the necessary NULL checks in place to handle reachable functions accessing either nulled object.\n\n**Minimal repro:**\n```python\nimport contextvars, json, types\n\ndef safe_import(name, *args, allowed={\"json\": json}):\n return allowed[name]\n\nns = types.ModuleType(\"ns\")\nns.json = __lazy_import__(\"json\", {\"__builtins__\": {\"__import__\": safe_import}})\n\nclass Handle:\n def __del__(self, ns=ns):\n ns.json\n\ncontextvars.ContextVar(\"v\").set(Handle())\n```\n```bash\n$ ./python repro.py\nSegmentation fault (core dumped)\n\n#0 PySet_Discard (set=0x0, ...) at Objects/setobject.c\n#1 _PyImport_DiscardLazyModule (...) at Python/import.c\n#2 lazy_import_resolve_impl (...) at Objects/lazyimportobject.c\n#3 _PyLazyImport_Reify (...)\n#4 _Py_module_getattro_impl (...)\n```\n\nNOTE: PR is in progress\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158821\n* gh-159062\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/brittanyrey","@type":"Person","name":"brittanyrey"},"datePublished":"2026-10-05T01:52:01Z","url":"https://github.com/python/cpython/issues/158820"}
| 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:451429e5-3c85-c9ad-429e-2b486d27bfca |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8644:6B7DC:1B5EB6:22F76A:6B01F258 |
| html-safe-nonce | 64a6ce0e946c7d63c5f08bafccc3770934109e34368fee22f425d59d900ef2cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjQ0OjZCN0RDOjFCNUVCNjoyMkY3NkE6NkIwMUYyNTgiLCJ2aXNpdG9yX2lkIjoiMTkwMjE4ODM0ODAwOTczODg0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bb2edbe8dee9dbac52d30102a033492e88373a710d483e33bdb970037a522156 |
| hovercard-subject-tag | issue:5705301054 |
| 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/158820/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8c6df16dd1c192e5c02e5c2b7ad55cc23549ac9ad9f386dae0fca7430f981318/python/cpython/issues/158820 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8c6df16dd1c192e5c02e5c2b7ad55cc23549ac9ad9f386dae0fca7430f981318/python/cpython/issues/158820 |
| og:image:alt | Crash report Bug: If a finalizer declares or resolves a lazy import during shutdown, CPython can segfault. This is a corner case where both of the following must exist: a lazy object whose __import... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | brittanyrey |
| 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