Skip to content

fix(search): run the debounced work once when both timers fire - #4030

Merged
aduffeck merged 1 commit into
mainfrom
fix/search-debouncer-double-run
Nov 18, 2026
Merged

aduffeck merged 1 commit into
mainfrom
fix/search-debouncer-double-run

Conversation

@dschmidt

Copy link
Copy Markdown
Contributor

SpaceDebouncer keeps a debounce timer and a timeout timer per space, and both call the same work function. That function checked for a run in progress before taking the mutex. When both timers are due together, both calls pass the check and the space is indexed twice at the same time.

The work function now takes the mutex first and returns when its work item is no longer the pending one.

This is also the cause of the flaky unit test SpaceDebouncer fires at the timeout even when continuously debounced (Expected 2 to equal 1). Its timers are due about 50ms apart, which a busy CI runner can turn into "together". In production the timers are 1s and 30s, so it needs steady events on one space close to the 30s mark, or a stalled process.

Tested on current main:

before after
the flaky spec, 150 runs with 200 busy loops pinned to the same core 10 failed 0 failed
new spec with both timers due together, no load 10 of 10 failed 0 of 20 failed

The package is green with -race. Not tested against a running instance.

AI usage: the flake was reproduced and traced with Claude Code (claude-fable-5-2), which also wrote the patch and the test.

Every pending space has a debounce timer and a timeout timer, and both
call the same work function. The function checked for a run in progress
before taking the mutex, so when both timers were due together, both
calls passed the check and the space was indexed twice at the same
time.

Take the mutex first and return when the work item is no longer the
pending one.

This is also what made "fires at the timeout even when continuously
debounced" flaky: its two timers are due about 50ms apart, which is
close enough on a busy CI runner.
@codacy-production

codacy-production Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

🟢 Coverage 100.00% diff coverage · +0.00% coverage variation

Metric Results
Coverage variation ✅ +0.00% coverage variation (-1.00%)
Diff coverage ✅ 100.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (2032e85) 94240 28200 29.92%
Head commit (ca733e7) 94243 (+3) 28201 (+1) 29.92% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#4030) 7 7 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@dschmidt
dschmidt marked this pull request as ready for review November 17, 2026 11:17

@aduffeck aduffeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@aduffeck
aduffeck merged commit 9200fac into main Nov 18, 2026
61 of 62 checks passed
@aduffeck
aduffeck deleted the fix/search-debouncer-double-run branch November 18, 2026 09:28
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.

2 participants