Repository navigation
fix(store): make an interrupted git call wait for its child before finalizers run (open-only) - #216
Conversation
…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.
|
Local gate on this head (993f3e8): |
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.
|
Merged
Still open-only: it changes what |
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.
Open for Laith: this changes what
packages/storedoes 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 abortsignaltoexecFile, 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 isgit mvorgit addit still holds.git/index.lock. Interruption proceeded straight to the caller's finalizers, and the write compensation instore.tsopens withgit reset -q -- <paths>; against the still-held lock that exits 128,restorefails as a whole (only logged), and the half-donemvstays 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.callbackregister inspawnGitnow returns a cleanup effect,awaitChildExit(child), that resolves on the child'sexit(orclose, for a spawn that never started; immediately ifexitCode/signalCodeis already set). At effect 4.0.0-rc.115,Effect.callbackruns and awaits that returned effect on an interrupting cause before the cause propagates to outer finalizers (internal/effect.jsasyncFinalizer:hasInterrupts(cause) ? flatMap(cleanup, () => failCause(cause)) : failCause(cause), run uninterruptibly), andFiber.interruptreturns only after that chain completes.exitrather thanclosealone becauseclosealso 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>.lockby hand, runsgit -c core.filesRefLockTimeout=-1 commit -a -m ...through the port in a forked fiber (that holdsindex.lockand retries the ref lock indefinitely), captures the child through thechild_processdiagnostics channel the file already uses, waits untilindex.lockexists, interrupts the fiber, and then asserts with no clock involved:exitCode/signalCodeset,process.kill(pid, 0)throwsESRCH,index.lockabsent, andgit 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);tscandbiomeclean.Caveats worth a look: the wait relies on git honoring SIGTERM (its lockfile handler removes
.lockfiles 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 andcore.filesRefLockTimeout=-1meaning "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.