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:f348a75c-7924-b604-d10e-f12f204fc12e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A378:1F8468:332AE1B:443C928:6A654712 |
| html-safe-nonce | 84f889d634fb49eb5f48ab334bdf463039d3a034d6d8ca550506a584b9a8db9e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMzc4OjFGODQ2ODozMzJBRTFCOjQ0M0M5Mjg6NkE2NTQ3MTIiLCJ2aXNpdG9yX2lkIjoiMzY3NTIzODA1NzA5OTIxNjY1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 89d8e8cb4b3b06cac31224a0c49c1de392cc8372c6ab37cf5824ec0893a0436a |
| 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 | 52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b |
| 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 | 309153364422b3c499922d1a2a6404910a58ed8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width