8000 gh-127421: Fix race in test_start_new_thread_failed (#127549) · python/cpython@13b68e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13b68e1

Browse files
authored
gh-127421: Fix race in test_start_new_thread_failed (#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.
1 parent 0cb5222 commit 13b68e1

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Lib/test/test_threading.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,11 +1192,12 @@ def f():
11921192
resource.setrlimit(resource.RLIMIT_NPROC, (0, hard))
11931193
11941194
try:
1195-
_thread.start_new_thread(f, ())
1195+
handle = _thread.start_joinable_thread(f)
11961196
except RuntimeError:
11971197
print('ok')
11981198
else:
11991199
print('!skip!')
1200+
handle.join()
12001201
"""
12011202
_, out, err = assert_python_ok("-u", "-c", code)
12021203
out = out.strip()

0 commit comments

Comments
 (0)
0