Skip to content

Commit

Permalink
Fix scheduler hang when pending limit is reached
Browse files Browse the repository at this point in the history
  • Loading branch information
pollydrag committed Sep 21, 2021
1 parent b977dca commit a20ddc8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES/97.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix scheduler hang when pending limit is reached.

10 changes: 7 additions & 3 deletions aiojobs/_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ async def spawn(self, coro):
job = Job(coro, self, self._loop)
should_start = (self._limit is None or
self.active_count < self._limit)
self._jobs.add(job)
if should_start:
job._start()
else:
# wait for free slot in queue
await self._pending.put(job)
try:
# wait for free slot in queue
await self._pending.put(job)
except asyncio.CancelledError:
await job.close()
raise
self._jobs.add(job)
return job

async def close(self):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,19 @@ async def coro(fut):
fut3 = asyncio.Future()

await scheduler.spawn(coro(fut1))
assert scheduler.active_count == 1
assert scheduler.pending_count == 0

await scheduler.spawn(coro(fut2))
assert scheduler.active_count == 1
assert scheduler.pending_count == 1

with pytest.raises(asyncio.TimeoutError):
# try to wait for 1 sec to add task to pending queue
with timeout(1, loop=loop):
await scheduler.spawn(coro(fut3))

assert scheduler.active_count == 1
assert scheduler.pending_count == 1


Expand Down

0 comments on commit a20ddc8

Please sign in to comment.