Skip to content

Commit

Permalink
[3.13] gh-127421: Fix race in test_start_new_thread_failed (GH-127549) (
Browse files Browse the repository at this point in the history
#127574)

gh-127421: Fix race in test_start_new_thread_failed (GH-127549)

Fix race in test_start_new_thread_failed

When we succeed in starting a new thread, for example if setrlimit
was ineffective, we must wait for the newly spawned thread to exit.
Otherwise, we run the risk that the newly spawned thread will race
with runtime finalization and access memory that has already been
clobbered/freed.

`_thread.start_new_thread()` only spawns daemon threads, which the runtime
does not wait for at shutdown, and does not return a handle. Use
`_thread.start_joinable_thread()` and join the resulting handle when
the thread is started successfully.
(cherry picked from commit 13b68e1)

Co-authored-by: mpage <mpage@meta.com>
  • Loading branch information
miss-islington and mpage authored Dec 3, 2024
1 parent dddea7c commit 0b266aa
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,12 @@ def f():
resource.setrlimit(resource.RLIMIT_NPROC, (0, hard))
try:
_thread.start_new_thread(f, ())
handle = _thread.start_joinable_thread(f)
except RuntimeError:
print('ok')
else:
print('!skip!')
handle.join()
"""
_, out, err = assert_python_ok("-u", "-c", code)
out = out.strip()
Expand Down

0 comments on commit 0b266aa

Please sign in to comment.