OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

CI flake: API & CLI Integration Tests fail with constant tally on fork PRs (TreeLock not released)

Open lg320531124 opened this issue 5 months ago • 4 comments

Summary

The 06. API & CLI Integration Tests job on pull_request runs for fork PRs is failing on what appears to be a test-infrastructure TreeLock leak, not a regression in any single PR's code. The same failure mode reproduces across two unrelated PRs touching disjoint modules, while main branch schedule runs of the same workflow pass in the same window. I'm opening this so the lock-release path can be looked at for the CI environment, rather than contributors burning review cycles chasing a phantom code defect.

Failure mode

06. API & CLI Integration Tests (ubuntu-24.04) finishes with a constant tally:

3 failed, 25 passed, 7 skipped, 11 errors   (in ~19min, job total ~42min)

Every ERROR at setup + FAILED traces back to one shared session-scoped fixture:

ERROR test_cli_content.py::TestContentRead::test_read - AssertionError: add-resource failed after retries:
  ov add-resource /tmp/tmpXXX.txt --to viking://resources/cli_test_51cef59c/test_pack --wait --timeout 300 -o json
  │ OpenViking returned an error: CONFLICT: Resource is busy:
  │ viking://resources/cli_test_51cef59c/test_pack.

The cli_test_<uuid> segment is a random per-session uuid (tests/cli/conftest.py:639), so this is not a fixed-name collision between runs. The 11 ERROR at setup + 3 FAILED all depend on the session-scoped test_pack_uri fixture (tests/cli/conftest.py:647), whose ov add-resource returns CONFLICT: Resource is busy after 15×@10s retries (conftest.py:488, "retries for CONFLICT/busy/network errors"). The dependent tests cascade-fail at setup, before any assertion in the PR under test runs.

Why this is not a per-PR code regression

Two of my PRs touching completely disjoint modules fail with the identical tally on the same run:

PR Module touched Head Tally
#2900 fix/grep-empty-result-fallback openviking/storage/viking_fs.py + grep tests b00e336 3 failed / 25 passed / 7 skipped / 11 errors
#2874 feat/tool-input-compaction a different module entirely d4e525e2 3 failed / 25 passed / 7 skipped / 11 errors

Two PRs touching disjoint code producing byte-identical failure counts on disjoint random uuids is strong evidence of a shared test-infra flake rather than a code defect in either branch.

Why this is not base-skew or a stable-environment failure

main schedule runs of the same workflow pass in the same window:

2026-06-30T13:34 schedule success  run=28448421744
2026-06-30T11:01 schedule success  run=28439464064
2026-06-30T08:11 schedule success  run=28430022453
2026-06-30T05:22 schedule success  run=28422273789
2026-06-30T01:40 schedule success  run=28414478820
2026-06-29T14:22 schedule success  run=28379069126
2026-06-29T11:39 schedule success  run=28369282785
2026-06-29T08:37 schedule success  run=28359360613

8 consecutive main schedule runs green, bracketing the time window where both fork PRs go red with the constant tally. So the workflow and the lock code are healthy on main; the failure is specific to the pull_request path's fixture lifecycle.

Base recency is irrelevant: #2900 is rebased onto the latest main (base is current) yet fails; main schedule runs in the same window pass. So this is not a stale-base artifact.

Root cause (source-level)

Resource is busy is raised from openviking/utils/resource_processor.py:441:

return await OwnedLockLease.acquire_tree(lock_manager, path, timeout=timeout)
...
raise ResourceBusyError(f"Resource is busy: {uri or path}", ..., retryable=True)

The exception is marked retryable=True, and the fixture retries 15×@10s — yet the lock stays held across all 15 attempts. A retryable=True lock that never recovers within the session-scoped fixture's lifetime reads like the per-resource TreeLock not being released when the owning setup step completes, so the session-scoped test_pack_uri fixture's add-resource cannot re-acquire it and the dependent tests cascade-fail at setup.

The workflow itself acknowledges the conflict surface — the step echoes Running CLI integration tests (serial to avoid resource conflicts)... — but serializing did not prevent it here.

What I tried

  • Rebased both PRs onto latest main (heads b00e336 / d4e525e2) to rule out base-skew. Same failure.
  • Triggered a fresh pull_request run via an empty commit (heads 573ca1a / 930b4725) to see if it was a transient timing hit. Pending at time of writing; if it flips green that confirms the flake, if red again the lock path is the lead.

Ask

Could someone with admin access (1) rerun the 06. API & CLI Integration Tests job on fix/grep-empty-result-fallback (#2900) and feat/tool-input-compaction (#2874), and (2) take a look at the TreeLock-release path in acquire_resource_lock (resource_processor.py ~line 441) for the pull_request CI environment? A retryable=True busy error that never recovers across 15 retries within one session-scoped fixture looks like the lock isn't released between the owning setup step and the fixture's own re-acquire.

Happy to provide more log excerpts or run anything locally if it helps narrow it down.

lg320531124 avatar Jul 01 '26 01:07 lg320531124

Dug into this; confirming it's a TreeLock leak as you suspected, and opened #2919 with a fix. Sharing the source-level mechanism in case it's useful.

Why main is green but fork PRs are red

The discriminator is secrets, not the code under test. add-resource --wait with indexing enabled acquires a per-resource TreeLock in Phase 3.5, then Phase 4 enqueues a SemanticMsg and calls OwnedLockLease.handoff() (resource_processor.py:363). handoff() stops the owning refresh loop and detaches without releasing the lock; a semantic worker is meant to adopt the handle (SemanticLockScope.resolve → OwnedLockLease.from_handoff) and release it once vectorization completes.

  • With secrets (main schedule): the worker adopts and finishes embeddings in <1s, so the lock is released almost immediately. Green.
  • Without secrets (fork PRs): the CI config writes dummy VLM/embedding keys pointed at the real Ark endpoint (.github/workflows/api_test.yml). The embedding circuit breaker opens and re-enqueues the message before the lock is ever adopted (the breaker branch in semantic_processor.py precedes the resolve()/from_handoff adoption). So nothing restarts a refresher on the handed-off handle.

Why it never recovers inside the retry window

A handed-off-but-unadopted handle isn't refreshed, so last_active_at freezes. The in-process _stale_cleanup_loop only reclaims handles older than lock_expire (300s), and acquire_tree's own on-disk stale check is also keyed to 300s. The test_pack_uri fixture retries ov add-resource for only ~150s (15×@10s). 150s < 300s, so every retry sees a still-fresh token → CONFLICT: Resource is busy → the session fixture and its dependents cascade. This is exactly the constant 3 failed / 25 passed / 7 skipped / 11 errors tally you documented, on a fresh per-session uuid, reproducing across disjoint PRs.

Fix (#2919)

Stamp handed_off_at on handoff() and clear it when a worker re-adopts the handle; _is_handle_stale reclaims a handed-off-but-unadopted handle after a short 30s grace instead of the full 300s. A late adopter re-acquires via the existing from_handoff recovery path. The with-secrets path is unaffected because adoption clears the marker within ~1s. Unit tests added under tests/transaction/test_lock_manager_staleness.py.

One tradeoff flagged for maintainer review in the PR: 30s narrows the async baton-pass window, so a handoff that legitimately sits in the semantic queue longer than the grace (heavy production backlog) could be reclaimed early and fall back to re-acquire. The grace is a single tunable constant; an alternative is to release the lock directly in the breaker re-enqueue branch. Happy to adjust based on what you prefer.

khou avatar Jul 01 '26 05:07 khou

Thanks @khou — this is a much sharper mechanism than I'd traced. Confirming the cross-PR evidence from my side lines up with your diagnosis:

  • Two of my PRs touching disjoint modules (#2900 fix/grep-empty-result-fallback, #2874 feat/tool-input-compaction) hit the identical 3 failed / 25 passed / 7 skipped / 11 errors tally on the same run, each on its own fresh per-session cli_test_<uuid> — consistent with a shared session-fixture lock leak, not a per-branch defect.
  • 8 consecutive main schedule runs of the same workflow were green in the same window the fork PRs went red — matching your "secrets is the discriminator, not the code" read.
  • Two empty-commit retries on each branch stayed red — consistent with the 150s retry window being shorter than the 300s stale threshold you identified, so every retry sees a still-fresh token.

The handed_off_at + 30s grace approach in #2919 makes sense to me. One data point that might be useful for the maintainer-review tradeoff you flagged: the failing fork-PR runs finish the whole CLI suite in ~19min before the cascade, so the handed-off handle in the CI environment sits unadopted for the full embedding-circuit-breaker duration — well beyond any 30s grace. That's the exact path #2919 reclaims, which is what we want here. The heavy-production-backlog concern is the real edge to weigh, but for the CI flake this targets, the grace is correctly sized.

Happy to re-run #2900/#2874 once #2919 lands to confirm green. Appreciate the root-cause fix.

lg320531124 avatar Jul 01 '26 06:07 lg320531124

Update: my earlier root-cause comment (TreeLock handoff not reclaimed) was wrong. The CI server log showed the lock is adopted by the semantic worker and actively refreshed, so a time-based reclaim can't touch it.

Real root cause: the embedding handler (collection_schemas.py on_dequeue) classifies 401/403 auth errors as transient and re-enqueues them forever, tripping the embedding circuit breaker. Since add-resource holds the resource tree lock and blocks --wait until pending_embedding_roots empties, the dummy Ark keys on no-secrets fork-PR CI hang the lock and the request indefinitely, so every retry hits Resource is busy. With real keys embeddings finish in <1s, hence main stays green.

Fix in #2919: route ERROR_CLASS_AUTH to terminal failure (mark failed, no re-enqueue, no breaker trip) so the tracker drains, --wait completes, and the lock releases. That PR's own 06. API & CLI Integration Tests run is green, which is the end-to-end repro of this flake.

khou avatar Jul 01 '26 18:07 khou

Thanks @khou — good catch on the correction. The auth-error path fits my cross-PR evidence more cleanly than the handoff theory did: both #2900 and #2874 are no-secrets fork runs on dummy Ark keys, so a 401/403 classified as transient and re-enqueued forever hangs add-resource --wait on both regardless of which module each PR touches — exactly the identical 3 failed / 25 passed / 7 skipped / 11 errors tally on disjoint uuids I observed, and exactly why main (real keys, embeddings finish <1s) stays green in the same window.

My earlier handoff-reclaim attribution is superseded; the cross-PR tally evidence still holds, it just points at the embedding auth path rather than the lock path. #2919 routing ERROR_CLASS_AUTH to terminal failure so the tracker drains and --wait completes is the right shape, and its own green 06. API & CLI Integration Tests run is a convincing end-to-end repro.

Happy to rerun #2900 / #2874 once #2919 lands to confirm green on the fork-PR path.

lg320531124 avatar Jul 01 '26 22:07 lg320531124