oficina / slides Public

Say what is true of a stalled log sink: close does not wait, the pump cannot be interrupted

Astra on #20, round 4: with the sink stalled, the log pump stays alive
after close — closing its channel cannot interrupt a Write already
blocked — and the test released the sink before checking the baseline,
which hid that.

Go cannot interrupt a blocked io.Writer, so the code and the test now
say so instead. The pump's and close's comments state the limit: a
write blocked inside the logger can only finish or die with the
process, and shutdown deliberately does not wait for it.

TestMailQueueCloseIsBoundedWithAStalledLogger now asserts, with the
sink STILL stalled, that close returned within its bound, the worker has
exited, and at most the one pump goroutine remains; then it releases
the sink and asserts the pump exits and the goroutine count is back at
baseline. It fails if close waits on the pump, or if the worker logs
through anything that can block ("close hung on a stalled log sink" for
both mutations).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev f5d839ab92e6a362216c1fec79bfee962fbfa0cd parent fbe994f
2 files changed, +43 −13
  • internal/slides/mailqueue.go +10 −1
  • internal/slides/mailqueue_test.go +33 −12

CI passed — run details

diff --git a/internal/slides/mailqueue.go b/internal/slides/mailqueue.go
index 1a585c8..d8c23bc 100644
--- a/internal/slides/mailqueue.go
+++ b/internal/slides/mailqueue.go
@@ -168,6 +168,13 @@ func (q *mailQueue) logf(msg string, args ...any) {
// pump is the only goroutine that writes to the logger. It ends when
// the worker closes logs on its way out.
+//
+// Its limit, stated rather than engineered around: a write blocked
+// inside the logger (a stalled sink) cannot be interrupted from Go. It
+// can only finish, or die with the process. Closing logs does not reach
+// it mid-write, so a stalled sink leaves this one goroutine behind
+// until the sink moves. Nothing else waits on it: not Send, not the
+// worker, not close.
func (q *mailQueue) pump() {
for l := range q.logs {
if n := q.lostLogs.Swap(0); n > 0 {
@@ -245,7 +252,9 @@ func (q *mailQueue) wait() {
// close stops taking links and drains what is queued, within
// drainTimeout; then it cancels, abandons the rest, and stops the
-// worker (see the type's comment).
+// worker (see the type's comment). It never waits on the log pump:
+// shutdown deliberately does not wait for a write blocked inside the
+// logger, which can only finish or die with the process (see pump).
func (q *mailQueue) close() {
q.mu.Lock()
if q.closed {
diff --git a/internal/slides/mailqueue_test.go b/internal/slides/mailqueue_test.go
index 9dcb969..3740fdb 100644
--- a/internal/slides/mailqueue_test.go
+++ b/internal/slides/mailqueue_test.go
@@ -214,13 +214,28 @@ func mailWorkers() int {
}
// TestMailQueueCloseIsBoundedWithAStalledLogger: close reports through
-// nothing that can block. With the log sink stalled — every write hangs
-// — and a relay that never answers but honours cancellation, close
-// still returns within its bound and the worker has exited. Once the
-// sink is released, the goroutine count is back to its baseline: the
-// only thing a stalled sink can hold is the log pump's one write, never
-// close or the worker (Astra, round 3, P2).
+// nothing that can block, and never waits on the log pump. With the log
+// sink stalled — every write hangs — and a relay that never answers but
+// honours cancellation, and WHILE THE SINK IS STILL STALLED:
+//
+// - close returns within its bound;
+// - the mail worker has exited;
+// - at most the one log pump remains, blocked in its write.
+//
+// A write blocked inside the logger cannot be interrupted from Go; it
+// can only finish or die with the process, and shutdown deliberately
+// does not wait for it. So only once the sink is released must the pump
+// exit and the goroutine count return to its baseline (Astra on #20,
+// rounds 3 and 4). The test fails if close waited on the pump, or if
+// the pump could hold the worker.
func TestMailQueueCloseIsBoundedWithAStalledLogger(t *testing.T) {
+ waitFor := func(cond func() bool) bool {
+ deadline := time.Now().Add(2 * time.Second)
+ for !cond() && time.Now().Before(deadline) {
+ time.Sleep(5 * time.Millisecond)
+ }
+ return cond()
+ }
baseline := runtime.NumGoroutine()
release := make(chan struct{})
var once sync.Once
@@ -246,17 +261,23 @@ func TestMailQueueCloseIsBoundedWithAStalledLogger(t *testing.T) {
<-closed
t.Fatal("close hung on a stalled log sink")
}
+
+ // The sink is still stalled.
select {
case <-q.done:
default:
t.Fatal("the worker was still running after close, held by the stalled log sink")
}
- free()
- deadline := time.Now().Add(2 * time.Second)
- for runtime.NumGoroutine() > baseline && time.Now().Before(deadline) {
- time.Sleep(5 * time.Millisecond)
+ if !waitFor(func() bool { return runtime.NumGoroutine() <= baseline+1 }) {
+ t.Fatalf("%d goroutines with the sink stalled, %d before: more than the one blocked log pump remains", runtime.NumGoroutine(), baseline)
}
- if n := runtime.NumGoroutine(); n > baseline {
- t.Fatalf("%d goroutines after close and release, %d before", n, baseline)
+ if n := mailWorkers(); n > 1 {
+ t.Fatalf("%d mail queue goroutines with the sink stalled, want at most the log pump", n)
+ }
+
+ // Released, the pump finishes its write and exits.
+ free()
+ if !waitFor(func() bool { return mailWorkers() == 0 && runtime.NumGoroutine() <= baseline }) {
+ t.Fatalf("after release: %d mail queue goroutines, %d goroutines against %d before", mailWorkers(), runtime.NumGoroutine(), baseline)
}
}