fix(selector): close the token iterator displaced on retry - #2217
Open
AkramBitar wants to merge 1 commit into
Open
fix(selector): close the token iterator displaced on retry#2217AkramBitar wants to merge 1 commit into
AkramBitar wants to merge 1 commit into
Conversation
Selector.selectInternal replaced s.cache on every immediate retry without closing the iterator it displaced, so a single Select could abandon up to maxImmediateRetries iterators. On the lazy fetcher path an iterator wraps sql.Rows, so each one held a database cursor and its pooled connection until the finalizer ran — draining, under contention, the same pool the vault and commit path use. s.mu is documented as protecting the cache field, but selectInternal read s.cache.Next() and wrote s.cache without holding it. A concurrent Close() nils the field, so it could be dereferenced after the isClosed() check. Route both accesses through mutex-guarded helpers: next() reads the current iterator under s.mu, and swapCache() closes the displaced iterator before installing the new one. If the selector was closed while the fetch was in flight, swapCache closes the new iterator too and reports the error, so no iterator is left unclosed on any path. Fixes #2019 Signed-off-by: AkramBitar <akram@il.ibm.com>
AkramBitar
force-pushed
the
fix-2019-selector-iterator-leak
branch
from
August 13, 2026 14:56
a90ed6a to
5923da7
Compare
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.
Fixes #2019
The problem
To spend your tokens, the code asks the database for a list of them. What comes back is a cursor — a live handle to that list. Every open cursor takes one connection out of a small shared pool, and only gives it back when you close it.
Tokens are briefly locked while another transaction is being put together, so the code often runs out of candidates and has to ask for a fresh list — up to 5 times. Each retry opened a new cursor and forgot the old one without closing it. One selection could leave 5 cursors open, each still holding a connection.
Nothing visibly broke, but the timing is bad: retries only happen when many transactions compete for the same tokens — exactly when connections are scarce, and they come from the same pool the ledger needs. The busier the system, the more it wasted.
The fix
Close the old cursor before installing the new one, and do it while holding the lock that was already supposed to guard it. That lock was being skipped, which also meant a crash if something closed the selector mid-retry — fixed by the same change.
One cursor open at a time now, instead of six. No API, config, or behaviour change.
Tests
Two new tests: one checks every replaced cursor gets closed, the other runs a close concurrently with a retry. Both fail on the old code (5 leaked cursors; data races and a nil-pointer panic), and pass with the fix.