Skip to content

Commit aed6e26

Browse files
committed
Fix resource leake in test_cwe_404.py
Need to wait for individual handler tasks when shutting down server.
1 parent c497789 commit aed6e26

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

tests/test_asyncio/test_cwe_404.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def __init__(self, addr, redis_addr, delay: float = 0.0):
1515
self.send_event = asyncio.Event()
1616
self.server = None
1717
self.task = None
18+
self.cond = asyncio.Condition()
19+
self.running = 0
1820

1921
async def __aenter__(self):
2022
await self.start()
@@ -63,10 +65,10 @@ async def stop(self):
6365
except asyncio.CancelledError:
6466
pass
6567
await self.server.wait_closed()
66-
# do we need to close individual connections too?
67-
# prudently close all async generators
68-
loop = self.server.get_loop()
69-
await loop.shutdown_asyncgens()
68+
# Server does not wait for all spawned tasks. We must do that also to ensure
69+
# that all sockets are closed.
70+
async with self.cond:
71+
await self.cond.wait_for(lambda: self.running == 0)
7072

7173
async def pipe(
7274
self,
@@ -75,6 +77,7 @@ async def pipe(
7577
name="",
7678
event: asyncio.Event = None,
7779
):
80+
self.running += 1
7881
try:
7982
while True:
8083
data = await reader.read(1000)
@@ -94,6 +97,10 @@ async def pipe(
9497
# ignore errors on close pertaining to no event loop. Don't want
9598
# to clutter the test output with errors if being garbage collected
9699
pass
100+
async with self.cond:
101+
self.running -= 1
102+
if self.running == 0:
103+
self.cond.notify_all()
97104

98105

99106
@pytest.mark.onlynoncluster

0 commit comments

Comments
 (0)