console tests: every interactive hub check waits on the hub's change, never a timer or a read at once (rootfs plan 6)
CI at d2ad4bcf: TestAdmissionHoldsItsReservationUntilRegistered saw "hub holds 2". Root cause is the test's ordering, not a leak: B's admission is refused at its CAS, B's End is sent from refuseSession, and B's reservation is released by serveInteractive's deferred (unconditional) release as it returns, just after. The test counted the hub as soon as B had its End. A 50 ms sleep before that release reproduces the CI message every time. The same probe found a second race of the class: after a box dial whose upgrade fails, the door's releaseAttach runs after the box has its 400, and ixStillWaits read the attach flag at once. The hub gains one test seam, onChange, run under h.mu by every call that may change what it holds of an id. The tests wait on it through ixWaitHubUntil (re-check on each change; fail after 10 s with no satisfying change): ixWaitHub no longer polls on a timer; the reservation test waits for B's release; ixStillWaits waits for active and unattached; the drain-expiry test waits for pending; the serial-bump test reads after its own event (the bump's PUT follows the admission step's return) instead of a time-bounded loop. Changed tests, each 200/200 at nice 19 and robust with 50 ms sleeps widening release, end, endDone, pend, finished and releaseAttach.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/apiinteractive_lifetimes_test.go+2 −7internal/console/apiinteractive_test.go+60 −13internal/console/apiinteractivebox_test.go+9 −6internal/console/interactivehub.go+26 −0
CI passed — run details
Intent
<task-notification> <task-id>bmkwgjzkq</task-id> <tool-use-id>toolu_012irqYTt5UYAiayiUDEjcif</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ba9fa944-a429-4788-9b3c-f2b2f42617f3/tasks/bmkwgjzkq.output</output-file> <status>completed</status> <summary>Background command "Wait for CI on the Plan 6 tip" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>a7099b6064086f5c2</task-id> <tool-use-id>toolu_01MyJSzHFh22zdYr3cD592DL</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ba9fa944-a429-4788-9b3c-f2b2f42617f3/tasks/a7099b6064086f5c2.output</output-file> <status>completed</status> <summary>Agent "Plan 6 Task 10: console session door" is waiting on its own background work</summary> <note>This agent stopped with background work of its own still running. It may resume on its own when that work completes or reports, and the same task-id notifies again if it does; the result below may be interim.</note> <result>Waiting for the monitor's event.</result> <usage><subagent_tokens>495403</subagent_tokens><tool_uses>26</tool_uses><duration_ms>894774</duration_ms></usage> </task-notification>
Attributed to this commit: 61.9 g CO₂e (1,266,787 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on rootfs-apps-6-interactive, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/apiinteractive_lifetimes_test.go b/internal/console/apiinteractive_lifetimes_test.go| index 246ee86a8..8b51fc2e5 100644 |
| --- a/internal/console/apiinteractive_lifetimes_test.go |
| +++ b/internal/console/apiinteractive_lifetimes_test.go |
| @@ -176,13 +176,8 @@ func TestAWaitingSessionExpiresDuringTheDrain(t *testing.T) { |
| if e := ixWantEnd(t, ws); e.Reason != interactive.EndExpired { |
| t.Fatalf("end %+v, want expired at PickupBy, with no write needed to say so", e) |
| } |
| - deadline := time.Now().Add(10 * time.Second) |
| - for hubState(h, id) != "pending" && time.Now().Before(deadline) { |
| - time.Sleep(10 * time.Millisecond) |
| - } |
| - if st := hubState(h, id); st != "pending" { |
| - t.Fatalf("hub: %q, want pending (the end step refused while draining)", st) |
| - } |
| + // The end step is refused while draining, after the CLI has its End. |
| + ixWaitHub(t, h, id, "pending") |
| h.mu.Lock() |
| pendingEnd := h.endPending[id] |
| h.mu.Unlock() |
diff --git a/internal/console/apiinteractive_test.go b/internal/console/apiinteractive_test.go| index 6f7a5c0fd..16944ad8f 100644 |
| --- a/internal/console/apiinteractive_test.go |
| +++ b/internal/console/apiinteractive_test.go |
| @@ -941,9 +941,11 @@ func TestAdmissionHoldsItsReservationUntilRegistered(t *testing.T) { |
| if g := ixGrants(t, fx); len(g) != 1 || g[0] != interactive.GrantKey(fx.acct, "shop", aID) { |
| t.Fatalf("grants %v, want A's only", g) |
| } |
| - if hubCount(h) != 1 || hubState(h, aID) != "active" { |
| - t.Fatalf("hub holds %d, A is %q", hubCount(h), hubState(h, aID)) |
| - } |
| + // B's reservation is released as B's goroutine returns, after its End: |
| + // wait for that change, not a read at once. |
| + ixWaitHubUntil(t, h, "B's reservation released and A active", func() bool { |
| + return hubCount(h) == 1 && hubState(h, aID) == "active" |
| + }) |
| a.ws.Close() |
| ixWaitEvents(t, fx, "interactive-end", 1) |
| } |
| @@ -1114,12 +1116,57 @@ func ixSweepAt(fx *interactiveFx, now time.Time) bool { |
| // ixWaitHub waits up to 10 seconds for id to reach state in the hub. |
| func ixWaitHub(t *testing.T, h *interactiveHub, id, state string) { |
| t.Helper() |
| - deadline := time.Now().Add(10 * time.Second) |
| - for hubState(h, id) != state && time.Now().Before(deadline) { |
| - time.Sleep(10 * time.Millisecond) |
| - } |
| - if got := hubState(h, id); got != state { |
| - t.Fatalf("hub: %s is %q, want %q", id, got, state) |
| + ixWaitHubUntil(t, h, fmt.Sprintf("%s to be %q (it is %q)", id, state, hubState(h, id)), func() bool { return hubState(h, id) == state }) |
| +} |
| + |
| +// hubWatch turns the hub's onChange seam into a broadcast: ch is closed, |
| +// and replaced, on every change. |
| +type hubWatch struct { |
| + mu sync.Mutex |
| + ch chan struct{} |
| +} |
| + |
| +// watchHub installs the test's change broadcast on h, once per test. |
| +func watchHub(t *testing.T, h *interactiveHub) *hubWatch { |
| + h.mu.Lock() |
| + defer h.mu.Unlock() |
| + if w, ok := hubWatches.Load(h); ok { |
| + return w.(*hubWatch) |
| + } |
| + w := &hubWatch{ch: make(chan struct{})} |
| + hubWatches.Store(h, w) |
| + h.onChange = func() { |
| + w.mu.Lock() |
| + close(w.ch) |
| + w.ch = make(chan struct{}) |
| + w.mu.Unlock() |
| + } |
| + t.Cleanup(func() { hubWatches.Delete(h) }) |
| + return w |
| +} |
| + |
| +var hubWatches sync.Map // *interactiveHub -> *hubWatch |
| + |
| +// ixWaitHubUntil waits until cond holds, re-checking it on each of the |
| +// hub's change events (never on a timer). It fails after 10 seconds with |
| +// no change that satisfies cond: the event never came. |
| +func ixWaitHubUntil(t *testing.T, h *interactiveHub, what string, cond func() bool) { |
| + t.Helper() |
| + w := watchHub(t, h) |
| + timeout := time.NewTimer(10 * time.Second) |
| + defer timeout.Stop() |
| + for { |
| + w.mu.Lock() |
| + changed := w.ch |
| + w.mu.Unlock() |
| + if cond() { |
| + return |
| + } |
| + select { |
| + case <-changed: |
| + case <-timeout.C: |
| + t.Fatalf("timed out waiting for %s", what) |
| + } |
| } |
| } |
| @@ -1351,11 +1398,11 @@ func TestTheSerialBumpHoldsNoAdmission(t *testing.T) { |
| t.Fatal("no marker while the bump hangs") |
| } |
| id := rec.Interactive.ID |
| + // The bump's PUT (entered) follows the admission step's return in the |
| + // session's goroutine, so the unit's end and the activation have |
| + // happened by the event: read them now, and the time it took. |
| bound := wait + 2*time.Second |
| - for (fx.srv.cutover.status().Pending != 0 || hubState(h, id) != "active") && time.Since(start) < bound { |
| - time.Sleep(5 * time.Millisecond) |
| - } |
| - if p, st := fx.srv.cutover.status().Pending, hubState(h, id); p != 0 || st != "active" { |
| + if p, st := fx.srv.cutover.status().Pending, hubState(h, id); p != 0 || st != "active" || time.Since(start) > bound { |
| t.Fatalf("with the bump hanging %v after the Open: the gate counts %d units, the id is %q; want 0 and active within %v", time.Since(start), p, st, bound) |
| } |
| if got := serial(t, fx.consoleFixture); got != before { |
diff --git a/internal/console/apiinteractivebox_test.go b/internal/console/apiinteractivebox_test.go| index 6b1b24be0..2e5105da1 100644 |
| --- a/internal/console/apiinteractivebox_test.go |
| +++ b/internal/console/apiinteractivebox_test.go |
| @@ -206,14 +206,17 @@ func hubAttached(h *interactiveHub, id string) bool { |
| // ixStillWaits proves the CLI's session is still open and unattached: the |
| // hub holds it active and the console answers its keepalive. |
| +// |
| +// A refused dial may have claimed the attach first (a failed upgrade |
| +// releases it only after the box has its answer), so this waits on the |
| +// hub's change for the session to be active and unattached; a claim that is |
| +// never released fails it. |
| func ixStillWaits(t *testing.T, fx *interactiveFx, cli *websocket.Conn, id string) { |
| t.Helper() |
| - if st := hubState(fx.srv.interactive, id); st != "active" { |
| - t.Fatalf("hub: %q, want active", st) |
| - } |
| - if hubAttached(fx.srv.interactive, id) { |
| - t.Fatal("the hub has the session attached") |
| - } |
| + h := fx.srv.interactive |
| + ixWaitHubUntil(t, h, id+" active and unattached", func() bool { |
| + return hubState(h, id) == "active" && !hubAttached(h, id) |
| + }) |
| if err := interactive.Send(cli, interactive.KindAlive, nil); err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/console/interactivehub.go b/internal/console/interactivehub.go| index 323450261..3b371997e 100644 |
| --- a/internal/console/interactivehub.go |
| +++ b/internal/console/interactivehub.go |
| @@ -100,6 +100,13 @@ type interactiveHub struct { |
| // returns fails the lookup there. |
| endRowCheck func(id string) error |
| + // onChange, when set, runs under h.mu after every call that may change |
| + // what the hub holds of an id (reserve, activate, release, an attach |
| + // claim or release, end, endDone, the pend calls, finished, prune), so |
| + // a test can wait on the change rather than on a timer. It must neither |
| + // block nor take h.mu. Set it under h.mu. A TEST SEAM. |
| + onChange func() |
| + |
| // endRows serializes every interactive-end row's check and insert, so |
| // a session gets one terminal row (auditInteractiveEnd). |
| endRows sync.Mutex |
| @@ -195,12 +202,20 @@ func (h *interactiveHub) live(st provision.InteractiveState, boot string) bool { |
| return ok |
| } |
| +// changedLocked runs the onChange seam. h.mu is held. |
| +func (h *interactiveHub) changedLocked() { |
| + if h.onChange != nil { |
| + h.onChange() |
| + } |
| +} |
| + |
| // reserve holds sess's id, not yet active: before the grant and the |
| // admission, so the marker is live from the moment its CAS lands. |
| func (h *interactiveHub) reserve(sess *hubSession) { |
| h.mu.Lock() |
| sess.active = false |
| h.byID[sess.id] = sess |
| + h.changedLocked() |
| h.mu.Unlock() |
| if h.afterReserve != nil { |
| h.afterReserve(sess.id) |
| @@ -212,6 +227,7 @@ func (h *interactiveHub) reserve(sess *hubSession) { |
| func (h *interactiveHub) activate(id string) bool { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| sess, ok := h.byID[id] |
| if !ok { |
| return false |
| @@ -230,6 +246,7 @@ func (h *interactiveHub) release(id string) { |
| if released { |
| delete(h.byID, id) |
| } |
| + h.changedLocked() |
| h.mu.Unlock() |
| if released && h.afterRelease != nil { |
| h.afterRelease(id) |
| @@ -254,6 +271,7 @@ func (h *interactiveHub) activeSession(id string) *hubSession { |
| func (h *interactiveHub) claimAttach(id string) bool { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| sess, ok := h.byID[id] |
| if !ok || !sess.active || sess.attached { |
| return false |
| @@ -266,6 +284,7 @@ func (h *interactiveHub) claimAttach(id string) bool { |
| func (h *interactiveHub) releaseAttach(id string) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| if sess, ok := h.byID[id]; ok { |
| sess.attached = false |
| } |
| @@ -287,6 +306,7 @@ func (h *interactiveHub) registry(path string) (consoleRegistry, error) { |
| func (h *interactiveHub) end(id string) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| now := time.Now() |
| h.pruneLocked(now) |
| until := now |
| @@ -311,6 +331,7 @@ func (h *interactiveHub) end(id string) { |
| func (h *interactiveHub) endDone(sess *hubSession) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| delete(h.ending, sess.id) |
| h.ended[sess.id] = sess.pickupBy |
| } |
| @@ -320,6 +341,7 @@ func (h *interactiveHub) endDone(sess *hubSession) { |
| func (h *interactiveHub) pend(sess *hubSession, e interactive.End) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| delete(h.ended, sess.id) |
| delete(h.ending, sess.id) |
| h.endPending[sess.id] = e |
| @@ -332,6 +354,7 @@ func (h *interactiveHub) pend(sess *hubSession, e interactive.End) { |
| func (h *interactiveHub) pendGrant(sess *hubSession) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| h.endPending[sess.id] = interactive.End{Reason: interactive.EndRefused} |
| h.pendingSess[sess.id] = sess |
| h.grantOnly[sess.id] = true |
| @@ -343,6 +366,7 @@ func (h *interactiveHub) pendGrant(sess *hubSession) { |
| func (h *interactiveHub) pendRow(sess *hubSession, e interactive.End) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| if _, held := h.byID[sess.id]; held || h.ending[sess.id] { |
| return |
| } |
| @@ -379,6 +403,7 @@ func (h *interactiveHub) pending() []pendingWork { |
| func (h *interactiveHub) finished(id string) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| sess, only := h.pendingSess[id], h.grantOnly[id] || h.rowOnly[id] |
| delete(h.endPending, id) |
| delete(h.pendingSess, id) |
| @@ -432,6 +457,7 @@ func (h *interactiveHub) owns(id string) bool { |
| func (h *interactiveHub) prune(now time.Time) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + defer h.changedLocked() |
| h.pruneLocked(now) |
| } |