Files
magpie/internal/plugin/stderr_drain_test.go
T
CaramelizedCUDAandClaude Code 3f5c3b0553 plugin: the host keeps reading a plugin's stderr after a line over 4 MiB, so the plugin host no longer hangs (Windows) or keeps the unread output in memory (Linux)
The host logged the plugin child's stderr with a bufio.Scanner capped
at 4 MiB. On the first longer line Scan stopped with ErrTooLong, the
goroutine returned, and nothing read the pipe again. host.js sends
console.log and process.stdout.write to stderr, so one long line from
any plugin was enough. Now the reader drains the pipe past an
overlong line and logs what it can, so the output stops being
dropped silently.

Measured on Bun 1.4.2 (Windows and Linux, official binaries, sha256
checked): on Windows the plugin host stopped answering init when a
plugin wrote one long line while loading; on Linux a script writing
200 MiB to stderr with no reader went from 18 MiB to 412 MiB RSS.
Each of those reproduced at HEAD and is gone with this change; the
Linux memory number comes from that Bun-only measurement, since
magpie's own RSS did not differ at this scale.

Run before this commit: go test -tags nogui ./internal/gui
./internal/plugin is ok on Linux (WSL2 Ubuntu 24.04, go1.26.8) both on
HEAD and on this change. On Windows, go test -tags nogui ./... (49
packages): every failing test also fails at 6c1c0a74 and is listed in
#1827; the plugin package's TestLoadConcurrentWithSavesAndEdits fails
at HEAD too.

macOS was not run here; CI's macOS job has no Bun setup step, so the
end-to-end test skips there if bun is not on PATH. The unit test runs
either way.

Reviewed for magpie (CaramelizedCUDA/magpie pr/plugin-stderr-drain at 56120240, cherry-picked onto f340aecab, then rebased onto 6ccf23efa and run again there): with drainStderr's io.Copy removed, TestDrainStderrKeepsTheChildGoing fails on macOS ("the child is still blocked writing stderr after a line over 4 MiB"). On the Windows 11 box (windows/amd64 test binary, Bun 1.4.2 official zip, sha256 checked, USERPROFILE/APPDATA in a scratch folder) both tests fail without it, TestHostStartsPastAnOverlongStderrLine with "starting plugins: context deadline exceeded" after 62s, and both pass with it (0.02s, 1.99s). go test -tags nogui ./internal/gui ./internal/plugin is ok on macOS.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-12 23:33:56 +08:00

63 lines
1.5 KiB
Go

package plugin
import (
"bytes"
"log"
"os"
"os/exec"
"strings"
"testing"
"time"
)
// The test binary stands in for a plugin's child when MAGPIE_STDERR_CHILD is
// set: 8 MiB to stderr with no newline, then a marker line, then it exits.
func init() {
if os.Getenv("MAGPIE_STDERR_CHILD") == "1" {
chunk := strings.Repeat("x", 64<<10)
for range 128 {
os.Stderr.WriteString(chunk)
}
os.Stderr.WriteString("\nMARKER\n")
os.Exit(0)
}
}
// A plugin's console.log of one line over 4 MiB stopped the drain, and nothing
// read the child's stderr after that, so the child blocked on its next write.
func TestDrainStderrKeepsTheChildGoing(t *testing.T) {
var logs bytes.Buffer
old := log.Writer()
log.SetOutput(&logs)
t.Cleanup(func() { log.SetOutput(old) })
cmd := exec.Command(os.Args[0], "-test.run=^TestDrainStderrKeepsTheChildGoing$")
cmd.Env = append(os.Environ(), "MAGPIE_STDERR_CHILD=1")
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
drained := make(chan struct{})
go func() {
drainStderr(stderr)
close(drained)
}()
exited := make(chan error, 1)
go func() { exited <- cmd.Wait() }()
select {
case <-exited:
case <-time.After(15 * time.Second):
cmd.Process.Kill()
<-exited
<-drained
t.Fatal("the child is still blocked writing stderr after a line over 4 MiB")
}
<-drained
if !strings.Contains(logs.String(), "stderr is dropped from here on") {
t.Fatalf("the log doesn't say the plugin's stderr stopped being logged:\n%.300s", logs.String())
}
}