From 3826bc99723bc21d2e2f6681beeaa9de6c4efdb8 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 14 Dec 2021 10:55:27 -0500 Subject: [PATCH] Don't catch key errors from the yielded to scope --- tractor/trionics/_mngrs.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tractor/trionics/_mngrs.py b/tractor/trionics/_mngrs.py index ab42b4e..af60c70 100644 --- a/tractor/trionics/_mngrs.py +++ b/tractor/trionics/_mngrs.py @@ -161,19 +161,15 @@ async def maybe_open_context( a _Cache hit. ''' + # lock resource acquisition around task racing / ``trio``'s + # scheduler protocol await _Cache.lock.acquire() ctx_key = id(mngr) - value = None + try: - # lock feed acquisition around task racing / ``trio``'s - # scheduler protocol value = _Cache.values[key] - log.info(f'Reusing _Cached resource for {key}') - _Cache.users += 1 - _Cache.lock.release() - yield True, value except KeyError: log.info(f'Allocating new resource for {key}') @@ -196,6 +192,12 @@ async def maybe_open_context( yield False, value + else: + log.info(f'Reusing _Cached resource for {key}') + _Cache.users += 1 + _Cache.lock.release() + yield True, value + finally: _Cache.users -= 1