From 6a82bab627e6b500a45128f2e6a54b043b8ca810 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 29 Jul 2025 15:13:38 -0400 Subject: [PATCH] Always pop `._Cache.resources` AFTER `mng.__aexit__()` The correct ordering is to de-alloc the surrounding `service_n` + `trio.Event` **after** the `mng` teardown ensuring the `mng.__aexit__()` never can hit a ref-error if it touches either (like if a `tn` is passed to `maybe_open_context()`! --- tractor/trionics/_mngrs.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tractor/trionics/_mngrs.py b/tractor/trionics/_mngrs.py index 99f05852..95ea3390 100644 --- a/tractor/trionics/_mngrs.py +++ b/tractor/trionics/_mngrs.py @@ -223,16 +223,18 @@ class _Cache: task_status: trio.TaskStatus[T] = trio.TASK_STATUS_IGNORED, ) -> None: - async with mng as value: - _, no_more_users = cls.resources[ctx_key] - cls.values[ctx_key] = value - task_status.started(value) - try: - await no_more_users.wait() - finally: - # discard nursery ref so it won't be re-used (an error)? - value = cls.values.pop(ctx_key) - cls.resources.pop(ctx_key) + try: + async with mng as value: + _, no_more_users = cls.resources[ctx_key] + try: + cls.values[ctx_key] = value + task_status.started(value) + await no_more_users.wait() + finally: + value = cls.values.pop(ctx_key) + finally: + # discard nursery ref so it won't be re-used (an error)? + cls.resources.pop(ctx_key) @acm