FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.14] gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) by miss-islington · Pull Request #134596 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 22 additions & 0 deletions Lib/test/_test_multiprocessing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6832,6 +6832,28 @@ def f(x): return x*x
self.assertEqual("332833500", out.decode('utf-8').strip())
self.assertFalse(err, msg=err.decode('utf-8'))

def test_forked_thread_not_started(self):
# gh-134381: Ensure that a thread that has not been started yet in
# the parent process can be started within a forked child process.

if multiprocessing.get_start_method() != "fork":
self.skipTest("fork specific test")

q = multiprocessing.Queue()
t = threading.Thread(target=lambda: q.put("done"), daemon=True)

def child():
t.start()
t.join()

p = multiprocessing.Process(target=child)
p.start()
p.join(support.SHORT_TIMEOUT)

self.assertEqual(p.exitcode, 0)
self.assertEqual(q.get_nowait(), "done")
close_queue(q)


#
# Mixins
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :exc:`RuntimeError` when using a not-started :class:`threading.Thread` after calling :func:`os.fork`
6 changes: 6 additions & 0 deletions Modules/_threadmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
continue;
}

// Keep handles for threads that have not been started yet. They are
// safe to start in the child process.
if (handle->state == THREAD_HANDLE_NOT_STARTED) {
continue;
}

// Mark all threads as done. Any attempts to join or detach the
// underlying OS thread (if any) could crash. We are the only thread;
// it's safe to set this non-atomically.
Expand Down

Back | FazBrowse Home | New Git URL