gateway: a write to a Claude Code magpie has ended says so at once, not only after the pipe fails: until Wait reaps it, its input still takes a write that no one reads

TestClaudeEndedBeforeItsInputSaysWhy was red on ubuntu in main's run
37711705564 ("a write after magpie ended Claude Code: <nil>"): the
events end with finish, before the killed process is reaped, so the
effort write went into a pipe still open. tell now checks killed first.
This commit is contained in:
yetone
2026-10-08 09:49:04 +08:00
parent ca387c3ce3
commit 47e571ad51
2 changed files with 24 additions and 0 deletions
@@ -88,4 +88,14 @@ func TestClaudeEndedBeforeItsInputSaysWhy(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "ended by magpie") || strings.Contains(err.Error(), "signal") {
t.Errorf("a write after magpie ended Claude Code: %v", err)
}
// at once, before Claude Code has been reaped (red on ubuntu CI, where
// the write went into a pipe still open and nothing was said)
run, _, err := s.subscription.start(context.Background(), req, "claude-sonnet-5", "", "", nil)
if err != nil {
t.Fatal(err)
}
run.abort()
if err := run.setEffort("high"); err == nil || !strings.Contains(err.Error(), "ended by magpie") {
t.Errorf("a write right after magpie ended Claude Code: %v", err)
}
}
+14
View File
@@ -2884,6 +2884,14 @@ func (r *subscriptionRun) launch() error {
// how it ended, or that magpie ended it, and the last it wrote to stderr —
// the cause comes last, after any warnings before it.
func (r *subscriptionRun) tell(line []byte) error {
// one magpie ended is not written to: until Wait has reaped it, its
// input still takes a write, which no one will read
r.mu.Lock()
killed := r.killed
r.mu.Unlock()
if killed {
return r.whyEnded()
}
_, err := r.stdin.Write(append(line, '\n'))
if err == nil {
return nil
@@ -2899,6 +2907,12 @@ func (r *subscriptionRun) tell(line []byte) error {
case <-time.After(outputDrain + time.Second):
return err
}
return r.whyEnded()
}
// whyEnded says how the run's Claude Code ended, or that magpie ended it,
// and the last it wrote to stderr.
func (r *subscriptionRun) whyEnded() error {
r.mu.Lock()
defer r.mu.Unlock()
why := "exited before it read its input"