Skip to content

feat: prevent double backfill - #11377

Merged
jif-oai merged 3 commits into
mainfrom
jif/prevent-double-backfill
Feb 11, 2026
Merged

jif-oai merged 3 commits into
mainfrom
jif/prevent-double-backfill

Conversation

@jif-oai

@jif-oai jif-oai commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a DB-backed lease to prevent duplicate .sqlite backfill workers from running concurrently.

What changed

  • Added StateRuntime::try_claim_backfill(lease_seconds) that atomically claims backfill only when:
    • backfill is not complete, and
    • no fresh running worker currently owns it.
  • Updated backfill_sessions to use the claim API and exit early when another worker already holds the lease.
  • Added runtime tests covering:
    • singleton claim behavior,
    • stale lease takeover,
    • claim blocked after complete.
  • Set backfill lease to 900s in production and 1s in tests.

Why

This avoids duplicate backfill work and reduces backfill status churn under concurrent startup, while preserving
current best-effort fallback behavior.

@jif-oai

jif-oai commented Feb 10, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 15652365a1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

DB_ERROR_METRIC,
1,
&[("stage", "backfill_state_mark_running")],
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Badge Retry backfill instead of exiting on an active lease

Returning here makes lease contention a one-shot decision, but init_if_enabled only spawns backfill_sessions once during startup (codex-rs/core/src/state_db.rs, lines 68-75). If a previous process crashed after setting status=running, a restart within the 900s lease window will hit !claimed and exit permanently, leaving backfill incomplete for the lifetime of that process and keeping DB-backed read paths unavailable until another restart.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes this is normal. We don't want a worker for this, we can live without the backfill

SET status = ?, updated_at = ?
WHERE id = 1
AND status != ?
AND (status != ? OR updated_at <= ?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge Refresh the lease during long-running backfill work

The stale-worker check is based solely on updated_at <= lease_cutoff, but this timestamp is only refreshed at claim time and at batch checkpoints; if processing one batch takes longer than the 900s lease, another process can successfully claim and start a duplicate backfill while the first worker is still running. This reintroduces the concurrent-worker behavior under large/slow backfills unless the lease is heartbeated more frequently.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No chance this is the case unless you're on an intel Pentium on Windows 95

@jif-oai
jif-oai enabled auto-merge (squash) February 11, 2026 00:23
@jif-oai
jif-oai merged commit 87bbfc5 into main Feb 11, 2026
37 of 38 checks passed
@jif-oai
jif-oai deleted the jif/prevent-double-backfill branch February 11, 2026 00:24
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants