Repository navigation
Self-host: egress fence never publishes ok after lost - #2978
Conversation
The fence's kernel-event reader could latch between the main loop's latch check and its mv, leaving a fresh ok attestation after lost for one pass. lost() and publish() now share a lock, and celld/start.sh and verify-fence read the attestation before checking the latch. Self-host only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
Review follow-up: publish() failing (lock open, ENOSPC) exited under set -e without latching. Now it latches and exits; covered by an unopenable-lock test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@coderabbitai review |
Action not completed
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2936593036
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Stress result: |
Codex review: flock failing on an open lock returned success from publish(), so the fence stayed up and healthy while celld's attestation went stale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|



Summary
Self-hosting only. Hosted SAM is unaffected: every changed file lives under
deploy/selfhost/orscripts/selfhost/.The L23 egress fence (
deploy/selfhost/egress-fence/fence.sh) had two writers of its attestation: the kernel-event reader subprocess (lost()) and the main loop, which publishesok <ts>through a tmp file andmv. The main loop checked$LOSTand then published. If the reader latched between that check and themv, the attestation readokafterlostuntil the next pass, 10-100 ms later.lost()and the newpublish()hold oneflockon the fence's private tmpfs (${STATE}.lock, which no consumer can reach).publish()skips when$LOSTexists, so oncelostis written, nookis ever written. The latch never depends on the lock: if the lock file cannot be opened or taken,lost()latches anyway (fail closed), whilepublish()publishes nothing and the attestation goes stale. The old "converge any stale ok" branch is gone, because with the lock it was dead code.$LOSTis never cleared while the fence runs, so if it is absent after the read, thatokwas not published after a loss. This closes the gap that remains even with the lock: the microseconds insidelatch()between writing$LOSTand writinglost.deploy/selfhost/celld/start.shfence_problem(): the latch check moved from before the probes to after the attestation read.scripts/selfhost/verify-fence.tscheckService(): the attestation is read beforefence.sh check, and its age is measured at read time.Why this design and not the alternatives:
mvwould only shrink the window.okvisible in the file.ok… thenlost, never back). The consumer reorder makes each read sound. Both are a few lines, and busyboxflockis already in the pinned Alpine image (checked:/usr/bin/flock -> /bin/busybox).Was the main CI failure something worse?
No. Main CI run 45041955760, job 137268241147, failed in 104 ms, not at the 3 s deadline. The old test's poll saw the stub's own injected
lost, left the loop, and then read theokthat the already-admittedmvpublished. That is exactly the window described above. I traced every way the fence can be lost:kill -0→ latch;check()→ latch;okrefresh → celld stops at the 2 s max age.I found no path where
$LOSTis never written, and none wherecheck()passes after a ruleset change.Blast radius before this fix
start.sh) re-checks every 0.1 s. A staleokread in the window bought at most one extra check interval of serving, and the kernel table had already changed by then. The probe checks are independent of the attestation and still applied.pnpm selfhost:verify-fencecould report "egress fence in effect" once, for a fence that had just latched.fence.sh check) reads$LOSTand the fingerprint, not the attestation, so it was never affected.Validation
pnpm lint: eslint and prettier are clean on the changed TS.deploy/selfhost/README.mdalready failed prettier on main and is unchanged in that respect.pnpm typecheck:tsc -p scripts/selfhost/live/tsconfig.jsonis clean.pnpm test: the self-host suite tests touched here pass:fence-watchdog2/2, andverify-fence-cli,fence-verifyandfence-runtime40 passed, 3 skipped.Deterministic tests (rule 62: the test owns the ordering). Each was shown red on the old code:
scripts/selfhost/fence-watchdog.test.ts: the real reader is driven by a realnft monitorevent, released from inside the firstmvof anokpublication. A stubflockrecords lock attempts. Themvstub waits until the reader has either latched (old fence) or reached the lock (fixed fence), so the latch lands between the check and themvevery time. The test asserts that no publication ever happens while$LOSTexists and that the attestation stayslost. Results:['ok-while-lost']);publish(): red;publish(): red;ok, with no latch.flockfailing on an open lock: the fence latcheslost, publishes nothing, and exits 1. It goes red ifpublish || { lost; exit 1; }orlost()'s|| latchis removed, or ifpublishswallows a flock failure (exit 0, the Codex finding).scripts/selfhost/verify-fence-cli.test.ts"refuses an ok read just before the fence latched lost": the stubdockerlatches the fence right after the attestation read. Red on the old order (exit 0), green with the fix.scripts/selfhost/live/fence-netns.live.test.ts"does not start on a fresh ok read after the fence latched lost": the attestation is a FIFO, sostart.shblocks inside its read. The test creates the latch only once the reader has opened the FIFO, then writes a freshok. Red on the oldstart.sh(celld started), green with the fix.Dockerfile.lab,--privileged --network none, the same command as CI): 68/68 passed, flush-to-stop 72 ms. The Self-host Live CI job runs it again on this PR. The shared box app.vmtest.sam2.party was not used.Staging Verification (REQUIRED for all code changes — merge-blocking)
N/A for hosted staging. The change touches only the self-host box (
deploy/selfhost/,scripts/selfhost/), which hosted staging neither deploys nor runs. The task named the egress-fence live suite as the live gate instead. It runs real nftables and the realfence.shandstart.shin a privileged network namespace, as the fenced uid. See the Validation section.Staging Verification Evidence
Live fence-netns suite: 68/68 passed in the privileged lab, plus the Self-host Live CI job.
End-to-End Verification (Required for multi-component changes)
Data Flow Trace
nft monitor rulesetwrites to the FIFO.fence.shcallslost(), takesflock, and runslatch(): it writes$LOST, thenlostto the attestation, then the event.publish()takesflock, sees[ -f $LOST ], and publishes nothing.celld/start.shfence_problem()reads the attestation, then checks$LOST; the latch is present, so it SIGKILLs celld.verify-fence.tscheckService()reads the attestation, thenfence.sh checkfails because of$LOST, so it reports a failure.Untested Gaps
N/A: every ordering is covered by the deterministic tests above.
Post-Mortem (Required for bug fix PRs)
What broke
The fence attestation could read
okfor up to one recheck interval after the fence latchedlost, and the watchdog test flaked on main and on PR #2960.Root cause
L23 (#2917) gave the attestation two writers with no mutual exclusion, and its consumers checked the latch before reading the attestation.
Class of bug
Check-then-act across two processes writing one file (TOCTOU), plus a consumer reading two signals in the unsafe order.
Why it wasn't caught
The original test forced the race but exited its poll on the intermediate state. It accepted eventual convergence instead of asserting that no
okfollowslost, and the code comment named the window as acceptable.Process fix included in this PR
The tests now own the ordering, through stub barriers and a FIFO, rather than relying on timing. No rule change: rule 62 already covers this; the original test predated applying it.
Post-mortem file
tasks/archive/2026-11-22-egress-fence-ok-after-lost.mdSpecialist Review Evidence (Required for agent-authored PRs)
needs-human-reviewlabel added and merge deferred to human (N/A: all completed)publish(lock open, ENOSPC) exited underset -ewithout latching. Fixed in 738b9da:publish || { lost; exit 1; }, plus a new unopenable-lock test that goes red if either fallback is removed. LOW: the single-instance assumption is now documented in the comment and README. LOW, not fixed: shutdown noise in events.jsonl, a zombiekill -0, and real nft monitor formatting (partly covered by the live suite). None of these is a fail-open.||-context errexit, and no deadlock. LOW 1 (an inaccurate lock-failure comment) is fixed in 738b9da. LOW 2-4 (an accepted residual if only the reader's flock fails, the test's lock-reached proxy, and fixed absence windows) are not false-failure risks.CodeRabbit Review Evidence (Required for agent-authored PRs)
CodeRabbit Notes
Requested at 16:49 UTC through the
coderabbit-reviewlabel. Bot workflow run 45045959116 succeeded. CodeRabbit repliedReview skipped(auto reviews disabled) andReview rate limited, so it did not review. Codex reviewed instead: one P2 (a swallowed flock failure inpublish), fixed and answered on its thread.Exceptions (If any)
Agent Preflight (Required)
Classification
External References
Verified busybox
flock(fd formflock [-sxun] FD) in the pinnedalpine:3.24image by running it, including a contention check that showed the second holder waits. Used util-linuxflock(1)in the lab image.Codebase Impact Analysis
deploy/selfhost/egress-fence/fence.sh(writer),deploy/selfhost/celld/start.shandscripts/selfhost/verify-fence.ts(consumers), and the compose healthcheck (unchanged; it reads$LOSTand the fingerprint only). Tests:scripts/selfhost/fence-watchdog.test.ts,scripts/selfhost/verify-fence-cli.test.ts,scripts/selfhost/live/fence-netns.live.test.ts.Documentation & Specs
Updated
deploy/selfhost/README.md, "Egress fence and metadata broker". The public docs do not describe the attestation.Constitution & Risk Check
Principle XI: no new hardcoded tunables. The lock path derives from
SAM_EGRESS_FENCE_STATE. Risk: a lock-open failure. The latch does not depend on the lock (fail closed), and publication without the lock is skipped, so celld stops at the max age.🤖 Generated with Claude Code