util/winutil/s4u: drain the pseudoconsole relay's output before exiting

After the relayed process exited, beRelay closed the Process, which closed
the pseudoconsole's output pipe while the goroutine copying that output to
the relay's stdout might still be reading it, and then exited. Any output
still in the pseudoconsole at that point was lost. For a short command
that was all of it: "ssh -t host echo hello" against a domain controller
test node printed nothing in 10 of 10 tries.

Release the process instead, which closes the pseudoconsole and so lets
the output pipe reach EOF, wait for the copy to finish, and only then close
the pipes and exit.

Updates #4697

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I1a3c5e7f9b2d4f6a8c0e2b4d6f8a0c2e4b6d8f0a
This commit is contained in:
Brad Fitzpatrick committed 2026-11-13 02:13:25 +00:00
1 parent dbb24ee255
commit 24ff282afe
1 file changed
+18 -4
+18 -4
View File
@@ -965,12 +965,19 @@ func beRelay(args []string) error {
defer psp.Close()
go resizeLoop(logf, resize, psp.PTYResizer())
outDone := make(chan struct{})
go func() {
defer close(outDone)
if debugRelay {
debugLogPTYOutput(logf, os.Stdout, psp.rStdout)
} else {
io.Copy(os.Stdout, psp.rStdout)
}
}()
if debugRelay {
go debugLogPTYInput(logf, psp.wStdin, os.Stdin)
go debugLogPTYOutput(logf, os.Stdout, psp.rStdout)
} else {
go io.Copy(psp.wStdin, os.Stdin)
go io.Copy(os.Stdout, psp.rStdout)
}
exitCode, err := psp.Wait()
@@ -982,10 +989,17 @@ func beRelay(args []string) error {
logf("relayed process returned %v", exitCode)
}
if err := psp.Close(); err != nil {
logf("s4u.Process.Close error: %v", err)
// The process has exited, but its last output may still be in the
// pseudoconsole. Release closes the pseudoconsole, which is what makes
// the output pipe reach EOF; drain it before exiting, or the tail of
// the output of a short command ("echo hello") is lost.
if err := psp.Release(); err != nil {
logf("s4u.Process.Release error: %v", err)
return err
}
<-outDone
psp.rStdout.Close()
psp.wStdin.Close()
// The relay stands in for the process it ran, so its exit code is
// that process's exit code.
if exitCode != 0 {