From ac3b1758edbaaee4dbdf6e8a4d7553f55c024e5e Mon Sep 17 00:00:00 2001 From: Nathan Rijksen Date: Fri, 28 Jun 2024 11:53:44 -0700 Subject: [PATCH] Ensure time is provided AFTER process state has been asserted --- termtest_windows.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/termtest_windows.go b/termtest_windows.go index c01241f..68ca81d 100644 --- a/termtest_windows.go +++ b/termtest_windows.go @@ -27,11 +27,6 @@ func (tt *TermTest) waitIndefinitely() error { tt.opts.Logger.Printf("Waiting for PID %d to exit\n", tt.Cmd().Process.Pid) for { - // There is a race condition here; which is that the pty could still be processing the last of the output - // when the process exits. This sleep tries to work around this, but on slow hosts this may not be sufficient. - // This also gives some time in between process lookups - time.Sleep(100 * time.Millisecond) - // For some reason os.Process will always return a process even when the process has exited. // According to the docs this shouldn't happen, but here we are. // Using gopsutil seems to correctly identify the (not) running process. @@ -39,6 +34,14 @@ func (tt *TermTest) waitIndefinitely() error { if err != nil { return fmt.Errorf("could not find process: %d: %w", tt.Cmd().Process.Pid, err) } + + // There is a race condition here; which is that the pty could still be processing the last of the output + // when the process exits. This sleep tries to work around this, but on slow hosts this may not be sufficient. + // We want this after the process state is asserted, but before we break out, to ensure we give at least the + // specified time AFTER the process has exited. + // This also povides time between process lookups. + time.Sleep(100 * time.Millisecond) + if !exists { break }