Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if daemon task is still alive while waiting, otherwise we … #219

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions saq/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ async def listen(self, *channels: str, timeout: float | None = None) -> t.AsyncG
queue = await self.subscribe(*channels)

try:
while True:
if timeout:
yield await asyncio.wait_for(queue.get(), timeout)
else:
yield await queue.get()
queue.task_done()
while self._daemon_task and not self._daemon_task.done():
try:
yield await asyncio.wait_for(queue.get(), timeout if timeout else 1.0)
queue.task_done()
except asyncio.TimeoutError:
if timeout:
raise
finally:
await self.unsubscribe(queue)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,13 @@ async def test_priority(self) -> None:
assert await self.enqueue("test", priority=-1)
self.assertEqual(await self.count("queued"), 1)
assert not await self.queue.dequeue(0.01)

async def test_listen_cancel(self) -> None:
job = await self.queue.enqueue("test")
task = asyncio.create_task(job.refresh(0))
await asyncio.sleep(0.1)
await self.queue._listener.close()
await task

with self.assertRaises(asyncio.TimeoutError):
await job.refresh(0.1)