Repository navigation
fix(worker): #173 items 5, 6, 8 — build constraint, joined Serve goroutines, activity-based drain grace - #229
Conversation
…ssoctl#173 item 8) runner.go referenced syscall.SysProcAttr{Setpgid} and syscall.Kill directly, so a non-unix build failed inside that file with two errors about a struct literal — "unknown field Setpgid" and "undefined: syscall.Kill" — neither of which names the actual cause, an unsupported platform. Split both calls behind //go:build unix. The !unix side is a refusal rather than a second implementation: process-group isolation is what makes abort and timeout stop a command's grandchildren, and a runner that cannot reliably kill what it spawned is not a degraded mode worth shipping. Run now returns "remote-worker is unix-only: <GOOS> has no equivalent of the process-group isolation (Setpgid/Kill) that abort and timeout depend on" before spawning anything. The worker ships as a Linux container, so nothing else changes. Guarded by TestModuleCrossCompilesForNonUnix, which cross-compiles the module for GOOS=windows (~1.1s) rather than asserting on the constraint's text: the property worth pinning is that the build succeeds and the platform is reported in words at run time, and a comment claiming the tag exists cannot go stale in a way that test misses. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…l#173 item 5) Tests launched Serve with `go func() { _ = s.Serve(...) }()` and simply ended. A teardown deadlock, or a WaitGroup that never reached zero, therefore left a goroutine wedged forever while `go test` still printed PASS. Measured rather than assumed. Deleting the cancelConn() call that precedes close(queue) in Serve — so the heartbeat producer never returns and wg.Wait() never reaches zero — left 14 of loop_test.go's 15 tests passing, one failing. With the join registered, the same fault fails 17. Two surfaces, both fixed: - loop_test.go gets serve(), which starts Serve and registers the cleanup that closes the stream and waits for the return. It subsumes the trailing st.close() each test used to end with, so a body now closes the stream only when the close itself is under test. fakeStream.close() becomes idempotent (sync.Once) because the cleanup always closes too. - contract_test.go's attachCancellable already had a `served` channel and a cleanup that cancelled — but never joined. Verified with a second fault that cancellation cannot mask (close(outbound) removed): all 11 contract tests now catch it; before, they passed. Also corrects a claim about TestSendFailureDoesNotWedgeTeardown, which reads as though it observes teardown generally. It does not: a failing Send makes the sender call cancelConn() itself, which ends the heartbeat producer as a side effect, so it passes even with the missing-cancelConn deadlock injected. Its real coverage is the sender's drain-after-failure behaviour, and the comment now says so. Only ONE test in the file genuinely observed teardown, not two. live_test.go (SH_LIVE_RELAY gate) joins too. Its cleanup cancels first rather than relying on the existing t.Cleanup(cancel): cleanups run LIFO, so that one runs after and the wait would have deadlocked. No production code changed — loop.go is byte-identical to main. Session suite runtime is unchanged (9.8s vs 10.7s baseline). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…soctl#173 item 6) The drain watchdog waited a flat 2s after runCtx ended and then force-closed the pipe readers. That cannot tell a WEDGED drain from a merely SLOW one, so a pipe holder still producing output steadily was cut off mid-flight and its trailing output discarded. Measured, not argued. A python3 writer that os.setsid()s out of the process group (so the SIGKILL at timeout misses it) and emits a tick every 400ms delivered 7 of 10 ticks, truncated at exactly 3.01s = 1s timeout + 2s grace. The last three were legitimate output the command produced. drainGrace is now a QUIET period: every read that returns bytes restarts it, so a drain making progress is left alone. Progress is a single atomic counter that drain bumps before delivery — before, so the record is of when the pipe produced bytes, not when a slow sink finished accepting them. The reset is capped by a new drainCeiling of 30s. Unbounded, a holder trickling one byte per second would reset the grace forever and pin both a pool slot (1 of MaxConcurrent) and up to BufferCap of buffer — a slower version of the wedge the watchdog exists to prevent. 15x the grace is far longer than any legitimate trailing flush and negligible beside the harness's 30-minute exec ceiling. The watchdog moves out of Run into a package-level watchDrain so both behaviours are testable without a child process. The ceiling especially: end-to-end it would mean waiting 30 real seconds, whereas the unit tests drive it at 50ms/500ms. Verified by neutering the ceiling and watching only the ceiling test fail. Coverage: TestSlowDrainKeepsTrailingOutput end-to-end (all 10 ticks, 4.4s), plus five watchDrain unit tests — closes when quiet, holds off while reads progress, closes at the ceiling anyway, leaves a healthy exec's pipes alone, and never closes before runCtx ends. The pre-existing wedged-holder test still passes; it is the quiet path and is unaffected. No wire or spec change: the watchdog is internal and appears in no spec. worker-deployment.test.ts (which parses BufferCap out of this file) is 7/7 green. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
pdettori
left a comment
There was a problem hiding this comment.
Review
Reviewed the full diff, traced watchDrain's state machine, the atomic.Uint64 publication in drain, timer Reset-after-drain safety, the defer/LIFO ordering of close(watchdogDone) against the runCtx cancel, and the serveResult.err-via-channel-close happens-before. No correctness bugs found, and the unix/!unix halves are signature-identical with exhaustive tag coverage. Three non-blocking comments below.
Verified against main rather than taken on trust: time was already imported in contract_test.go; go 1.25.0 supports the unix tag; nothing outside runner.go/runner_test.go references drainGrace, so "no spec amendment" holds. Items 5, 6 and 8 match #173's text and items 1, 2, 4, 9, 10 do remain open, so "does not close #173" is accurate. Both new environment-gated skips do run in CI — the Go tests execute in the proto job on ubuntu-latest, which ships both go and python3, so neither is a silent no-op.
Also checked whether append(cmd.Environ(), "GOOS=windows", ...) could be silently ignored when GOOS is already exported: it cannot, os/exec dedupes in favor of later values.
Summary
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Go (remote-worker/internal/exec, remote-worker/internal/session), tests, commit/PR conventions, security
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed-off: yes
CI status: passing (12/12)
No must-fix findings. Posted as a COMMENT review rather than an approval only because GitHub does not permit approving one's own PR — this still needs a second maintainer's review to unblock the merge.
| // (go.dev/issue/23019: the os/exec Cancel/WaitDelay mitigation explicitly | ||
| // excludes *Pipe() users). | ||
| // | ||
| // It is a QUIET period, not a wall clock: every read that returns bytes restarts |
There was a problem hiding this comment.
suggestion — "every read that returns bytes restarts it" overstates the precision of the mechanism.
The timer is re-armed only when it expires and progress is observed at that moment, not on each read. So a read arriving just after a window opens extends the wait by a further full grace, and the pipes close somewhere between 1x and 2x grace after the last byte — a worst-case teardown of ~4s, not 2s.
That is the right behaviour, and the bound is what matters rather than the exact instant. But this const doc is the number someone reasoning about teardown latency will read, and watchDrain's own doc already states it accurately ("a pump got bytes since the last check"). Worth matching the two.
There was a problem hiding this comment.
Fixed in afc83eb. The doc now states the sampling behaviour and the bound it produces: progress is read when the timer expires, not per read, so a read inside a window re-arms for a further full grace and the close lands between 1x and 2x drainGrace after the last byte — up to ~4s. It also says why sampling is the deliberate choice (one atomic load per window rather than a wakeup per read).
Went one step further and pinned the number with a test, since a bound living only in a comment is precisely what drifts: TestWatchDrainClosesAtTwiceGraceAfterTheLastRead asserts the close cannot land before 2x grace. Confirmed it discriminates rather than merely passing — swapping watchDrain for a finely-polling version that reacts to each read as it happens makes it fail, closing one grace after the lone read.
| // The bound still has to exist. A drain that keeps resetting forever would hold | ||
| // a pool slot and its buffer indefinitely, which is the hazard the watchdog was | ||
| // added for in the first place. | ||
| if elapsed := time.Since(start); elapsed > 20*time.Second { |
There was a problem hiding this comment.
nit — this bound can never fire, so it does not check what its comment claims.
The writer exits at ~3.6s and closes the pipe, so both pumps hit EOF and wg.Wait() returns naturally; the watchdog never reaches a force-close on this path, with or without a ceiling. Removing drainCeiling entirely would leave this assertion just as green.
The ceiling is genuinely covered — by TestWatchDrainClosesAtCeilingDespiteProgress, which is exactly why that test exists. So either drop this bound and point at its real guard, or keep it but soften "The bound still has to exist" to say it is a smoke check on total runtime rather than coverage of the ceiling.
There was a problem hiding this comment.
Correct, and verified before removing it rather than taken on trust: neutering drainCeiling and re-running this test gave 4.38s and PASS, identical to the run with the ceiling in place. The writer exits at ~4s and closes the pipe, so both pumps hit EOF and Run returns naturally — the watchdog never reaches a force-close on this path.
Removed in afc83eb, with a comment naming TestWatchDrainClosesAtCeilingDespiteProgress as the ceiling's real guard and saying explicitly why no bound is asserted here. Took the "drop it" option rather than the "soften the wording" one: an assertion that cannot fail is worse than no assertion, because it advertises coverage that does not exist.
Also corrected an inaccuracy of mine in the same comment block while there — it said "the last two are lost" where the measured run lost three (7 of 10, truncated at 3.01s).
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| t.Fatalf("GOOS=windows build failed (%v); the unix-only syscalls are not behind a build constraint:\n%s", err, out) | ||
| } | ||
| if runtime.GOOS == "windows" { |
There was a problem hiding this comment.
nit — the guard sits after the build, so on a Windows host this does a full native GOOS=windows build (the cross-compile the test is named for becomes a no-op) and only then fails. Hoisting it above the LookPath makes the refusal immediate and the intent read in order.
There was a problem hiding this comment.
Fixed in afc83eb — the runtime.GOOS check now sits above the LookPath, so a windows host refuses immediately instead of paying for a full native build to reach the same conclusion. Reads in order now too.
… bound, drop a dead assertion
Three review comments, all non-blocking, all correct.
1. drainGrace's doc claimed "every read that returns bytes restarts it", which
overstates the mechanism. Progress is SAMPLED when the timer expires, not
observed per read, so a read inside a window re-arms for a further full grace
and the force-close lands between 1x and 2x grace after the last byte — up to
~4s, not 2s. The doc now says that, matching watchDrain's own doc ("since the
last check"), and explains why sampling is the deliberate choice.
Also pinned by a new test rather than left as prose, since a number that lives
only in a comment is exactly what drifts: TestWatchDrainClosesAtTwiceGrace-
AfterTheLastRead asserts the close cannot land before 2x grace. Verified it
discriminates — against a finely-polling watchdog (reacting to each read as it
happens) it fails, closing one grace after the lone read.
2. TestSlowDrainKeepsTrailingOutput's elapsed-time bound could never fire, so it
did not check what its comment claimed. The writer exits at ~4s and closes the
pipe, so both pumps reach EOF and Run returns naturally; the watchdog never
force-closes on that path. Confirmed by neutering drainCeiling and re-running:
4.38s and PASS, identical to before. Removed, with a comment pointing at the
ceiling's real guard, TestWatchDrainClosesAtCeilingDespiteProgress. An
assertion that cannot fail is worse than none — it advertises absent coverage.
Fixed a related inaccuracy in the same comment while there: it said "the last
two are lost" where the measured run lost three (7 of 10, truncated at 3.01s).
3. TestModuleCrossCompilesForNonUnix checked runtime.GOOS AFTER the build, so on
a windows host it ran a full native build — the cross-compile it is named for
becoming a no-op — before failing. Hoisted above the LookPath.
No production behaviour changes: the only runner.go edit is a comment. Full Go
suite green, gofmt and vet clean.
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Three of the smaller follow-ups from #173, taken as one batch because they are all Go in
remote-worker/and share a toolchain. Does not close #173 — items 1, 2, 4, 9 and 10 remain open, and item 1 is the top correctness item on that list.One commit per item, each independently revertable.
item 8 — the unix-only syscalls now sit behind a build constraint
runner.goreferencedsyscall.SysProcAttr{Setpgid}andsyscall.Killdirectly, so a non-unix build failed inside that file with two errors about a struct literal:Neither names the actual cause. Both calls move behind
//go:build unix.The
!unixside is a refusal, not a second implementation: process-group isolation is what makes abort and timeout stop a command's grandchildren, and a runner that cannot reliably kill what it spawned is not a degraded mode worth shipping.Runreturns before spawning anything.Note this goes one step past the item's own "just the tag" — a bare tag would have turned the two errors into
build constraints exclude all Go files, which is better located but still not a clear message.Guarded by
TestModuleCrossCompilesForNonUnix, which cross-compiles the module forGOOS=windowsin ~1.1s rather than asserting on the constraint's text.item 5 — every session test now joins the
ServegoroutineTests launched
Serveand simply ended, so a teardown deadlock left a goroutine wedged forever whilego testprinted PASS.Measured rather than assumed. Deleting the
cancelConn()that precedesclose(queue)inServe— so the heartbeat producer never returns andwg.Wait()never reaches zero — leaves 14 ofloop_test.go's 15 tests passing. With the join registered, the same fault fails 17.Two surfaces:
loop_test.gogetsserve(), which startsServeand registers the cleanup that closes the stream and waits for the return. It subsumes the trailingst.close()each test used to end with, so a body now closes the stream only when the close itself is under test.fakeStream.close()becomes idempotent.contract_test.go'sattachCancellablealready had aservedchannel and a cleanup that cancelled — but never joined. Verified with a second fault that cancellation cannot mask (close(outbound)removed): all 11 contract tests catch it; before, they passed.Corrects a claim in passing.
TestSendFailureDoesNotWedgeTeardownreads as though it observes teardown generally. It does not — a failingSendmakes the sender callcancelConn()itself, ending the heartbeat producer as a side effect, so it passes even with the missing-cancelConndeadlock injected. Its real coverage is the sender's drain-after-failure behaviour, and the comment now says so. Only one test in that file genuinely observed teardown, not two.No production code changed by this commit —
loop.gois byte-identical tomain.item 6 — the drain grace is activity-based, with a ceiling
The watchdog waited a flat 2s after
runCtxended, then force-closed the pipe readers. That cannot tell a wedged drain from a merely slow one, so a pipe holder still producing output steadily was cut off and its trailing output discarded.Measured: a
python3writer thatos.setsid()s out of the process group (so the SIGKILL at timeout misses it) emitting a tick every 400ms delivered 7 of 10 ticks, truncated at exactly 3.01s = 1s timeout + 2s grace. The missing three were legitimate output.drainGraceis now a quiet period — every read returning bytes restarts it, tracked by one atomic counter thatdrainbumps before delivery, so the record is of when the pipe produced bytes rather than when a slow sink finished accepting them.The reset is capped by a new
drainCeilingof 30s. Unbounded, a holder trickling one byte per second resets the grace forever and pins both a pool slot and up toBufferCapof buffer — a slower version of the wedge the watchdog exists to prevent. 15× the grace is far longer than any legitimate trailing flush and negligible beside the harness's 30-minute exec ceiling.The watchdog moves out of
Runinto a package-levelwatchDrain, so the ceiling is testable at 50ms/500ms instead of waiting 30 real seconds. Verified by neutering the ceiling and watching only the ceiling test fail.No wire or spec change — the watchdog appears in no spec, so this needs no §8 amendment.
Verification
Each fix was watched failing before it was written, including both injected faults, which were removed before commit.
go test -race ./...(remote-worker)go vet ./...·gofmt -l .make test(whole repo)make typecheckmake lintworker-deployment.test.ts(parsesBufferCapout ofrunner.go)New coverage: 1 cross-compile test, 1 end-to-end trailing-output test, 6
watchDrainunit tests. The pre-existing wedged-holder test is unaffected — it is the quiet path.Review round (
afc83eb)Three non-blocking comments, all correct, all addressed:
drainGrace's doc overstated the mechanism. Progress is sampled when the timer expires, not observed per read, so the force-close lands between 1× and 2× grace after the last byte — up to ~4s, not 2s. Doc corrected, and the bound is now pinned byTestWatchDrainClosesAtTwiceGraceAfterTheLastReadrather than left as prose. Verified that test discriminates: a finely-polling watchdog fails it.drainCeilingand getting an identical PASS. Removed, with a pointer to the ceiling's real guard. Also fixed a wrong number in that comment ("last two" → three).runtime.GOOSwas checked after the build, so a windows host did a full native build before refusing. Hoisted above theLookPath.runner.go's only change in that commit is a comment — verified by diff.Cost:
internal/execgoes from 6.7s to 15.0s (the end-to-end trickle test is 4.4s of that, by design — it waits out a real writer). The session suite is unchanged at ~9.8s.Assisted-By: Claude (Anthropic AI) [email protected]