File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -309,6 +309,7 @@ async def __aexit__(
309309 ) -> None :
310310 if self ._root :
311311 await self ._root .__aexit__ (exc_type , exc_val , exc_tb )
312+ self ._root = None
312313
313314
314315class SyncContainer (_BaseContainer ):
@@ -340,6 +341,7 @@ def __exit__(
340341 ) -> None :
341342 if self ._root :
342343 self ._root .__exit__ (exc_type , exc_val , exc_tb )
344+ self ._root = None
343345
344346 @property
345347 def root (self ) -> SyncContext :
Original file line number Diff line number Diff line change @@ -24,3 +24,26 @@ async def dependency() -> AsyncIterator[int]:
2424
2525 assert shutdown is False
2626 assert shutdown is True
27+
28+
29+ async def test_root_context_should_close_singletons () -> None :
30+ open_count = 0
31+ closed_count = 0
32+
33+ @contextlib .asynccontextmanager
34+ async def dependency () -> AsyncIterator [int ]:
35+ nonlocal open_count , closed_count
36+ open_count += 1
37+ yield i
38+ closed_count += 1
39+
40+ container = Container ()
41+ container .register (Singleton (dependency ))
42+
43+ for i in range (1 , 5 + 1 ):
44+ async with container :
45+ assert closed_count == i - 1
46+ assert await container .root .resolve (int ) == i
47+ assert open_count == i
48+ assert closed_count == i - 1
49+ assert closed_count == i
Original file line number Diff line number Diff line change @@ -23,3 +23,26 @@ def dependency() -> Iterator[int]:
2323
2424 assert shutdown is False
2525 assert shutdown is True
26+
27+
28+ async def test_root_context_should_close_singletons () -> None :
29+ open_count = 0
30+ closed_count = 0
31+
32+ @contextlib .contextmanager
33+ def dependency () -> Iterator [int ]:
34+ nonlocal open_count , closed_count
35+ open_count += 1
36+ yield i
37+ closed_count += 1
38+
39+ container = SyncContainer ()
40+ container .register (Singleton (dependency ))
41+
42+ for i in range (1 , 5 + 1 ):
43+ with container :
44+ assert closed_count == i - 1
45+ assert container .root .resolve (int ) == i
46+ assert open_count == i
47+ assert closed_count == i - 1
48+ assert closed_count == i
You can’t perform that action at this time.
0 commit comments