From 66069acc1abca13fadd4f73e42f3f5abec89e996 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Mon, 23 Oct 2023 22:51:28 -0400 Subject: [PATCH] fix thread reentry deadlocks 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.) --- trio/_threads.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/trio/_threads.py b/trio/_threads.py index d44e4b68f8..023b1c198e 100644 --- a/trio/_threads.py +++ b/trio/_threads.py @@ -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)