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 at6c1c0a74and 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 ontof340aecab, then rebased onto6ccf23efaand 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>
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// TestHostStartsPastAnOverlongStderrLine runs the real host with the real Bun
|
|
// testenv finds (the one on PATH, at least BunVersion's MinBun), and a plugin
|
|
// whose load writes one 5 MiB line to stderr. The line stops the drain at
|
|
// 4 MiB; with the child's stderr left unread, on Windows its init answer never
|
|
// comes and startOn runs out of time. On Linux Bun keeps the unread bytes in
|
|
// memory instead and starts either way, so there TestDrainStderrKeepsTheChildGoing
|
|
// is what fails without the fix.
|
|
func TestHostStartsPastAnOverlongStderrLine(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
dir := storeSandbox(t)
|
|
plug := filepath.Join(dir, "plug.mjs")
|
|
src := "console.log('x'.repeat(5 * 1024 * 1024))\nexport const name = 'overlong-stderr'\n"
|
|
if err := os.WriteFile(plug, []byte(src), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err := json.Marshal(map[string]any{"plugins": []map[string]string{{"spec": plug}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeList(t, string(list))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
h, _, err := startOn(ctx, bun)
|
|
if err != nil {
|
|
t.Fatalf("the host did not start past the plugin's overlong stderr line: %v", err)
|
|
}
|
|
t.Cleanup(h.stop)
|
|
}
|