Skip to content

Commit 69c398b

Browse files
committed
resolved threads+gc problem, though not sure if in a good way
1 parent af964cf commit 69c398b

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/trio/_core/_run.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,25 +1640,7 @@ def _attempt_delivery_of_any_pending_cancel(self) -> None:
16401640
if not self._cancel_status.effectively_cancelled:
16411641
return
16421642

1643-
# FIXME: I haven't quite figured out how to get the cancel reason to stay
1644-
# alive for passing into other threads, but also not cause any cyclic garbage.
1645-
if (
1646-
self._cancel_status is None
1647-
or self._cancel_status._scope._cancel_reason is None
1648-
):
1649-
# _cancel_status._cancel_reason is None when misnesting
1650-
cancelled = Cancelled._create(source="unknown", reason="misnesting")
1651-
else:
1652-
cancelled = Cancelled._create(
1653-
source=self._cancel_status._scope._cancel_reason.source,
1654-
reason=self._cancel_status._scope._cancel_reason.reason,
1655-
source_task=self._cancel_status._scope._cancel_reason.source_task,
1656-
)
1657-
1658-
def raise_cancel() -> NoReturn:
1659-
raise cancelled
1660-
1661-
self._attempt_abort(raise_cancel)
1643+
self._attempt_abort(RaiseCancel(self._cancel_status._scope._cancel_reason))
16621644

16631645
def _attempt_delivery_of_pending_ki(self) -> None:
16641646
assert self._runner.ki_pending
@@ -1672,6 +1654,24 @@ def raise_cancel() -> NoReturn:
16721654
self._attempt_abort(raise_cancel)
16731655

16741656

1657+
class RaiseCancel:
1658+
def __init__(self, reason: CancelReason | None) -> None:
1659+
if reason is None:
1660+
self.cancelled = Cancelled._create(source="unknown", reason="misnesting")
1661+
else:
1662+
self.cancelled = Cancelled._create(
1663+
source=reason.source,
1664+
reason=reason.reason,
1665+
source_task=reason.source_task,
1666+
)
1667+
1668+
def __call__(self) -> NoReturn:
1669+
try:
1670+
raise self.cancelled
1671+
finally:
1672+
del self.cancelled
1673+
1674+
16751675
################################################################
16761676
# The central Runner object
16771677
################################################################

src/trio/_threads.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ def deliver_worker_fn_result(result: outcome.Outcome[RetT]) -> None:
430430

431431
def abort(raise_cancel: RaiseCancelT) -> trio.lowlevel.Abort:
432432
# fill so from_thread_check_cancelled can raise
433-
cancel_register[0] = raise_cancel
433+
import copy
434+
435+
cancel_register[0] = copy.copy(raise_cancel)
434436
if abandon_bool:
435437
# empty so report_back_in_trio_thread_fn cannot reschedule
436438
task_register[0] = None

0 commit comments

Comments
 (0)