Skip to content

Commit

Permalink
fix thread reentry deadlocks
Browse files Browse the repository at this point in the history
if there is a checkpoint after the result is put on the queue to the thread, there is a thread race with a chance to reschedule the task twice, causing hangs.

The try-finally suite was removed as it makes typing difficult and none of the operations can fail (barring MemoryErrors and friends.)
  • Loading branch information
richardsheridan committed Oct 24, 2023
1 parent c212435 commit 66069ac
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions trio/_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ async def run(self) -> None:
task = trio.lowlevel.current_task()
old_context = task.context
task.context = self.context.copy()
try:
await trio.lowlevel.cancel_shielded_checkpoint()
result = await outcome.acapture(self.unprotected_afn)
self.queue.put_nowait(result)
finally:
task.context = old_context
await trio.lowlevel.cancel_shielded_checkpoint()
await trio.lowlevel.cancel_shielded_checkpoint()
result = await outcome.acapture(self.unprotected_afn)
task.context = old_context
await trio.lowlevel.cancel_shielded_checkpoint()
self.queue.put_nowait(result)

async def run_system(self) -> None:
result = await outcome.acapture(self.unprotected_afn)
Expand Down

0 comments on commit 66069ac

Please sign in to comment.