Skip to content

gh-102512: Fix threading after os.fork() called from a foreign thread #102517

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

Closed
wants to merge 4 commits into from
Closed
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
49 changes: 49 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,55 @@ def func():
self.assertEqual(err.decode('utf-8'), "")
self.assertEqual(data, "Thread-1 (func)\nTrue\nTrue\n")

@unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
@support.requires_fork()
@unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
def test_main_thread_after_fork_from_foreign_thread(self):
code = """if 1:
import os, threading, sys, traceback, _thread
from test import support

def func(lock):
# call current_thread() before fork to allocate DummyThread
current = threading.current_thread()
print(current.name)
# flush before fork, so child won't flush it again
sys.stdout.flush()
pid = os.fork()
if pid == 0:
main = threading.main_thread()
print(main.ident == threading.current_thread().ident)
print(main.ident == threading.get_ident())
# stdout is fully buffered because not a tty,
# we have to flush before exit.
sys.stdout.flush()
try:
threading._shutdown()
os._exit(0)
except:
os._exit(1)
else:
try:
support.wait_process(pid, exitcode=0)
except Exception as e:
# avoid 'could not acquire lock for
# <_io.BufferedWriter name='<stderr>'> at interpreter shutdown,'
traceback.print_exc()
sys.stderr.flush()
finally:
lock.release()

join_lock = _thread.allocate_lock()
join_lock.acquire()
th = _thread.start_new_thread(func, (join_lock,))
join_lock.acquire()
"""
# "DeprecationWarning: This process is multi-threaded, use of fork() may lead to deadlocks in the child"
_, out, err = assert_python_ok("-W", "ignore::DeprecationWarning", "-c", code)
data = out.decode().replace('\r', '')
self.assertEqual(err, b"")
self.assertEqual(data, "Dummy-1\nTrue\nTrue\n")

def test_main_thread_during_shutdown(self):
# bpo-31516: current_thread() should still point to the main thread
# at shutdown
Expand Down
2 changes: 1 addition & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ class _DummyThread(Thread):
def __init__(self):
Thread.__init__(self, name=_newname("Dummy-%d"),
daemon=_daemon_threads_allowed())

self._set_tstate_lock()
self._started.set()
self._set_ident()
if _HAVE_THREAD_NATIVE_ID:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix handling threading after os.fork() called from a foreign thread (aka
DummyThread).