Title: `os.fork()` child can hang inside `PyOS_AfterFork_Child()` on the warnings lock (3.14 regression) · Issue #158973 · python/cpython · GitHub
Open Graph Title: `os.fork()` child can hang inside `PyOS_AfterFork_Child()` on the warnings lock (3.14 regression) · Issue #158973 · python/cpython
X Title: `os.fork()` child can hang inside `PyOS_AfterFork_Child()` on the warnings lock (3.14 regression) · Issue #158973 · python/cpython
Description: Bug report Since 3.14, the warnings module protects its state with a lock in the interpreter, added in #128386 for #128384. PyOS_AfterFork_Child() does not reset this lock. If another thread holds it when a thread calls os.fork(), the ch...
Open Graph Description: Bug report Since 3.14, the warnings module protects its state with a lock in the interpreter, added in #128386 for #128384. PyOS_AfterFork_Child() does not reset this lock. If another thread holds ...
X Description: Bug report Since 3.14, the warnings module protects its state with a lock in the interpreter, added in #128386 for #128384. PyOS_AfterFork_Child() does not reset this lock. If another thread holds ...
Opengraph URL: https://github.com/python/cpython/issues/158973
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`os.fork()` child can hang inside `PyOS_AfterFork_Child()` on the warnings lock (3.14 regression)","articleBody":"### Bug report\n\nSince 3.14, the warnings module protects its state with a lock in the interpreter, added in #128386 for #128384. `PyOS_AfterFork_Child()` does not reset this lock. If another thread holds it when a thread calls `os.fork()`, the child inherits it locked, and the first warning in the child blocks forever. The child does not need to run any code for this to happen. `PyOS_AfterFork_Child()` clears the states of the other threads. Objects held only in those states, such as `threading.local()` data, are freed there, and a finalizer can issue a warning before `os.fork()` returns.\n\nIn the reproducer, one thread keeps an unclosed socket in `threading.local()` and issues warnings in a loop. The child only calls `os._exit(0)`, so a child that hangs is stuck inside `os.fork()`:\n\n```python\nimport os, socket, sys, threading, time, warnings\n\n# Every warning is shown, so warn() runs showwarning(), which is Python code,\n# while it holds the warnings lock.\nwarnings.simplefilter(\"always\")\nwarnings.showwarning = lambda *args, **kwargs: None\n\nlocal = threading.local()\n\ndef warn_forever():\n local.sock = socket.socket() # never closed: freeing it issues a ResourceWarning\n while True:\n warnings.warn(\"from another thread\")\n\nthreading.Thread(target=warn_forever, daemon=True).start()\ntime.sleep(0.5)\n\nhung = 0\nfor _ in range(20):\n pid = os.fork()\n if pid == 0:\n os._exit(0) # the child runs no other code\n time.sleep(0.5)\n if os.waitpid(pid, os.WNOHANG) == (0, 0):\n hung += 1\n os.kill(pid, 9)\n os.waitpid(pid, 0)\n\nprint(f\"{sys.version.split()[0]}: {hung} of 20 children hung\", flush=True)\nos._exit(0) # skip interpreter shutdown, which can hang too (see below)\n```\n\nResults on Linux x86_64 with glibc 2.43:\n\n| Python | Build | Children that hung, of 20 |\n|---|---|---|\n| 3.12.13 | built from source | 0 |\n| 3.13.16 | conda-forge | 0 |\n| 3.13.16, free-threaded | conda-forge | 0 |\n| 3.14.4 | Ubuntu package | 17 |\n| 3.14.8 | conda-forge | 20 |\n| 3.14.8, free-threaded | conda-forge | 18 |\n| 3.15.0rc3 | conda-forge | 15 |\n\ngdb on a hung child (3.14.4, Ubuntu build without debug symbols; frames without a name are cut) shows a finalizer that runs in `PyThreadState_Clear()` and waits for the lock in `PyErr_ResourceWarning()`:\n\n```\n#10 0x00000000005cbfc6 in _PyRecursiveMutex_Lock ()\n...\n#14 0x000000000047da7c in PyErr_ResourceWarning ()\n...\n#16 0x00000000005cd2d8 in PyObject_CallFinalizerFromDealloc ()\n...\n#23 0x00000000005d9bf6 in PyObject_ClearWeakRefs ()\n...\n#26 0x00000000006d0f22 in PyThreadState_Clear ()\n#27 0x00000000004850c2 in PyOS_AfterFork_Child ()\n```\n\nThe lock can be held while other threads run. `do_warn()` holds it across `warn_explicit()`, and `warn_explicit()` runs the Python function `showwarning()` that writes the message:\n\nhttps://github.com/python/cpython/blob/16108074b1496b1dd60dfdafc762d9e26d6f149c/Python/_warnings.c#L1136-L1139\n\n`catch_warnings.__enter__()` and `__exit__()`, `filterwarnings()` and `simplefilter()` also run Python code while they hold the lock. Every C warning call takes the lock before it checks the filters, so the `ResourceWarning` in the child blocks although the default filters ignore it.\n\nThe lock is not in `LOCKS_INIT`, the list of runtime locks that the child resets. On main, `PyOS_AfterFork_Child()` also resets `_Py_jit_debug_mutex`. It then clears the dead thread states, and the comment above that call notes that this may call destructors:\n\nhttps://github.com/python/cpython/blob/16108074b1496b1dd60dfdafc762d9e26d6f149c/Modules/posixmodule.c#L782-L799\n\nHolding the lock across the fork avoids the hang. With these two lines added to the reproducer, 0 of 20 children hang on 3.14.4, 3.14.8 and 3.15.0rc3:\n\n```python\nimport _warnings\nos.register_at_fork(before=_warnings._acquire_lock, after_in_parent=_warnings._release_lock, after_in_child=_warnings._release_lock)\n```\n\nA fix could take the lock in `PyOS_BeforeFork()` and release it after the fork, as is done for the import lock, or reset it in `PyOS_AfterFork_Child()`, as is done for `_Py_jit_debug_mutex`.\n\nThis hangs JupyterLab terminals. [terminado](https://github.com/jupyter/terminado/blob/8be6c17a575afd396415feb68d52bb92f94128ae/terminado/management.py#L58) starts them with ptyprocess on the server's event loop thread, and ptyprocess [waits for `exec()` with a blocking read](https://github.com/pexpect/ptyprocess/blob/12d3221806c0a7cf114b894101814b1f9e338ed4/ptyprocess/ptyprocess.py#L322) after `pty.fork()`. When the child hangs inside `os.forkpty()`, the whole Jupyter server stops responding. IPython runs `!` commands through pexpect, and pexpect uses the same code.\n\nI know this is grey territory. The `os.fork()` documentation says that \"it has never been safe to mix threading with os.fork() on POSIX platforms\", and earlier reports of hangs in a fork child were closed on that basis (#111635, #112788). Here the child runs no code of its own, and it waits for a lock that CPython added in 3.14. Two earlier issues about a lock or state that the child inherits were fixed:\n\n- #112275: the child inherited `HEAD_LOCK` locked and deadlocked on it in `PyOS_AfterFork_Child()`. The fix was backported to 3.8.\n- #139894: a 3.14 regression in the asyncio state that the child inherits. It was fixed in 3.14 by adding `reset_asyncio_state()` to `PyOS_AfterFork_Child()`.\n\nThe same lock can also hang interpreter shutdown, with no fork. At shutdown, the daemon thread is frozen while it holds the lock, and the `ResourceWarning` for its socket then blocks the main thread. The reproducer without the fork loop and the final `os._exit(0)` hangs at exit in 10 of 10 runs on 3.14.4, 8 of 10 on 3.14.8 and 9 of 10 on 3.15.0rc3. It hangs in 0 of 10 runs on 3.13.16, and on 3.14.4 without the socket. I can open a separate issue for it.\n\n---\n\nI used Opus 5.5 for assistance with investigation and the writeup.\n\n### CPython versions tested on:\n\n3.12, 3.13, 3.14, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158974\n* gh-159613\n* gh-159614\n* gh-159615\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/krassowski","@type":"Person","name":"krassowski"},"datePublished":"2026-10-07T15:39:56Z","url":"https://github.com/python/cpython/issues/158973"}
| 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:fca11231-3be7-3900-7e8f-d5872e07ba38 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C6E:16E4ED:AD8BCE:D9C3DD:6B021F55 |
| html-safe-nonce | c6a40b5d6ca3ee5157eba5c4eef8988ebd9d829cc448d797d3bda7535a13dd59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzZFOjE2RTRFRDpBRDhCQ0U6RDlDM0REOjZCMDIxRjU1IiwidmlzaXRvcl9pZCI6IjEzMzczNzc2ODE2MTE4MjQ5ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1164af1cd56a08f63ae00d2b82aa8c6d0e2a4a5c9e2c83f5e18e0ad9d7c4db9f |
| hovercard-subject-tag | issue:5747663008 |
| 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/158973/issue_layout |
| twitter:image | https://opengraph.githubassets.com/126eeea2d884fa6c2630073b5d196a6c28d08397fd63ad5435fff61b63a53c0f/python/cpython/issues/158973 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/126eeea2d884fa6c2630073b5d196a6c28d08397fd63ad5435fff61b63a53c0f/python/cpython/issues/158973 |
| og:image:alt | Bug report Since 3.14, the warnings module protects its state with a lock in the interpreter, added in #128386 for #128384. PyOS_AfterFork_Child() does not reset this lock. If another thread holds ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | krassowski |
| 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