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