Skip to content

extract: hold the per-repo rebuild lock for the whole pipeline - #1

Merged
Azeem1985 merged 1 commit into
v8from
feat/extract-lock
Aug 24, 2026
Merged

Azeem1985 merged 1 commit into
v8from
feat/extract-lock

Conversation

@Azeem1985

Copy link
Copy Markdown

What: graphify extract now takes the same advisory file lock (flock on graphify-out/.rebuild.lock) that the watcher and git-hook rebuilds already take, and holds it for the entire extract pipeline.

Why: extract and the watcher share graphify-out/. Two writers racing on one output dir interleave cache saves and clobber graph.json. With the lock, a second run waits its turn: it prints who holds the lock (PID), waits up to GRAPHIFY_LOCK_TIMEOUT seconds (default 600), and exits with a clear error instead of hanging forever behind a wedged rebuild.

Tests: tests/test_extract_lock.py (5 tests) — lock held during extract, contended-run wait, timeout exit, lock released on failure paths. All offline; the LLM/subprocess layers are mocked.

🤖 Generated with Claude Code

Two `graphify extract` runs (or an extract racing a watcher/hook rebuild)
on one graphify-out/ interleave cache saves and clobber graph.json. extract
now takes the same advisory flock _rebuild_code takes, immediately after
the output dir exists, and holds it to the end of the command. A contended
run prints the holder's PID and waits up to GRAPHIFY_LOCK_TIMEOUT seconds
(default 600) via a new bounded-wait mode on _rebuild_lock (the kernel only
offers skip or wait-forever, so the deadline is polled in userspace), then
exits 1 instead of hanging forever behind a wedged rebuild. The lock is
acquired exactly once — flock is per-open-file-description, so re-entry in
the same process would self-deadlock — and SystemExit unwinds the context
manager, so the unlink-on-release contract holds on every exit path.

Co-Authored-By: Claude Fable 5 <[email protected]>

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

Copy link
Copy Markdown

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: f7c5899aa2

ℹ️ 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".

Comment thread graphify/watch.py
Comment on lines +198 to +199
fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
acquired = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep the lock pathname bound to the acquired inode

When a timed waiter opens the lock file while another process owns it, the current owner unlocks and then unlinks that pathname during release; this waiter can subsequently acquire the still-open but now unlinked inode here. While its extraction is running, a third extract/update recreates .rebuild.lock, locks a different inode, and runs concurrently, allowing the graph/cache corruption this change is intended to prevent. Ensure a successful waiter still owns the inode currently referenced by lock_path (or change the unlink protocol) before entering the pipeline.

Useful? React with 👍 / 👎.

@Azeem1985
Azeem1985 merged commit 2373895 into v8 Aug 24, 2026
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