Fix CapacityLimiter._pending_borrowers leak on non-Cancelled exceptions#3399
Closed
bysiber wants to merge 1 commit intopython-trio:mainfrom
Closed
Fix CapacityLimiter._pending_borrowers leak on non-Cancelled exceptions#3399bysiber wants to merge 1 commit intopython-trio:mainfrom
bysiber wants to merge 1 commit intopython-trio:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3399 +/- ##
===============================================
Coverage 100.00000% 100.00000%
===============================================
Files 128 128
Lines 19424 19424
Branches 1318 1318
===============================================
Hits 19424 19424
🚀 New features to boost your workflow:
|
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.
3f66165 to
16ec20f
Compare
A5rocks
requested changes
Feb 20, 2026
Contributor
A5rocks
left a comment
There was a problem hiding this comment.
Please create an issue before fixing bugs like this. I'm fairly certain this is not actually an issue, so I will be closing this. I'd be happy to reopen this though.
| await self._lot.park() | ||
| except trio.Cancelled: | ||
| self._pending_borrowers.pop(task) | ||
| except BaseException: |
Contributor
There was a problem hiding this comment.
KI cannot be raised here because we are in a @enable_ki_protection function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CapacityLimiter.acquire_on_behalf_ofonly cleans up_pending_borrowerswhenpark()raisestrio.Cancelled. Ifpark()raises any other exception — such asKeyboardInterruptdelivered to a parked task via the abort mechanism — the task's entry stays in_pending_borrowerspermanently.The relevant code:
When
KeyboardInterruptis delivered to the task, the abort function succeeds and the task is rescheduled withraise_cancel()which raisesKeyboardInterrupt, nottrio.Cancelled. The stale entry leaks.Fix
Broadened the exception handler to
BaseExceptionand usepop(task, None)for safety against double-removal: