fix(xray): capture whole core crash reports instead of the first pipe chunk

LogWriter inspected each pipe chunk on its own. The Go runtime writes
"panic: " with a separate write before the message and traceback, so the
crash report was written from that first chunk alone and the panel left
7-byte core_crash_*.log files holding only "panic: ". Any ordinary log
line mentioning panic/exception/stack trace was also treated as a crash
and dropped from the panel log.

The writer now splits output into lines, starts a crash only on a line
beginning with the Go runtime's "panic: " / "fatal error: " header, keeps
everything after it, and writes one report from Flush, which
waitForCommand calls after cmd.Wait has delivered all output. The header
line becomes LastLine so GetResult shows the cause.

The report filename format "_000000000" is not a Go fractional-seconds
layout and always produced literal zeros, so reports from the same second
overwrote each other; os.CreateTemp now gives every report a unique name.
This commit is contained in:
MHSanaei
2026-10-14 15:31:09 +02:00
parent c45ffcdafa
commit 760f428c41
4 changed files with 242 additions and 84 deletions
+31
View File
@@ -5,6 +5,7 @@ import (
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
@@ -280,6 +281,33 @@ func TestWaitForCommand_CrashExitRecordsError(t *testing.T) {
}
}
// A real child panic reaches the LogWriter in several pipe chunks; the crash
// report is only whole if waitForCommand flushes the writer after cmd.Wait.
func TestWaitForCommand_PanicWritesWholeCrashReport(t *testing.T) {
dir := t.TempDir()
t.Setenv("XUI_LOG_FOLDER", dir)
cmd := exec.Command(os.Args[0], "-test.run=TestMutationAuditHelper", "--", "panic")
cmd.Env = append(os.Environ(), "XRAY_MUT_HELPER=1")
p := newProcess(nil)
cmd.Stdout = p.logWriter
cmd.Stderr = p.logWriter
if err := p.startCommand(cmd); err != nil {
t.Fatalf("startCommand: %v", err)
}
if err := p.waitForExit(5 * time.Second); err != nil {
t.Fatalf("child did not exit: %v", err)
}
reports := crashReportsIn(t, dir)
if len(reports) != 1 || !strings.HasPrefix(reports[0], "panic: xray helper panic\n\ngoroutine ") {
t.Fatalf("crash reports = %q, want one holding the whole panic traceback", reports)
}
if got := p.GetResult(); got != "panic: xray helper panic" {
t.Fatalf("GetResult = %q, want the panic header", got)
}
}
// TestStop_RemovesTempConfigFile pins process.go:579 — the
// `if p.configPath != ""` guard that removes the per-run temp config file on
// Stop (so test runs never disturb the main config.json). A mutated guard
@@ -337,5 +365,8 @@ func TestMutationAuditHelper(t *testing.T) {
os.Exit(2)
case "block":
select {}
case "panic":
go func() { panic("xray helper panic") }()
select {}
}
}