Skip to content

fix(selector): close the token iterator displaced on retry - #2217

Open
AkramBitar wants to merge 1 commit into
mainfrom
fix-2019-selector-iterator-leak
Open

fix(selector): close the token iterator displaced on retry#2217
AkramBitar wants to merge 1 commit into
mainfrom
fix-2019-selector-iterator-leak

Conversation

@AkramBitar

@AkramBitar AkramBitar commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

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.

@AkramBitar AkramBitar self-assigned this Aug 13, 2026
@AkramBitar AkramBitar added this to the Q3/26 milestone Aug 13, 2026
@AkramBitar
AkramBitar marked this pull request as ready for review August 13, 2026 14:56
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
AkramBitar force-pushed the fix-2019-selector-iterator-leak branch from a90ed6a to 5923da7 Compare August 13, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sherdlock selector leaks the token iterator when refreshing its cache on retry

1 participant