Skip to content

Commit

Permalink
Ensure time is provided AFTER process state has been asserted
Browse files Browse the repository at this point in the history
  • Loading branch information
Naatan committed Jun 28, 2024
1 parent e176b0b commit ac3b175
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions termtest_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ 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.
exists, err := gopsutil.PidExists(int32(tt.Cmd().Process.Pid))
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
}
Expand Down

0 comments on commit ac3b175

Please sign in to comment.