Repository navigation
Fix flaky test_concurrent_watches losing one watch - #114041
Conversation
test_keeper_back_to_back::test_concurrent_watches fails as `assert 999 == 1000`. It is a test bug, not a Keeper defect. trigger_watch mutated a path before it owned the corresponding existing_path token, and removed the token afterwards inside a bare `except: pass`. Since random.choice + set() + remove() is not atomic across the 10 pool threads, two threads can select the same token when only one is present. A watch is one-shot per (path, session), so the second mutation notifies nobody, and the deferred remove then consumes a token belonging to a different, still outstanding registration. That registration is never mutated again, so its callback never fires and the aggregate count lands one short. In the failing run this happened on /487: register, two mutations 1 ms apart, one event, a second register, then a stale remove ate its token, while the run's last mutation was ~1.8 s later on another path. The expected count is 1000 rather than the number of distinct paths because kazoo fans a single event out to every callback registered for that path, which is why the test creates a new closure per registration. Claim the token atomically before mutating and skip when nothing was claimed, so that every registration is followed by a mutation. All four assertions are kept verbatim, including the exact count: the test now satisfies its own precondition instead of relying on the race not firing. tests/integration/test_keeper_watches/test.py is the in-tree precedent for that discipline. The lock covers only the test's list bookkeeping and never a Keeper call, so the threads still issue overlapping requests and the multi-watcher collisions remain (372 duplicate registrations, up to 6 on one path). Validated with a deterministic replay of the observed schedule against a real Keeper (pre-fix body fails 3/3, post-fix passes 3/3), by four mutation arms that each redden the retained assertion, and by 50/50 repetitions under the job that reported the failure.
The bound was assert trigger_called >= 1000, which is all that could hold while mutating a path the thread held no token for was ordinary behaviour: pre-fix runs issue 1024-1034 mutations for 1000 registrations and pass. Claiming a token before mutating makes the count exact, so a revert of that change now reddens the test (assert 1026 == 1000) instead of passing silently. Also fix the comment: a mutation that consumes a registration does notify it. The registration left unnotified is a later one, orphaned because a stale removal ate its token. Verified: restoring the pre-fix bodies under the tightened bound fails 1026 == 1000 with 26 stale removes; the tightened test passes 25/25 under Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6), the suite passes 9/9, and trigger_called is exactly 1000 across 0 duplicates, 979 duplicates, 1 thread and 50 threads. Mutation arms that drop, steal or duplicate a notification still fail on dumb_watch_triggered_counter (997, 999, 1001).
Internal second-model review (2 rounds, 0 open findings)Reviewed by an independent model that did not write the patch, over two rounds. Round 1: 1 major, agreed and fixed. ❌ The fix was not verifiable by reverting it. Round 2: 0 findings on the code; 3 majors on the PR description, corrected before publication.
💡 Not changed: the empty changelog entry is correct for the Also observed while triaging, not addressed here: three failures attributed to this test name are |
Pre-PR validation gate
Randomized runs: 50/50 and then 25/25 pass under |
|
cc @alesapin, could you review this? It is a test-only fix: the test mutated a path before claiming its token from a list shared by 10 pool threads, so a stale removal could drop the token of a later registration, which then never got a mutation and its one-shot watch never fired. |
|
Workflow [PR], commit [e02e7a8] Summary: ✅ AI ReviewSummaryThis PR fixes Final Verdict✅ No blocking issues found. |
CI finish ledger — e02e7a8CI is fully finished on this commit:
Session id: cron:our-pr-ci-monitor:20260809-210000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
test_keeper_back_to_back::test_concurrent_watchesfails asassert 999 == 1000(test.py:832) onIntegration tests (amd_asan_ubsan, db disk, old analyzer, 5/6):CI report.
It is a test bug; Keeper honours the ZooKeeper contract.
Root cause.
trigger_watchmutated a path before owning itsexisting_pathtoken, then removedthe token inside a bare
except: pass.random.choice+set()+remove()is not atomic across the10 pool threads, so two threads can pick the same token. A watch is one-shot per (path, session), so
the second mutation notifies nobody, and the deferred
removeconsumes a token belonging to adifferent, still outstanding registration, which is then never mutated and never fires. Observed on
/487: register, two mutations 1 ms apart, one event, a second register, then a stale remove eats itstoken.
Fix. Claim the token atomically before mutating, and skip when nothing was claimed, so every
registration is followed by a mutation.
dumb_watch_triggered_counter == 1000and two otherassertions are unchanged;
trigger_called >= 1000is tightened to== 1000, which removing unclaimedmutations makes exact. That tightening is what makes the fix testable: restoring the old bodies under
it fails
assert 1026 == 1000, while under>=they passed. The lock covers only the test's own list,never a Keeper call.
Validation. A deterministic replay of that schedule against a real Keeper fails 3/3 before and
passes 3/3 after. Pre-fix runs issue 1024-1034 mutations for 1000 tokens, versus exactly 1000 in 11/11
runs after (including 1-thread and 50-thread); a watch is lost only when a stale remove lands on a live
token, hence the low rate. 25/25 repetitions pass under the failing job, the suite passes 9/9, five
mutation arms each redden the assertion, and collisions are unchanged (372 duplicate registrations, up
to 6 per path), so the lock did not serialize the test.