Skip to content

Commit 16ec20f

Browse files
committed
Fix CapacityLimiter._pending_borrowers leak on non-Cancelled exceptions
acquire_on_behalf_of only catches trio.Cancelled when cleaning up _pending_borrowers. If park() raises any other exception (e.g. KeyboardInterrupt delivered via the abort mechanism), the task's entry remains in _pending_borrowers permanently. This is a slow memory leak and can also cause _wake_waiters to encounter stale entries. Broaden the handler to BaseException and use pop with a default to be safe against double-removal.
1 parent 2fd138e commit 16ec20f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/trio/_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ async def acquire_on_behalf_of(self, borrower: Task | object) -> None:
377377
self._pending_borrowers[task] = borrower
378378
try:
379379
await self._lot.park()
380-
except trio.Cancelled:
381-
self._pending_borrowers.pop(task)
380+
except BaseException:
381+
self._pending_borrowers.pop(task, None)
382382
raise
383383
else:
384384
await trio.lowlevel.cancel_shielded_checkpoint()

0 commit comments

Comments
 (0)