oficina / equipo Public

Mac slice F wb fix 3 #3: no lost wakeup for a held report

Whole-branch review round 3, finding 3. Below 37 the sender could
classify a cancellation report as unclassified, the reader could then
accept its Checks dispatch before the sender noted the hold, and
neither side asked for a rewind: the report waited for the next
dispatch or a reconnect. The link now counts accepted dispatches; the
sender reads the count before it classifies, and when it notes a
hold after the count moved it rewinds and notifies itself. A test
seam (heldHook) pauses the sender between the two.

Regression: TestWorkerHeldReportRaceWithDispatch. Mutations: the
count ignored, a dispatch not counted; each fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 9478490ffecb5c02fc501425a65db69a1b3ce67e parent 70724b9
2 files changed, +124 −0
  • internal/runner/thin/link.go +19 −0
  • internal/runner/thin/wbfix3_link_test.go +105 −0
diff --git a/internal/runner/thin/link.go b/internal/runner/thin/link.go
index 16a0804b..d7eb7e16 100644
--- a/internal/runner/thin/link.go
+++ b/internal/runner/thin/link.go
@@ -130,6 +130,11 @@ type Link struct {
// outbox was last offered again; rewind: offer it again from the
// start, on this connection (a dispatch may have classified it).
heldStep, rewind bool
+ // dispatches counts accepted dispatches: a hold noted after one that
+ // came while its report was being classified rewinds by itself.
+ dispatches int64
+ // heldHook runs between holding a step report and noting it (tests).
+ heldHook func()
}
// Spoken is the protocol of the current connection (0 when disconnected)
@@ -524,11 +529,24 @@ func (l *Link) send(ctx context.Context, c *websocket.Conn, f wire.Frame) error
// the outbox for a connection that speaks it.
return nil
}
+ l.mu.Lock()
+ gen := l.dispatches
+ l.mu.Unlock()
if hold, err := l.State.holdBelow37(ctx, f, l.WorkerOnly || role == wire.RoleWorker, proto); hold {
// Nor of its Engineering, which only 37 places (holdBelow37).
+ if l.heldHook != nil {
+ l.heldHook()
+ }
l.mu.Lock()
l.heldStep = true
+ // A dispatch accepted since the classification began may have
+ // classified it, and saw no hold to rewind for: rewind now.
+ again := l.dispatches != gen
+ l.rewind = l.rewind || again
l.mu.Unlock()
+ if again {
+ l.Notify()
+ }
if err != nil && l.Logger != nil {
l.Logger.Warn("a step report kept for protocol 37: it could not be classified", "frame", f.ID, "err", sanitize(err))
}
@@ -886,6 +904,7 @@ func (l *Link) read(ctx context.Context, c *websocket.Conn) (time.Duration, erro
l.mu.Lock()
held := l.heldStep
l.heldStep, l.rewind = false, l.rewind || held
+ l.dispatches++
l.mu.Unlock()
if held {
l.Notify()
diff --git a/internal/runner/thin/wbfix3_link_test.go b/internal/runner/thin/wbfix3_link_test.go
new file mode 100644
index 00000000..5294d3f3
--- /dev/null
+++ b/internal/runner/thin/wbfix3_link_test.go
@@ -0,0 +1,105 @@
+package thin
+
+import (
+ "context"
+ "encoding/json"
+ "io"
+ "log/slog"
+ "sync"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Whole-branch fix 3, #3: below 37 the sender classifies a cancellation
+// report (not yet dispatched: unclassified, so held), and before it
+// notes the hold the Checks dispatch is accepted. The dispatch saw no
+// hold yet; the report must still go at once, not at the next dispatch
+// or reconnect. The seam pauses the sender between classifying and
+// noting the hold.
+func TestWorkerHeldReportRaceWithDispatch(t *testing.T) {
+ inst := newFakeInstance(t)
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+ st, err := OpenState(ctx, t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { st.Close() })
+ link := &Link{Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), Origin: inst.url, Token: "token", State: st, Role: wire.RoleWorker, WorkerOnly: true, Hello: func() wire.Hello {
+ return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}}
+ }}
+ paused, release := make(chan struct{}), make(chan struct{})
+ var once sync.Once
+ var released sync.Once
+ t.Cleanup(func() { released.Do(func() { close(release) }) })
+ link.heldHook = func() {
+ once.Do(func() {
+ close(paused)
+ <-release
+ })
+ }
+ steps := &StepEngine{State: st, Notify: link.Notify, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport {
+ return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepDone, Termination: true, Findings: []wire.Finding{}}
+ }}
+ link.Steps = steps
+ if err := steps.Start(ctx); err != nil {
+ t.Fatal(err)
+ }
+ done := make(chan error, 1)
+ go func() { done <- link.Run(ctx) }()
+ t.Cleanup(func() { cancel(); <-done; steps.Wait() })
+ c := <-inst.conns
+ read(t, c, wire.FrameHello)
+ send := func(id, kind string, body any) {
+ t.Helper()
+ f, err := wire.NewFrame(id, kind, body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ raw, _ := json.Marshal(f)
+ write(t, c, raw)
+ }
+ send("i-welcome", wire.FrameWelcome, wire.Welcome{Generation: 1, RunnerID: "RUNNER2", Protocol: wire.Protocol35, Role: wire.RoleWorker})
+ reports := make(chan wire.StepReport, 8)
+ go func() {
+ for {
+ _, raw, err := c.Read(context.Background())
+ if err != nil {
+ return
+ }
+ if f, body, err := wire.ParseFrame(raw); err == nil && f.Type == wire.FrameStep {
+ reports <- *body.(*wire.StepReport)
+ }
+ }
+ }()
+ send("i-c-r", wire.FrameCancelStep, wire.CancelStepCommand{Person: "p@example.com", TaskID: "tkr", StepID: "sr", Attempt: 1})
+ select {
+ case <-paused:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the cancellation report was never held")
+ }
+ send("i-d-r", wire.FrameDispatch, wire.DispatchCommand{Person: "p@example.com", TaskID: "tkr", StepID: "sr", Attempt: 1, Specialist: wire.SpecialistChecks, Repo: "amadan.net/woodstar/panes", Branch: "equipo/tkr-panes", BaseSHA: "1111111111111111111111111111111111111111", Findings: []wire.Finding{}})
+ // The dispatch is applied (its classification and its look at the
+ // hold) while the sender is still paused.
+ for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) {
+ var n int
+ if err := st.db.QueryRowContext(ctx, "SELECT count(*) FROM steps WHERE step_id = 'sr' AND json_extract(body, '$.specialist') = ?", wire.SpecialistChecks).Scan(&n); err == nil && n == 1 {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("the dispatch was not applied")
+ }
+ }
+ time.Sleep(200 * time.Millisecond)
+ released.Do(func() { close(release) })
+ select {
+ case r := <-reports:
+ if r.StepID != "sr" || r.State != wire.StepCancelled || !r.Termination {
+ t.Fatalf("report %+v", r)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("the cancellation's report was lost until the next dispatch or reconnect")
+ }
+}