Skip to content

Release uncertain lexical leases that can no longer cover a request - #602

Merged
suguanYang merged 2 commits into
mainfrom
fix/wangbinqi/release-uncertain-lexical-leases
Dec 1, 2026
Merged

suguanYang merged 2 commits into
mainfrom
fix/wangbinqi/release-uncertain-lexical-leases

Conversation

@suguanYang

@suguanYang suguanYang commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An index-use lease becomes uncertain when its holder loses track of an in-flight OpenSearch request. No code ever released one again, and an unreleased lease blocks three things for good:

uncertain index_use lease
  ├── its lexical build never completes cleanup        (cleanup.py: late-writer check)
  ├── its namespace index cannot close as empty        (coordination.py: closeEmpty)
  └── its revision's indexing input is never collected (input_collector.py: _hasConsumer)

Two sources produced them:

 LexicalCleanup.collect                 # delete-by-query for a retired/failed build
   finally
-    lease = "released" if outcome known else "uncertain"
+    lease = "released"                 # a late delete only removes this build's records;
+                                       # the build stays pending until a later delete verifies it empty

 sweepLexical (hourly)
+  _releaseSettledLeases                # uncertain AND expires_at <= now - 15 min → released
   claim and collect pending builds     # builds unblocked above complete in the same sweep
   ...

This PR also hardens test_execution_timeout_stops_native_wait_and_returns_without_replay, which failed twice on this branch's CI at 0.72 s against its 0.40 s bound. Its timed region included the first make_json_safe call, which imports numpy and pandas lazily (about 0.5 s cold). The test now calls make_json_safe({}) before the timer starts. The bound stays the same.

Writer leases (indexing and shadow backfill) still turn uncertain on an unknown write outcome. The sweep releases them only after the grace period. Writers require an active lease before every batch, so once the lease expires at most one request is still in flight, and it ends within the 30-second native request timeout.

Evidence

  • Before: production held 59 unreleased uncertain leases: 55 from delete timeouts, 4 from indexing attempts that expired. Because of them, 4 failed builds were retried every hour without ever completing, and 3 empty namespace indexes stayed open.
    After (new contract tests, PostgreSQL 16):
    test_cleanup_releases_its_lease_when_delete_outcome_is_unknown
      deleteBuild raises hasUnknownOutcome → lease "released", build still "pending"
    test_sweep_releases_settled_uncertain_writer_lease_and_completes_its_build
      lease expired 16 min ago → "released", build cleanup "completed"
      lease expired 1 min ago  → stays "uncertain"
    
    Both tests fail against main and pass with this change. All 29 tests in test_lexical_maintenance_contract.py pass. Ruff and pyright report nothing on the changed files.

Merge Danger

Door: two-way

Reverting restores the old behaviour. A lease released by the sweep is not set back to uncertain by a revert, but it would have stayed blocked forever otherwise.

Blast Radius: lexical-maintenance

Takes effect on the next worker release. The first hourly sweep after that releases the existing settled leases, then completes their builds and lets empty namespace indexes close. If a write somehow landed more than 15 minutes after its lease expired, that failed build's records could stay in the index. Retrieval reads only published builds, so they would not show up in results.

🤖 Generated with Claude Code

suguanYang and others added 2 commits December 1, 2026 09:40
An index-use lease turns uncertain when its holder loses track of an
in-flight OpenSearch request, and nothing ever released it again. Until
then its build stays pending cleanup, its namespace index cannot close as
empty, and its revision's indexing input is retained.

- Build cleanup now always releases its own lease. A delete with an
  unknown outcome can only remove the retired or failed build's records,
  and the build stays pending until a later delete verifies it is empty.
- The hourly maintenance sweep first releases uncertain leases that
  expired more than 15 minutes ago. Writers check their lease before every
  batch, so after expiry at most one request is still in flight, and it
  ends within the 30-second request timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The failure transition records audit metadata through make_json_safe,
which imports numpy and pandas on first use. When nothing earlier in the
process had loaded them, that import (about half a second) landed inside
the measured region and exceeded the 0.40-second bound. Run make_json_safe
once before the timer starts so the test measures cancellation only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@suguanYang
suguanYang merged commit 8fccc9d into main Dec 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant