Skip to content

test(file-lock): time the lock hand-off against a flat poll, not a fixed bound - #5488

Merged
kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:kalin/file-lock-followup
Oct 24, 2026
Merged

kovtcharov-amd merged 5 commits into
amd:mainfrom
kovtcharov:kalin/file-lock-followup

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

The busy-lock throughput test from #5469 fails on slow or loaded runners. It failed macOS CI (24.3 ms against an 8 ms bound) and 2 of 22 loaded Windows runs, so it can block unrelated PRs. It now times a lock hand-off against a flat 20 ms poll in the same run, and asserts the margin rather than a fixed limit. It passed 30 of 30 runs on Windows under heavy CPU load and still fails when the fast hand-off is reverted. The same change fixes the lock's handling of queue entries this user can't check, such as another user's on NFS:

  • A dead one no longer blocks everyone forever.
  • A client clock an hour behind the server no longer blocks waiters for an hour.
  • An aged-out one is named in a warning and no longer slows every later lock.

It also documents the three ways queue order can slip, and drops an unused "spent" marker.

Reproduced by test_a_busy_lock_keeps_its_throughput on macOS CI (run 39573703043); its replacement test_a_busy_lock_hands_off_far_faster_than_a_flat_poll passes here and fails when the 1 ms poll is reverted to 20 ms. The skew bug is reproduced by test_an_unchecked_entrys_age_ignores_this_machines_clock (client ±1 h); it fails against the previous head and passes here.

Test plan

  • test_a_busy_lock_hands_off_far_faster_than_a_flat_poll 30 of 30 under 6 CPU burners and 30 of 30 under 12 on Windows. Measured: real hand-off ~0.7–0.9 ms against ~10–14 ms with a flat poll.
  • Each new test fails when its fix is reverted:
    • the 1 ms poll set to 20 ms
    • the 20 ms cap removed
    • unchecked entries never ageing out
    • the registry's dot-name skip removed
    • age taken on this machine's clock
    • no warning for an aged-out entry
    • the fast path not looking past it
    • it missing from the timeout message
  • Reviewer probes on the new head: a client 5 s, 60 s or 1 h behind the server now gets the lock (skew.py). One stale foreign entry costs 0.34 ms per uncontended lock against 0.31 ms clean (stalecost.py).
  • python -m pytest tests/unit/test_file_lock.py tests/unit/test_concurrent_state_writes.py tests/unit/rag/ tests/test_rag.py tests/unit/test_hub_router.py tests/unit/test_hub_installer.py tests/unit/test_daemon_hub_routes.py tests/unit/test_skills_install_concurrency.py tests/unit/agents/test_registry_load_errors.py -n 4 on Windows, with an isolated GAIA_HOME. Everything passes except three tests that also fail on main in this environment (no pip in the venv; a registry test that ignores GAIA_HOME).
  • python util/lint.py: black, isort, flake8, pylint and the other checks pass. The tool-description check fails locally only, because it imports this machine's editable install from another checkout; CI's lint passed.
  • CI unit lanes pass on Linux, Windows and macOS at e2c595e (run 39609097494).
🔍 Technical details
  • Why a margin, not a ratio. Sleep overshoot (macOS CI) adds about the same delay to both runs, so a 3× ratio fails there while a 4 ms margin holds.
    • Each run takes 40 rounds, released at a random point of the waiter's poll.
    • The flat run's median landed between 7.9 and 15.2 ms across the reviewer's 72 runs. The real run stays near 1 ms, so the margin held every time.
    • A 4-thread throughput design was dropped: under the GIL one thread can run all its cycles alone, so a flat poll looked just as fast.
    • The absolute-bound latency test is folded into this one.
  • Unchecked entries. An entry this user cannot test (another user's, or NFS flock emulation) stops counting as ahead once older than LOCK_TIMEOUT_SECONDS.
    • Clock: the age is measured in the file server's time, as the waiter's own entry mtime plus the monotonic time since it joined, against the stuck entry's mtime. Only a waiter with no entry of its own, in a read-only folder, falls back to this machine's clock.
    • Never deleted: nobody can confirm its owner is gone. It is logged once per process, stays in the timeout message's "delete" list, and the fast path looks past it.
    • Worst case: a live one with a long timeout loses its place, never exclusion, because only the main lock decides the holder.
  • Spent marker removed. It showed no measurable gain and no test needed it. An unlocked entry is re-checked every poll and reaped after the waiter's turn, so a not-yet-locked earlier arrival is never skipped for good. test_an_earlier_entry_seen_before_its_owner_locked_it_still_counts still guards that.
  • Where order can slip. The module docstring now lists all three. Mutual exclusion never depends on any of them.
    • A request whose fast path failed, and that has not yet written its entry, can be beaten by one that arrives as the lock comes free.
    • An entry created but not yet locked reads as finished to a waiter polling at that instant.
    • An aged-out unchecked entry loses its place.
  • New tests. The _poll_interval 20 ms cap, and registry discovery skipping dot-named folders. list_installed already had one.

…xed bound

The busy-lock throughput test failed on macOS CI (24.3 ms against an 8 ms
bound) and twice in 22 loaded Windows runs, because it timed wall clock
against an absolute limit on runners that overshoot short sleeps.

- The test now times release-to-next-holder with the real poll and with a
  flat 20 ms poll in the same run, and asserts the flat run is at least
  4 ms slower. Sleep overshoot adds about the same to both, so a margin
  holds where a ratio does not. 30 of 30 runs pass under 6 and 12 CPU
  burners.
- The spent marker is gone: it bought no measurable speed and no test
  needed it. An unlocked entry is re-checked every poll and reaped after
  the turn.
- An entry this user cannot check (another user's, or on NFS) stops
  counting as ahead once older than the lock timeout, so a dead one can no
  longer block everyone. It is never deleted, and at worst a live one
  loses its place, never exclusion.
- The module docstring names the sub-millisecond window in which a request
  arriving as the lock comes free can beat one that is about to queue.
- New tests cover the 20 ms poll cap and the dot-name skip in registry
  discovery.
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve

The lock hand-off test that failed on slow macOS and loaded Windows runners now compares the real hand-off against a flat 20 ms poll in the same run, instead of using a fixed time limit. It should stop blocking unrelated PRs and still catches the regression it was written for. The lock changes look correct. An entry left by another user, or by NFS, can no longer block a lock forever. The unused "spent" marker is gone, and nothing in the queue's correctness depended on it.

No blocking issues. One small note is in the details below.

Real-world evidence

N/A: this changes internal lock code and tests, with no CLI, API, MCP or UI surface. No evidence bundle was produced. pytest isn't installed on this review runner, so I couldn't run the tests here. The verdict rests on static review plus the author's Windows runs under load (30 of 30) and their revert checks.

🔍 Technical details

Issues found

🟢 A stale entry slows that lock for good (src/gaia/utils/file_lock.py:298). A _STALE entry stops counting as ahead, but it is never reaped. While it sits in <name>.lock.d/, if not _queue_entries(path) at line 575 always fails, so every later acquire_file_lock on that target skips the fast path and pays for a queue entry. _UNKNOWN entries behaved the same way before this PR, so this isn't a regression. It may be worth a line in the _unchecked docstring, or a follow-up that lets the fast path ignore entries older than LOCK_TIMEOUT_SECONDS.

Checked and fine

  • Removing _SPENT is safe. An unlocked entry already counted as not ahead (_DEAD); the only change is that it is probed again on each poll. test_an_earlier_entry_seen_before_its_owner_locked_it_still_counts still covers the not-yet-locked case.
  • The stale cutoff is LOCK_TIMEOUT_SECONDS (10 s). The only production caller that sets its own timeout is hub/installer.py:397 with timeout=0.5, so no in-tree live waiter can lose its place to the age rule.
  • Stale entries go neither into unknown nor into dead. They stay out of the timeout message and are never deleted, which is the intended behaviour.

Strengths

  • The margin-based timing test cancels out runner overshoot and load instead of widening the bound. Randomising the release point inside the poll makes the medians meaningful.
  • Every new test was shown to fail when its fix is reverted, including the _poll_interval cap and the registry skipping dot-named folders.
  • The module docstring now names the sub-millisecond window where first-come, first-served ordering can slip, instead of claiming strict FIFO.

kovtcharov and others added 4 commits October 21, 2026 06:33
… name them

An entry this user cannot check (another user's, or on NFS) was aged by this
machine's clock against the file server's mtime, so a client an hour behind
blocked every waiter for an hour. Once aged out it was skipped silently, and
since nobody can delete it the queue never emptied, so every later lock lost
its fast path.

- Age is now the server's time from both sides: the waiter's own entry mtime
  plus the monotonic time since it joined, against the stuck entry's mtime.
  Only a waiter that has no entry of its own, in a read-only folder, still
  uses this machine's clock.
- An aged-out entry is logged once per process, naming the file and saying
  it can be deleted if no other user's GAIA is running. It stays in the
  timeout message's list, and the fast path looks past it.
- The module docstring lists all three ways queue order can slip, without a
  timing claim.
…g-window slip

A waiter polling past an aged-out queue entry now has a test that it is named exactly once: removing the dedupe made it warn 76 times without any test failing. The module docstring also notes that an entry written just after an arrival listed the queue can be overtaken, a window as long as a directory listing on NFS.
# Conflicts:
#	tests/unit/test_file_lock.py
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

The stale-entry note is handled: the fast path now looks past an entry that has aged out, so it no longer slows every later lock. The entry is also named in a warning once per process. test_an_aged_out_entry_is_reported_once_and_the_fast_path_returns covers it. Also merged latest main; the only conflict was main's deflake of the old hand-off test, which this PR replaces with the margin test.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 24, 2026
Merged via the queue into amd:main with commit 2e8f576 Oct 24, 2026
33 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/file-lock-followup branch October 24, 2026 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants