pii: a GPU pf-serve that died says why, not "read |0: file already closed" (#3741)

TestGPUDeathMovesToCPU went red on macOS CI at 778b8e075 with GPUError
"read |0: file already closed". pf-serve's stdout came from
cmd.StdoutPipe, and the goroutine that waits for pf-serve called
cmd.Wait, which closes that pipe's read end once the process exits. When
pf-serve died answering (a lost GPU) and Wait won the race with the read,
the read got os.ErrClosed instead of EOF; reader.fail takes only EOF for
a death, so the raw pipe error became the reason. Users saw it too:
Settings' privacy-filter status showed "file already closed" as why the
GPU was lost, and any answer pf-serve wrote just before exiting could be
dropped with the pipe.

Fix: stdout is magpie's own os.Pipe. pf-serve gets the write end, which
magpie closes after Start, so a read after the exit sees EOF and the
answer or death message is read whole; kill and end close the read end.
A beforeRead seam lets TestGPUDeathReadAfterExit read only after the GPU
pf-serve has exited and been waited for, so the ordering is red without
the fix every time.

Run on mote at this tree (based on 778b8e075):
- go test -race -count=20 -timeout 45m ./internal/pii/...: ok (pii
  1578s). The first try at the default 10m timed out mid-run in
  TestIdleServeEnds (1s in, no hang in the stacks): count=20 under race
  is longer than 10m.
- go test -timeout 45m ./internal/pii/... ./internal/gateway/...: ok
  (gateway 810s).
- gofmt -l internal/pii: empty; GOOS=linux go vet -tags nogui ./...: ok;
  GOOS=windows go vet ./...: ok; go vet ./internal/pii/...: ok.

Broken by hand:
- Old code with a 300ms pause before the read: TestGPUDeathMovesToCPU
  red with the CI's exact "GPUError:read |0: file already closed".
  With the fix and the same pause: green 3 of 3.
- Fix kept but the read end closed right after cmd.Wait (what
  StdoutPipe did): TestGPUDeathMovesToCPU and TestGPUDeathReadAfterExit
  both red with that message.

The issue's four ubuntu tests (TestWarm, TestFindJoinsTextsAndMapsBack,
TestServeStartsWithoutResidencyHeartbeat, TestGPUDeathMovesToCPU's
starts) had another cause, fixed in c84f859dd; ubuntu is green at
778b8e075.

Not tried: a real GPU death on a real pf-serve; the fake's death is
the same exit-after-stderr.

Fixes #3741
This commit is contained in:
yetone
2026-11-08 06:51:03 +08:00
parent 44b762d505
commit 1cb3dc435a
2 changed files with 46 additions and 6 deletions
+25 -6
View File
@@ -35,9 +35,15 @@ type hello struct {
// helper is one pf-serve running.
type helper struct {
cmd *exec.Cmd
in io.WriteCloser
out *bufio.Reader
cmd *exec.Cmd
in io.WriteCloser
out *bufio.Reader
// outf is out's end of the stdout pipe. It is magpie's, not
// cmd's: cmd.Wait closes a StdoutPipe as the process exits, so a
// read after a death got "read |0: file already closed" in place of
// the EOF that says pf-serve died, and the status showed that as why
// the GPU was lost (#3741). Closed by kill and end.
outf *os.File
stderr *tail
hello hello
exited chan struct{}
@@ -278,13 +284,19 @@ func startOne(path string, cpu bool, stop <-chan struct{}) (*helper, error) {
if err != nil {
return nil, err
}
out, err := cmd.StdoutPipe()
out, w, err := os.Pipe()
if err != nil {
in.Close()
return nil, err
}
h := &helper{cmd: cmd, in: in, out: bufio.NewReaderSize(out, 64<<10), stderr: &tail{}, exited: make(chan struct{})}
cmd.Stdout = w
h := &helper{cmd: cmd, in: in, out: bufio.NewReaderSize(out, 64<<10), outf: out, stderr: &tail{}, exited: make(chan struct{})}
cmd.Stderr = h.stderr
if err := cmd.Start(); err != nil {
err = cmd.Start()
// pf-serve has its own copy: once it exits, a read here sees EOF
w.Close()
if err != nil {
out.Close()
return nil, err
}
go func() {
@@ -389,6 +401,7 @@ func (h *helper) kill() {
h.cmd.Process.Kill()
}
<-h.exited
h.outf.Close()
}
// Warm starts pf-serve in the background, where the model is on disk, so
@@ -462,6 +475,7 @@ func end(h *helper) {
h.in.Close()
select {
case <-h.exited:
h.outf.Close()
case <-time.After(5 * time.Second):
h.kill()
}
@@ -639,6 +653,7 @@ func (h *helper) classify(texts []string) ([][]redact.Found, error) {
if _, err := h.in.Write(b.Bytes()); err != nil {
return nil, fmt.Errorf("pf-serve %s (%v)", h.died(), err)
}
beforeRead(h)
r := &reader{r: h.out}
status := r.u32()
if status == 1 {
@@ -671,6 +686,10 @@ func (h *helper) classify(texts []string) ([][]redact.Found, error) {
return out, nil
}
// beforeRead runs between sending a request and reading its answer, for
// a test to read after pf-serve has exited (TestGPUDeathReadAfterExit).
var beforeRead = func(*helper) {}
type reader struct {
r *bufio.Reader
err error
+21
View File
@@ -111,6 +111,27 @@ func TestGPUDeathMovesToCPU(t *testing.T) {
}
}
// The answer of a GPU pf-serve that died is read only after it has
// exited and been waited for, and the status still says the GPU was lost,
// not "read |0: file already closed" (#3741: cmd.Wait closed the pipe
// under the read).
func TestGPUDeathReadAfterExit(t *testing.T) {
old := beforeRead
beforeRead = func(h *helper) {
if h.hello.GPU {
<-h.exited
}
}
t.Cleanup(func() { beforeRead = old })
gpuInstall(t, "gpudies")
if _, err := Find([]string{"hi Alice"}); err != nil {
t.Fatal(err)
}
if st := Now(); st.Device != "CPU" || !strings.Contains(st.GPUError, "device lost") {
t.Fatalf("Now = %+v, want on the CPU saying the GPU was lost", st)
}
}
// A pf-serve-vulkan that can't start (no Vulkan loader) leaves it to
// pf-serve.
func TestVulkanThatWontStartFallsBack(t *testing.T) {