From 24ff282afea003fa16b0cf616104cddd878df871 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 16 Sep 2026 15:05:25 +0000 Subject: [PATCH] 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 Change-Id: I1a3c5e7f9b2d4f6a8c0e2b4d6f8a0c2e4b6d8f0a --- util/winutil/s4u/s4u_windows.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/util/winutil/s4u/s4u_windows.go b/util/winutil/s4u/s4u_windows.go index 194b89f7c..d25aa1ced 100644 --- a/util/winutil/s4u/s4u_windows.go +++ b/util/winutil/s4u/s4u_windows.go @@ -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 {