carlos / platform Public

demobuild: give the output turn back when validation or cleanup panics

Run took the process's one output turn and released it only on explicit
error returns, and Worker.Build's deferred scratch cleanup ran with the
output (and its turn) in hand. A panic in either skipped every release;
net/http recovers the panic and the service lives on, so every later
build waited out its deadline. debug/elf panics on a build's own bytes
(e_shnum 0 with SHN_XINDEX, an sh_link past sh_size sections).

Run now defers the release until the output is handed over; Build's
cleanup defer releases the turn and poisons the worker if cleanup
panics. Astra R1 P1, Fable R1 P2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 22a4efb15690599ace32676e269b059827c41363 parent fa8e9f7
3 files changed, +99 −5
  • internal/demobuild/runner.go +11 −4
  • internal/demobuild/transfer_test.go +74 −0
  • internal/demobuild/worker.go +14 −1
diff --git a/internal/demobuild/runner.go b/internal/demobuild/runner.go
index 7062838ab..ffa22bdfb 100644
--- a/internal/demobuild/runner.go
+++ b/internal/demobuild/runner.go
@@ -203,23 +203,30 @@ func Run(ctx context.Context, workDir string, lock Lock) (BuildOutput, error) {
return out, buildFailure("runUnit.start-wait", runErr)
}
// The turn is held from here until the caller releases the output after
- // writing its response; every failure below hands it back.
+ // writing its response. Every other way out hands it back, a panic
+ // included: debug/elf runs over the build's own bytes, and net/http
+ // recovers a handler's panic and keeps the service, so a turn lost to
+ // one would stall every later build until its deadline.
held, err := AwaitOutputTransfer(ctx)
if err != nil {
return out, buildFailure("runUnit.await-transfer", err)
}
+ handedOver := false
+ defer func() {
+ if !handedOver {
+ held.Release()
+ }
+ }()
f, err := instancefs.OpenBeneath(workDir, "out/app", os.O_RDONLY, 0)
if err != nil {
- held.Release()
return out, buildFailure("runUnit.open-output", err)
}
defer f.Close()
validated, err := readOutput(f)
if err != nil {
- held.Release()
validated = BuildOutput{}
} else {
- validated.hold = held.hold
+ validated.hold, handedOver = held.hold, true
}
validated.Diagnostics = out.Diagnostics
validated.DiagnosticsTruncated = out.DiagnosticsTruncated
diff --git a/internal/demobuild/transfer_test.go b/internal/demobuild/transfer_test.go
index 459bdf16c..ca1790c52 100644
--- a/internal/demobuild/transfer_test.go
+++ b/internal/demobuild/transfer_test.go
@@ -225,3 +225,77 @@ func TestDemoBuildOutputTransferPeak(t *testing.T) {
t.Logf("GOMEMLIMIT=%q GOGC=%q: peak HeapAlloc=%d MiB, peak Sys-HeapReleased=%d MiB, maxrss before=%d MiB after=%d MiB (delta %d MiB)",
os.Getenv("GOMEMLIMIT"), os.Getenv("GOGC"), peakHeap.Load()>>20, peakSys.Load()>>20, before>>20, after>>20, (after-before)>>20)
}
+
+// shstrndxPanicELF is an AArch64 executable header whose section table
+// makes debug/elf (Go 1.26.10, file.go:596) index past its sections: e_shnum
+// 0 and e_shstrndx SHN_XINDEX send the parser to section 0, whose sh_size
+// (0xff00 sections) and sh_link (string table 0xffff) it accepts without
+// checking one against the other. The table itself is zeros.
+func shstrndxPanicELF() []byte {
+ const shoff, sections = 4096, 0xff00
+ b := arm64ELFOfSize(shoff + sections*64)
+ binary.LittleEndian.PutUint64(b[40:], shoff) // e_shoff
+ binary.LittleEndian.PutUint16(b[58:], 64) // e_shentsize
+ binary.LittleEndian.PutUint16(b[60:], 0) // e_shnum: see section 0
+ binary.LittleEndian.PutUint16(b[62:], 0xffff) // e_shstrndx: SHN_XINDEX
+ binary.LittleEndian.PutUint64(b[shoff+32:], sections)
+ binary.LittleEndian.PutUint32(b[shoff+40:], 0xffff)
+ return b
+}
+
+// panicked runs f and returns what it panicked with, or nil.
+func panicked(f func()) (p any) {
+ defer func() { p = recover() }()
+ f()
+ return nil
+}
+
+// A panic while root validates an output (here a real one, inside
+// debug/elf, from the build's own bytes) unwinds through Run. net/http
+// recovers it and the service lives on, so the turn must come back with
+// the unwinding or every later build waits out its deadline.
+func TestDemoBuildOutputTransferValidationPanicReleases(t *testing.T) {
+ ctx, dir := finishedBuild(t, shstrndxPanicELF())
+ p := panicked(func() {
+ out, err := Run(ctx, dir, testLock(t))
+ out.Release()
+ t.Logf("no panic: err=%v", err)
+ })
+ if p == nil {
+ t.Fatal("the fixture no longer panics debug/elf; this test needs another panic source")
+ }
+ if !transferFree(t) {
+ t.Fatalf("a validation panic (%v) kept the output turn", p)
+ }
+}
+
+// A panic in Worker.Build's scratch cleanup, after Run handed it an output
+// holding the turn, must give the turn back, and a later output can take
+// it. The worker is poisoned: its scratch was not proven removed.
+func TestDemoBuildOutputTransferCleanupPanicReleases(t *testing.T) {
+ w, j, key, src := workerFixture(t)
+ bctx, bdir := finishedBuild(t, arm64ELF())
+ i := bctx.Value(runContextKey{}).(*runInvocation)
+ w.run = func(ctx context.Context, _ string, lock Lock) (BuildOutput, error) {
+ return Run(context.WithValue(ctx, runContextKey{}, i), bdir, lock)
+ }
+ w.prepare = func(context.Context, string, Source) (func() error, error) {
+ return func() error { panic("cleanup") }, nil
+ }
+ wire, err := SignJob(key, j)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if p := panicked(func() { w.Build(t.Context(), wire, bytes.NewReader(src)) }); p != "cleanup" {
+ t.Fatalf("panic=%v", p)
+ }
+ if !transferFree(t) {
+ t.Fatal("a cleanup panic kept the output turn")
+ }
+ w.mu.Lock()
+ poisoned := w.poisoned
+ w.mu.Unlock()
+ if !poisoned {
+ t.Fatal("cleanup panicked and the worker was not poisoned")
+ }
+}
diff --git a/internal/demobuild/worker.go b/internal/demobuild/worker.go
index 5cfa7ff6a..b0ed86803 100644
--- a/internal/demobuild/worker.go
+++ b/internal/demobuild/worker.go
@@ -270,7 +270,20 @@ func (w *Worker) Build(ctx context.Context, wire []byte, source io.Reader) (out
cleanup, err := w.prepare(ctx, dir, src)
if cleanup != nil {
defer func() {
- if e := cleanup(); e != nil {
+ // out may hold the output turn. A cleanup that panics must not
+ // take it along, and leaves scratch nobody proved removed.
+ cleaned := false
+ defer func() {
+ if !cleaned {
+ w.mu.Lock()
+ w.poisoned = true
+ w.mu.Unlock()
+ out.Release()
+ }
+ }()
+ e := cleanup()
+ cleaned = true
+ if e != nil {
w.mu.Lock()
w.poisoned = true
w.mu.Unlock()