carlos / platform Public

instancectl: bound a build response by the signed job's deadline

The response write deadline was the 180 s build window plus 5 s even
when the signed job ended sooner, so a job with seconds left could
finish and, with a client that never reads, hold the process's output
turn for about three minutes. The write is now bounded by the earlier
of the window and the grant deadline, plus 5 s. Tests: a stalled reader
with a 300 ms grant frees the turn in ~5.3 s (failed at 8 s before), and
a client that hangs up frees it at once. Astra R1 P2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 564830a7abe8b1bdec243729819fc27e1543d355 parent 22a4efb
2 files changed, +107 −3
  • internal/instancectl/demo_build.go +5 −3
  • internal/instancectl/demo_build_transfer_test.go +102 −0
diff --git a/internal/instancectl/demo_build.go b/internal/instancectl/demo_build.go
index 41ce48751..eaa014690 100644
--- a/internal/instancectl/demo_build.go
+++ b/internal/instancectl/demo_build.go
@@ -162,13 +162,15 @@ func (s *Server) handleDemoBuild(w http.ResponseWriter, r *http.Request) {
return
}
if grantDeadline.Before(deadline) {
- _ = ctl.SetReadDeadline(grantDeadline)
+ deadline = grantDeadline
+ _ = ctl.SetReadDeadline(deadline)
}
out, err := s.DemoBuilder.Build(ctx, wire, r.Body)
// A finished output holds the process's one output turn until its
// response is written (demobuild.AwaitOutputTransfer). The write is
- // bounded so a stalled reader cannot keep the turn: the client gives up
- // five seconds after the build window too.
+ // bounded so a stalled reader cannot keep the turn past the earlier of
+ // the build window and the signed job's deadline, plus five seconds
+ // (the client gives up five seconds after the window too).
defer out.Release()
_ = ctl.SetWriteDeadline(deadline.Add(5 * time.Second))
w.Header().Set("Content-Type", "application/json")
diff --git a/internal/instancectl/demo_build_transfer_test.go b/internal/instancectl/demo_build_transfer_test.go
index 1f581c156..753b5842b 100644
--- a/internal/instancectl/demo_build_transfer_test.go
+++ b/internal/instancectl/demo_build_transfer_test.go
@@ -8,6 +8,7 @@ import (
"io"
"net"
"net/http/httptest"
+ "path/filepath"
"strings"
"sync/atomic"
"testing"
@@ -22,6 +23,8 @@ import (
type transferBuilder struct {
binary []byte
turns atomic.Int32
+ // grant is how long the signed job has left; zero means a minute.
+ grant time.Duration
}
func (b *transferBuilder) Initialize(context.Context) error { return nil }
@@ -29,6 +32,9 @@ func (b *transferBuilder) Readiness(context.Context) (demobuild.WorkerReady, err
return demobuild.WorkerReady{Capability: "anonymous-demo-v1", GOARCH: "arm64", BundleSHA256: strings.Repeat("b", 64), Slots: 4}, nil
}
func (b *transferBuilder) GrantDeadline([]byte) (time.Time, error) {
+ if b.grant != 0 {
+ return time.Now().Add(b.grant), nil
+ }
return time.Now().Add(time.Minute), nil
}
func (b *transferBuilder) Build(ctx context.Context, _ []byte, source io.Reader) (demobuild.BuildOutput, error) {
@@ -105,3 +111,99 @@ func TestDemoBuildFourFinishedBuildsTakeTurns(t *testing.T) {
}
free.Release()
}
+
+// stalledConn passes writes through and blocks every read until stall
+// closes: a client that sends its build and never reads the answer.
+type stalledConn struct {
+ net.Conn
+ stall chan struct{}
+}
+
+func (c stalledConn) Read([]byte) (int, error) {
+ <-c.stall
+ return 0, net.ErrClosed
+}
+
+// unixControl serves s on a unix socket, as root's control service is
+// served, so the kernel buffers little of a response nobody reads.
+func unixControl(t *testing.T, s *Server) string {
+ t.Helper()
+ sock := filepath.Join(t.TempDir(), "c")
+ l, err := net.Listen("unix", sock)
+ if err != nil {
+ t.Fatal(err)
+ }
+ srv := httptest.NewUnstartedServer(s.Handler())
+ srv.Listener.Close()
+ srv.Listener = l
+ srv.Start()
+ t.Cleanup(srv.Close)
+ return sock
+}
+
+// turnTaken waits until b's build holds the output turn.
+func turnTaken(t *testing.T, b *transferBuilder) {
+ t.Helper()
+ for end := time.Now().Add(5 * time.Second); b.turns.Load() == 0; time.Sleep(5 * time.Millisecond) {
+ if time.Now().After(end) {
+ t.Fatal("the build never took the output turn")
+ }
+ }
+}
+
+// A client that never reads its response holds the turn only until the
+// signed job's own deadline plus five seconds, not the 180 s window: a job
+// with little time left cannot keep every other finished build waiting.
+func TestDemoBuildStalledReaderReturnsTurnByGrantDeadline(t *testing.T) {
+ b := &transferBuilder{binary: transferELF(4 << 20), grant: 300 * time.Millisecond}
+ sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b})
+ stall := make(chan struct{})
+ done := make(chan struct{})
+ t.Cleanup(func() { close(stall); <-done })
+ c := &Client{Path: sock, Dial: func(ctx context.Context, path string) (net.Conn, error) {
+ conn, err := (&net.Dialer{}).DialContext(ctx, "unix", path)
+ return stalledConn{conn, stall}, err
+ }}
+ go func() {
+ defer close(done)
+ c.BuildDemo(context.Background(), []byte("grant"), strings.NewReader("source"))
+ }()
+ turnTaken(t, b)
+ ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second)
+ defer cancel()
+ free, err := demobuild.AwaitOutputTransfer(ctx)
+ if err != nil {
+ t.Fatal("a stalled reader kept the output turn past its grant deadline plus five seconds")
+ }
+ free.Release()
+}
+
+// A client that hangs up before reading gives the turn back at once.
+func TestDemoBuildDisconnectedReaderReturnsTurn(t *testing.T) {
+ b := &transferBuilder{binary: transferELF(4 << 20)}
+ sock := unixControl(t, &Server{DemoBox: true, DemoBuilder: b})
+ stall := make(chan struct{})
+ conns := make(chan net.Conn, 1)
+ done := make(chan struct{})
+ t.Cleanup(func() { close(stall); <-done })
+ c := &Client{Path: sock, Dial: func(ctx context.Context, path string) (net.Conn, error) {
+ conn, err := (&net.Dialer{}).DialContext(ctx, "unix", path)
+ if err == nil {
+ conns <- conn
+ }
+ return stalledConn{conn, stall}, err
+ }}
+ go func() {
+ defer close(done)
+ c.BuildDemo(context.Background(), []byte("grant"), strings.NewReader("source"))
+ }()
+ turnTaken(t, b)
+ (<-conns).Close()
+ ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
+ defer cancel()
+ free, err := demobuild.AwaitOutputTransfer(ctx)
+ if err != nil {
+ t.Fatal("the turn was still held after the client hung up")
+ }
+ free.Release()
+}