-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-114271: Make _thread.ThreadHandle
thread-safe in free-threaded builds
#115190
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
Changes from 5 commits
cf6491c
e86dde8
9a61e20
bcf66ce
7f9720d
092c2eb
77da31d
321a4e0
cd0372c
7badb2d
9d35990
e554c5e
147a192
1fe27ce
9ec6d23
a46b727
61d9fa9
f6bfa08
1cc0f2e
3fbf119
9d21444
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,8 +189,8 @@ def task(): | |
with threading_helper.wait_threads_exit(): | ||
handle = thread.start_joinable_thread(task) | ||
handle.join() | ||
with self.assertRaisesRegex(ValueError, "not joinable"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is interesting... our docs already claim that threads can be joined multiple times. So I wonder why this existing test logic was previously explicitly checking for an error here. In this sense this change aligns with what our docs claim so a behavior change here could be seen as a bugfix. I do not expect anyone to be depending on subsequent join()s of a thread raising regardless. |
||
handle.join() | ||
# Subsequent join() calls should succeed | ||
handle.join() | ||
|
||
def test_joinable_not_joined(self): | ||
handle_destroyed = thread.allocate_lock() | ||
|
@@ -255,7 +255,7 @@ def task(): | |
handles.append(handle) | ||
start_joinable_thread_returned.release() | ||
thread_detached.acquire() | ||
with self.assertRaisesRegex(ValueError, "not joinable"): | ||
with self.assertRaisesRegex(ValueError, "detached and thus cannot be joined"): | ||
handle.join() | ||
|
||
assert len(errors) == 0 | ||
|
@@ -272,7 +272,7 @@ def task(): | |
# detach() returns even though the thread is blocked on lock | ||
handle.detach() | ||
# join() then cannot be called anymore | ||
with self.assertRaisesRegex(ValueError, "not joinable"): | ||
with self.assertRaisesRegex(ValueError, "detached and thus cannot be joined"): | ||
handle.join() | ||
lock.release() | ||
|
||
|
@@ -283,9 +283,74 @@ def task(): | |
with threading_helper.wait_threads_exit(): | ||
handle = thread.start_joinable_thread(task) | ||
handle.join() | ||
with self.assertRaisesRegex(ValueError, "not joinable"): | ||
with self.assertRaisesRegex(ValueError, "joined and thus cannot be detached"): | ||
handle.detach() | ||
|
||
def test_detach_then_detach(self): | ||
def task(): | ||
pass | ||
|
||
with threading_helper.wait_threads_exit(): | ||
handle = thread.start_joinable_thread(task) | ||
handle.detach() | ||
# Subsequent calls to detach() should succeed | ||
handle.detach() | ||
|
||
def test_join_then_self_join(self): | ||
# make sure we can't deadlock in the following scenario with | ||
mpage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# threads t0 and t1: | ||
# | ||
# - t0 joins t1 | ||
# - t1 self joins | ||
def make_lock(): | ||
lock = thread.allocate_lock() | ||
lock.acquire() | ||
return lock | ||
|
||
error = None | ||
self_joiner_handle = None | ||
self_joiner_started = make_lock() | ||
self_joiner_barrier = make_lock() | ||
def self_joiner(): | ||
nonlocal error | ||
|
||
self_joiner_started.release() | ||
self_joiner_barrier.acquire() | ||
|
||
try: | ||
self_joiner_handle.join() | ||
except Exception as e: | ||
error = e | ||
|
||
joiner_started = make_lock() | ||
def joiner(): | ||
joiner_started.release() | ||
self_joiner_handle.join() | ||
|
||
with threading_helper.wait_threads_exit(): | ||
self_joiner_handle = thread.start_joinable_thread(self_joiner) | ||
# Wait for the self-joining thread to start | ||
self_joiner_started.acquire() | ||
|
||
# Start the thread that joins the self-joiner | ||
joiner_handle = thread.start_joinable_thread(joiner) | ||
|
||
# Wait for the joiner to start | ||
joiner_started.acquire() | ||
|
||
# Not great, but I don't think there's a deterministic way to make | ||
gpshead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# sure that the self-joining thread has been joined. | ||
time.sleep(0.1) | ||
|
||
# Unblock the self-joiner | ||
self_joiner_barrier.release() | ||
|
||
self_joiner_handle.join() | ||
joiner_handle.join() | ||
|
||
with self.assertRaisesRegex(RuntimeError, "Cannot join current thread"): | ||
raise error | ||
|
||
|
||
class Barrier: | ||
def __init__(self, num_threads): | ||
|
Uh oh!
There was an error while loading. Please reload this page.