Title: multi-threading + fork warning when threads are stopped before fork · Issue #137109 · python/cpython · GitHub
Open Graph Title: multi-threading + fork warning when threads are stopped before fork · Issue #137109 · python/cpython
X Title: multi-threading + fork warning when threads are stopped before fork · Issue #137109 · python/cpython
Description: Bug report Bug description: If all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given import os import threading stop = False def tar...
Open Graph Description: Bug report Bug description: If all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given import ...
X Description: Bug report Bug description: If all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given import ...
Opengraph URL: https://github.com/python/cpython/issues/137109
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"multi-threading + fork warning when threads are stopped before fork","articleBody":"# Bug report\n\n### Bug description:\n\nIf all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given\n\n```python\nimport os\nimport threading\n\n\nstop = False\n\n\ndef target():\n while not stop:\n pass\n\n\nthread = threading.Thread(target=target)\n\n\ndef stop_thread():\n global stop\n stop = True\n thread.join()\n\n\ndef restart_thread():\n global thread, stop\n stop = False\n (thread := threading.Thread(target=target)).start()\n\n\nos.register_at_fork(before=stop_thread, after_in_child=restart_thread, after_in_parent=restart_thread)\n\n\nthread.start()\n\nif os.fork() == 0:\n os._exit(0)\n```\nIf the thread is not restarted in the parent, no warning is given. I believe `warn_about_fork_with_threads` should be refactored so that a warning is given to the user only if there are threads detected after the call to `PyOS_BeforeFork`. If it is not possible to give a warning there, as stated in the comments, perhaps one can just do the thread count check there and take note of the result, then warn after `PyOS_AfterFork_Parent`, when safe to do so.\n\n### CPython versions tested on:\n\n3.13\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-141438\n* gh-141614\n* gh-141639\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/P403n1x87","@type":"Person","name":"P403n1x87"},"datePublished":"2025-07-25T16:35:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/137109/cpython/issues/137109"}
| 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:4713a61d-0e3b-6796-3235-9fd17dd86a60 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A56C:3372CB:20D94B3:2D1E9D4:696ABBD4 |
| html-safe-nonce | cb569005616fa8ed4170a262364dd7933234742df3f26cce68fc515be0b97d3c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTZDOjMzNzJDQjoyMEQ5NEIzOjJEMUU5RDQ6Njk2QUJCRDQiLCJ2aXNpdG9yX2lkIjoiNDcxNjI4NTg3Mjc2NjEwNjU4MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8893af8112d35d7677a8559736959a3c36de8e490d157063a87de01f64fe95e9 |
| hovercard-subject-tag | issue:3263780300 |
| 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/137109/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c35799c4e8112d478cd10e6c6b8f410ed3e0193964f31e92decb25f432e0842f/python/cpython/issues/137109 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c35799c4e8112d478cd10e6c6b8f410ed3e0193964f31e92decb25f432e0842f/python/cpython/issues/137109 |
| og:image:alt | Bug report Bug description: If all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given import ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | P403n1x87 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 46ce962e0e18113ea447391b6ace8b02d4d2861e57b4fbab3658698f73d8855b |
| 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 | 30300f30bb3949de255e84a146706a3bdb5c19c9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width