moves: offline run's log stands alone; SIGPIPE cannot end a run
Astra branch review round 1, findings 4 and 5 (P2). run writes the terminal and the --log file each on its own (runOutput), never through a MultiWriter: a terminal that fails never keeps a line from the file, and a file that fails mid-run says OR45 once on standard error and the run carries on. No write can end the run. On Linux and macOS run ignores SIGPIPE as well as SIGHUP, so a write to a standard output whose reader went away (an SSH session without a terminal, dropped) fails with EPIPE, which run ignores, instead of the Go runtime killing it. A child-process test shows the difference. The CLI page's claim (OR56) now holds as written. OR45's deck context notes its use mid-run (context only).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+184
−19
approved_moves_copy_test.go+2 −2docs/drafts/offline-move-run/copy-review/strings.json+1 −1move.go+3 −0move_offline_run.go+45 −8move_offline_run_other.go+4 −3move_offline_run_test.go+62 −2move_offline_run_unix.go+7 −3move_offline_run_unix_test.go+60 −0
Intent
diff --git a/approved_moves_copy_test.go b/approved_moves_copy_test.go| index 18c03531e..7a298d547 100644 |
| --- a/approved_moves_copy_test.go |
| +++ b/approved_moves_copy_test.go |
| @@ -1603,8 +1603,8 @@ type movesDiagnosticWrite struct { |
| var movesDiagnosticWrites = map[string]map[string]movesDiagnosticWrite{ |
| "move_offline_run.go": { |
| - "offlineRun|runLogTrouble.logReason": { |
| - Why: "the OS error of opening the --log file (err.Error() of os.OpenFile), which OR45 shows as its <system error> by its approved wording; a diagnostic, not copy", |
| + "sayLogTrouble|runLogTrouble.logReason": { |
| + Why: "the OS error of opening or writing the --log file (err.Error() of os.OpenFile or of the file's Write), which OR45 shows as its <system error> by its approved wording; a diagnostic, not copy", |
| Count: 1, |
| }, |
| }, |
diff --git a/docs/drafts/offline-move-run/copy-review/strings.json b/docs/drafts/offline-move-run/copy-review/strings.json| index cc36e3182..ae3936ad7 100644 |
| --- a/docs/drafts/offline-move-run/copy-review/strings.json |
| +++ b/docs/drafts/offline-move-run/copy-review/strings.json |
| @@ -333,7 +333,7 @@ |
| "section": "Errors", |
| "label": "Log file", |
| "text": "cannot write the log %s: %v", |
| - "context": "%s the --log path, %v the system error. Exit 1 before anything is sent.", |
| + "context": "%s the --log path, %v the system error. Exit 1 before anything is sent when the file cannot be opened. Once the run is under way a write to the file that fails says it once on standard error, and the run carries on (the move is never stopped for its log).", |
| "status": "approved (Luna, by Paul's delegation)" |
| } |
| ] |
| \ No newline at end of file |
diff --git a/move.go b/move.go| index 5b20254e4..3fd001708 100644 |
| --- a/move.go |
| +++ b/move.go |
| @@ -166,6 +166,9 @@ type moveDeps struct { |
| now func() time.Time |
| after func(time.Duration) <-chan time.Time |
| probe func(ctx context.Context, url string) int |
| + // stderr is offline run's standard error (os.Stderr when nil): where |
| + // it says, once, that its --log stopped taking writes. |
| + stderr io.Writer |
| // interrupts starts the Ctrl-C source: it calls add once per Ctrl-C, |
| // from any goroutine, until stop. |
| interrupts func(add func()) (stop func()) |
diff --git a/move_offline_run.go b/move_offline_run.go| index fc8c951c5..562cf85bc 100644 |
| --- a/move_offline_run.go |
| +++ b/move_offline_run.go |
| @@ -36,13 +36,44 @@ import ( |
| // separators between pieces (runSep) and the commands printed under a |
| // sentence (sayRunCommand) are not sentences. |
| -// runLogTrouble is why --log could not be opened: the OS's own error, |
| -// shown as OR45's <system error> (a diagnostic, not copy). |
| +// runLogTrouble is why --log could not be opened or written: the OS's own |
| +// error, shown as OR45's <system error> (a diagnostic, not copy). |
| type runLogTrouble struct { |
| path string |
| logReason string |
| } |
| +// sayLogTrouble is OR45 for the log at path and the OS's error. |
| +func sayLogTrouble(out io.Writer, path string, err error) { |
| + t := runLogTrouble{path: path, logReason: err.Error()} |
| + moveSay(out, fmt.Sprintf("cannot write the log %s: %v", t.path, t.logReason)) |
| +} |
| + |
| +// runOutput is what run writes to: the terminal and, with --log, the file, |
| +// each written on its own (Astra branch round 1, finding 4). A terminal |
| +// that fails (a session gone) never keeps a line from the file, and a file |
| +// that fails never stops the move: OR45 is said once on standard error |
| +// and the run carries on, still offering the file each line. Write never |
| +// fails, so nothing the run says can end it. |
| +type runOutput struct { |
| + term io.Writer |
| + log io.Writer // nil without --log |
| + path string |
| + stderr io.Writer |
| + failed bool // OR45 was said |
| +} |
| + |
| +func (o *runOutput) Write(p []byte) (int, error) { |
| + _, _ = o.term.Write(p) |
| + if o.log != nil { |
| + if _, err := o.log.Write(p); err != nil && !o.failed { |
| + o.failed = true |
| + sayLogTrouble(o.stderr, o.path, err) |
| + } |
| + } |
| + return len(p), nil |
| +} |
| + |
| // runPiece says part of a line, with no newline: a copy gate sink, like |
| // moveSay. |
| func runPiece(out io.Writer, piece string) { |
| @@ -84,18 +115,23 @@ func runClock(t time.Time) string { return t.UTC().Format("15:04:05Z") } |
| // offlineRun carries one `carlos move offline run`. |
| func (m *mover) offlineRun(ctx context.Context, a moveArgs, d moveDeps) int { |
| + stderr := d.stderr |
| + if stderr == nil { |
| + stderr = os.Stderr |
| + } |
| + out := &runOutput{term: m.out, stderr: stderr} |
| if a.logPath != "" { |
| f, err := os.OpenFile(a.logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) |
| if err != nil { |
| - t := runLogTrouble{path: a.logPath, logReason: err.Error()} |
| - moveSay(m.out, fmt.Sprintf("cannot write the log %s: %v", t.path, t.logReason)) |
| + sayLogTrouble(m.out, a.logPath, err) |
| return moveExitError |
| } |
| defer f.Close() |
| // Every write goes to the file as it is made: os.File is |
| // unbuffered, and the file is opened to append. |
| - m.out = io.MultiWriter(m.out, f) |
| + out.log, out.path = f, a.logPath |
| } |
| + m.out = out |
| now, after, probe, interrupts := d.now, d.after, d.probe, d.interrupts |
| if now == nil { |
| now = time.Now |
| @@ -149,10 +185,11 @@ func (m *mover) offlineRun(ctx context.Context, a moveArgs, d moveDeps) int { |
| return watch.end(done) |
| } |
| -// ctrlC is the real Ctrl-C source: each SIGINT is one add. A hangup (the |
| -// SSH session dropped) is ignored, so the move goes on (unix only). |
| +// ctrlC is the real Ctrl-C source: each SIGINT is one add. A hangup, and |
| +// a write to a standard output nobody reads (the SSH session dropped), are |
| +// ignored, so the move goes on (unix only, ignoreDetach). |
| func ctrlC(add func()) func() { |
| - ignoreHangup() |
| + ignoreDetach() |
| sig := make(chan os.Signal, 2) |
| signal.Notify(sig, os.Interrupt) |
| quit := make(chan struct{}) |
diff --git a/move_offline_run_other.go b/move_offline_run_other.go| index f292034b5..935197316 100644 |
| --- a/move_offline_run_other.go |
| +++ b/move_offline_run_other.go |
| @@ -2,6 +2,7 @@ |
| package main |
| -// ignoreHangup does nothing here: there is no SIGHUP to ignore (unverified |
| -// whether a closed console window stops the process on Windows). |
| -func ignoreHangup() {} |
| +// ignoreDetach does nothing here: there is no SIGHUP or SIGPIPE to ignore |
| +// (unverified whether a closed console window stops the process on |
| +// Windows). |
| +func ignoreDetach() {} |
diff --git a/move_offline_run_test.go b/move_offline_run_test.go| index 054236179..42d7c0b9a 100644 |
| --- a/move_offline_run_test.go |
| +++ b/move_offline_run_test.go |
| @@ -3,6 +3,8 @@ package main |
| import ( |
| "bytes" |
| "context" |
| + "errors" |
| + "io" |
| "net/http" |
| "net/http/httptest" |
| "os" |
| @@ -259,6 +261,10 @@ type runSeams struct { |
| // press, when set, is filled with the Ctrl-C itself, for a test's |
| // console hooks to press at any point. |
| press *func() |
| + // term, when set, is the terminal the run writes to (else a buffer |
| + // whose bytes the run's output is); stderr is the run's standard error. |
| + term io.Writer |
| + stderr io.Writer |
| } |
| // runOfflineRun runs `carlos move offline run` against c on a fake clock |
| @@ -268,6 +274,10 @@ func runOfflineRun(t *testing.T, c *fakeOfflineConsole, seams runSeams, args ... |
| ts := httptest.NewServer(c) |
| t.Cleanup(ts.Close) |
| var out bytes.Buffer |
| + var term io.Writer = &out |
| + if seams.term != nil { |
| + term = seams.term |
| + } |
| clock := time.Date(2026, 11, 15, 6, 12, 0, 0, time.UTC) |
| var add func() |
| polls := 0 |
| @@ -312,12 +322,13 @@ func runOfflineRun(t *testing.T, c *fakeOfflineConsole, seams runSeams, args ... |
| } |
| return func() {} |
| }, |
| + stderr: seams.stderr, |
| } |
| args = append([]string{"offline", "run"}, args...) |
| args = append(args, "--host", oHost, "--account", "bab", "--app", "shop") |
| ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| defer cancel() |
| - code := moveMain(ctx, args, &out, deps) |
| + code := moveMain(ctx, args, term, deps) |
| return code, out.String() |
| } |
| @@ -857,7 +868,8 @@ func TestMoveOfflineRunCtrlCDuringRateLimit(t *testing.T) { |
| if posts := c.postsSent(); len(posts) != 0 { |
| t.Fatalf("posted %q", posts) |
| } |
| - if out != "Stopped. Nothing was sent.\n" { |
| + // The 429 is said when it was read before the Ctrl-C ended the read. |
| + if out != "Stopped. Nothing was sent.\n" && out != "The console is limiting requests. Checking every 10 seconds.\nStopped. Nothing was sent.\n" { |
| t.Fatalf("output:\n%s", out) |
| } |
| } |
| @@ -885,3 +897,51 @@ func TestMoveOfflineRunCtrlCAfterUncertainOpen(t *testing.T) { |
| t.Fatalf("said served after an open with no answer:\n%s", out) |
| } |
| } |
| + |
| +// brokenTerm is a terminal that has gone away: every write fails. |
| +type brokenTerm struct{ writes int } |
| + |
| +func (b *brokenTerm) Write([]byte) (int, error) { |
| + b.writes++ |
| + return 0, errors.New("write /dev/stdout: broken pipe") |
| +} |
| + |
| +// TestMoveOfflineRunLogStandsAlone (finding 4): the --log file is written |
| +// on its own. A terminal that fails every write (a dropped session) never |
| +// starves the log, and a log that fails mid-run never stops the move: OR45 |
| +// once on standard error, and the run carries on to the end. |
| +func TestMoveOfflineRunLogStandsAlone(t *testing.T) { |
| + log := filepath.Join(t.TempDir(), "run.log") |
| + term := &brokenTerm{} |
| + c := &fakeOfflineConsole{sim: &offlineSim{}} |
| + code, _ := runOfflineRun(t, c, runSeams{term: term}, "--to", "uk", "--log", log) |
| + if code != 0 { |
| + t.Fatalf("exit %d", code) |
| + } |
| + got, err := os.ReadFile(log) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if term.writes == 0 { |
| + t.Fatal("nothing was written to the terminal") |
| + } |
| + wantLines(t, string(got), "06:12:00Z stage sent, accepted", orHeader, "06:12:18Z check https://acme.shop.example.com/ answered 200", orMoved) |
| + |
| + // A log whose writes fail once the run is under way (a full disk). |
| + if _, err := os.Stat("/dev/full"); err != nil { |
| + t.Skip("no /dev/full to fail the log's writes with") |
| + } |
| + var stderr bytes.Buffer |
| + c = &fakeOfflineConsole{sim: &offlineSim{}} |
| + code, out := runOfflineRun(t, c, runSeams{stderr: &stderr}, "--to", "uk", "--log", "/dev/full") |
| + if code != 0 { |
| + t.Fatalf("exit %d:\n%s", code, out) |
| + } |
| + wantLines(t, out, orMoved) |
| + if want := "cannot write the log /dev/full: write /dev/full: no space left on device\n"; stderr.String() != want { |
| + t.Fatalf("standard error:\n%s\nwant OR45 once:\n%s", stderr.String(), want) |
| + } |
| + if got := doorsOf(c.postsSent()); got != "stage,start,seal,flip,open,finish,finish" { |
| + t.Fatalf("doors %s", got) |
| + } |
| +} |
diff --git a/move_offline_run_unix.go b/move_offline_run_unix.go| index d133a1d06..b9fbe0e31 100644 |
| --- a/move_offline_run_unix.go |
| +++ b/move_offline_run_unix.go |
| @@ -7,7 +7,11 @@ import ( |
| "syscall" |
| ) |
| -// ignoreHangup keeps `carlos move offline run` going when its terminal |
| +// ignoreDetach keeps `carlos move offline run` going when its terminal |
| // goes away (an SSH session dropped): the move is under way, and stopping |
| -// the watcher would strand it. |
| -func ignoreHangup() { signal.Ignore(syscall.SIGHUP) } |
| +// the watcher would strand it. SIGHUP is the terminal's hangup; SIGPIPE is |
| +// what Go's runtime raises on a write to a standard output whose reader |
| +// has gone (an SSH session with no terminal), which kills the process |
| +// unless it is ignored. Ignored, such a write fails with EPIPE, and run |
| +// ignores its output's errors (runOutput). |
| +func ignoreDetach() { signal.Ignore(syscall.SIGHUP, syscall.SIGPIPE) } |
diff --git a/move_offline_run_unix_test.go b/move_offline_run_unix_test.go| new file mode 100644 |
| index 000000000..c00e069ca |
| --- /dev/null |
| +++ b/move_offline_run_unix_test.go |
| @@ -0,0 +1,60 @@ |
| +//go:build linux || darwin |
| + |
| +package main |
| + |
| +import ( |
| + "errors" |
| + "fmt" |
| + "os" |
| + "os/exec" |
| + "syscall" |
| + "testing" |
| +) |
| + |
| +// TestOfflineRunSurvivesAClosedStdout (finding 5): `carlos move offline |
| +// run` keeps going when its standard output is a pipe nobody reads any |
| +// more (an SSH session without a terminal, dropped). Go's runtime kills a |
| +// program with SIGPIPE on a write to such a stdout unless SIGPIPE is |
| +// ignored, which ignoreDetach does with SIGHUP. The child writes to a |
| +// closed pipe with and without it; only without is it killed. |
| +func TestOfflineRunSurvivesAClosedStdout(t *testing.T) { |
| + switch os.Getenv("CARLOS_TEST_RUN_STDOUT") { |
| + case "detached": |
| + ignoreDetach() |
| + fallthrough |
| + case "attached": |
| + for i := 0; i < 100; i++ { |
| + fmt.Fprintln(os.Stdout, "a line nobody reads") |
| + } |
| + os.Exit(7) |
| + } |
| + for _, c := range []struct { |
| + mode string |
| + killed bool |
| + }{{"attached", true}, {"detached", false}} { |
| + t.Run(c.mode, func(t *testing.T) { |
| + r, w, err := os.Pipe() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r.Close() |
| + defer w.Close() |
| + cmd := exec.Command(os.Args[0], "-test.run=^TestOfflineRunSurvivesAClosedStdout$") |
| + cmd.Env = append(os.Environ(), "CARLOS_TEST_RUN_STDOUT="+c.mode, "TMPDIR="+t.TempDir()) |
| + cmd.Dir = t.TempDir() |
| + cmd.Stdout = w |
| + err = cmd.Run() |
| + var exit *exec.ExitError |
| + if !errors.As(err, &exit) { |
| + t.Fatalf("the child ended %v", err) |
| + } |
| + st := exit.Sys().(syscall.WaitStatus) |
| + switch { |
| + case c.killed && !(st.Signaled() && st.Signal() == syscall.SIGPIPE): |
| + t.Fatalf("without ignoreDetach the child was not killed by SIGPIPE (%v): the test does not bite", st) |
| + case !c.killed && (st.Signaled() || st.ExitStatus() != 7): |
| + t.Fatalf("with ignoreDetach the child ended %v, want exit 7", st) |
| + } |
| + }) |
| + } |
| +} |