Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.13] gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) #134597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 22 Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6494,6 +6494,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
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 6 Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,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
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.