Skip to content

Commit

Permalink
use cancel_register in _send_message_to_host_task
Browse files Browse the repository at this point in the history
avoids creating a Cancelled manually and a double-reschedule race
  • Loading branch information
richardsheridan committed Mar 9, 2023
1 parent 644b913 commit b87456f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions trio/_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,15 @@ def _check_token(trio_token):

def _send_message_to_host_task(message, trio_token):
task_register = THREAD_LOCAL.task_register
cancel_register = THREAD_LOCAL.cancel_register

def in_trio_thread():
task = task_register[0]
if task is None:
message.queue.put_nowait(outcome.Error(trio.Cancelled._create()))
trio.lowlevel.reschedule(task, outcome.Value(message))
raise_cancel = cancel_register[0]
if raise_cancel is None:
task = task_register[0]
trio.lowlevel.reschedule(task, outcome.Value(message))
else:
message.queue.put_nowait(outcome.capture(raise_cancel))

trio_token.run_sync_soon(in_trio_thread)
return message.queue.get().unwrap()
Expand Down

0 comments on commit b87456f

Please sign in to comment.