Skip to content

fix(store): make an interrupted git call wait for its child before finalizers run (open-only) - #216

Merged
laithalsaadoon merged 3 commits into
mainfrom
fix/store-interrupt-waits-for-git
Oct 20, 2026
Merged

laithalsaadoon merged 3 commits into
mainfrom
fix/store-interrupt-waits-for-git

Conversation

@laithalsaadoon

Copy link
Copy Markdown
Contributor

Open for Laith: this changes what packages/store does at runtime, which sits outside the dependency and flake grant.

The hazard, exposed by the store flake fixed in #214. spawnGit (packages/store/src/git.ts) passes an abort signal to execFile, so interrupting the fiber sends SIGTERM to the git child. A signal is only a request: the child is alive for a moment afterwards, and if it is git mv or git add it still holds .git/index.lock. Interruption proceeded straight to the caller's finalizers, and the write compensation in store.ts opens with git reset -q -- <paths>; against the still-held lock that exits 128, restore fails as a whole (only logged), and the half-done mv stays on disk with nothing to put the memory back. An MCP client that times out mid-archive takes exactly this path in production.

The fix. The Effect.callback register in spawnGit now returns a cleanup effect, awaitChildExit(child), that resolves on the child's exit (or close, for a spawn that never started; immediately if exitCode/signalCode is already set). At effect 4.0.0-rc.115, Effect.callback runs and awaits that returned effect on an interrupting cause before the cause propagates to outer finalizers (internal/effect.js asyncFinalizer: hasInterrupts(cause) ? flatMap(cleanup, () => failCause(cause)) : failCause(cause), run uninterruptibly), and Fiber.interrupt returns only after that chain completes. exit rather than close alone because close also waits for stdio pipes to drain, which a grandchild (hook, editor, credential helper) can hold open after git itself is dead and its locks are gone. The abort signal stays, so a git that ignores SIGTERM is still killed rather than waited on forever.

The test (packages/store/tests/git.test.ts, "interruption waits for the git child"). It creates .git/refs/heads/<branch>.lock by hand, runs git -c core.filesRefLockTimeout=-1 commit -a -m ... through the port in a forked fiber (that holds index.lock and retries the ref lock indefinitely), captures the child through the child_process diagnostics channel the file already uses, waits until index.lock exists, interrupts the fiber, and then asserts with no clock involved: exitCode/signalCode set, process.kill(pid, 0) throws ESRCH, index.lock absent, and git reset -q -- <path> succeeds. With the wait removed the case fails 3 of 3 runs on the exit-code assertion. Five consecutive runs pass; the whole store suite passes (191 tests); tsc and biome clean.

Caveats worth a look: the wait relies on git honoring SIGTERM (its lockfile handler removes .lock files on SIGTERM and re-raises); there is no SIGKILL escalation after a grace period, and a git stuck in uninterruptible I/O would hang the finalizer, which SIGKILL would not fix either. The test assumes the files ref backend and core.filesRefLockTimeout=-1 meaning "retry indefinitely" (true on git 2.50.1); under a reftable default the wait would time out loudly at 10 s rather than pass vacuously.

Local gate result in the first comment.

…nalizers run

The abort signal handed to execFile sends SIGTERM when the fiber is
interrupted, but the child is still alive for a moment afterwards, and if it
is `git mv` or `git add` it still holds .git/index.lock. Interruption then
proceeded straight to the caller's finalizers, and store.ts's compensation
opened with `git reset -q -- <paths>` against a lock that had not been
released: exit 128, the whole restore abandoned, and the half-done mv left on
disk with nothing to put the memory back. An MCP client timeout landing
mid-archive is exactly this path in production; the same race surfaced as a
CI flake in store.test.ts on 2026-09-23 (fixed on the test side in #214).

spawnGit's Effect.callback register now returns a cleanup effect that resolves
on the child's exit (or close, for a spawn that never started), which
Effect.callback runs and awaits on an interrupting cause before the cause
propagates (effect 4.0.0-rc.115 internal/effect.js asyncFinalizer). The
signal stays, so a git that ignores SIGTERM is still killed.

Test: blocks a real `git commit -a` on a hand-made refs/heads/<branch>.lock
with core.filesRefLockTimeout=-1, interrupts the fiber, and asserts the pid is
gone and index.lock absent by the time Fiber.interrupt returns, then that the
compensation's `git reset` succeeds. Removing the wait fails it 3 of 3.
@laithalsaadoon

Copy link
Copy Markdown
Contributor Author

Local gate on this head (993f3e8): mise run check green, 68 of 68 tasks in 206 s. Store suite alone: 7 files, 191 tests passed; the new case passed five consecutive runs and fails 3 of 3 with the wait removed. tsc --noEmit -p tsconfig.check.json and biome check src tests clean. Commit signature verified by the GitHub API. Left open for Laith: a production change in packages/store, outside the dependency and flake grant.

Copilot AI lite review requested due to automatic review settings September 23, 2026 15:19

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

One conflict, in the imports of packages/store/src/git.ts: main moved
GitFailure to @memhtml/contracts/errors and added withCheckoutLock, and
this branch added the ChildProcess type. The resolution keeps main's
imports plus `type ChildProcess`, and drops the branch's `Schema` import,
which only the moved GitFailure class used.
@laithalsaadoon

laithalsaadoon commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Merged main (bb6017b, 0.16.0 plus #292) into this branch with a merge commit (e64b967), so the PR is mergeable again.

  • One conflict, in the imports of packages/store/src/git.ts. Main moved GitFailure to @memhtml/contracts/errors and added withCheckoutLock; this branch added type ChildProcess. The resolution keeps main's imports plus type ChildProcess, and drops this branch's Schema import, which only the moved GitFailure class used. The awaitChildExit cleanup and its tests merged unchanged.
  • mise run check on the merged tree: 75/75 tasks, 210 s; packages/store/tests/git.test.ts passed 35/35.

Still open-only: it changes what spawnGit does on interruption. The doc comment's account of the failure still matches main: store.ts's compensation opens with git reset -q -- <paths> (packages/store/src/store.ts:533 at bb6017b).

Brings the branch current with main before the merge Laith approved on
2026-10-20 (bench card 63). Main gained #299, #300, #301, #304, #305 and
#297 (Effect 4.0.5) since the last merge; the branch's two files,
packages/store/src/git.ts and packages/store/tests/git.test.ts, are
untouched on main in that range.
@laithalsaadoon
laithalsaadoon merged commit ed1b826 into main Oct 20, 2026
11 checks passed
laithalsaadoon added a commit that referenced this pull request Oct 20, 2026
…fix) into the API doc-shapes branch

Brings the branch current with main before the merge Laith approved on
2026-10-20 (bench card 63). Main gained #297 and #216 since the last
merge; none of this branch's eight files changed in that range.
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.

2 participants