Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3f79c35

Browse files
committed
Fix Process.WaitForExit premature timeout on Unix
The Process.WaitForExit implementation on Unix is mixing up units, resulting in WaitForExits with timeouts exiting much earlier than the specified timeout. The code is using Stopwatch.GetTimestamp to get a current timestamp, subtracting the current timestamp from one gathered when the wait started, and then subtracting that difference from the specified timeout. But the specified timeout is in milliseconds, and the timestamp is in ticks. The fix just converts the difference from ticks to milliseconds before subtracing from the timeout value.
1 parent 0b34bdc commit 3f79c35

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

src/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ internal bool WaitForExit(int millisecondsTimeout)
367367
Debug.Assert(!Monitor.IsEntered(_gate));
368368

369369
// Track the time the we start waiting.
370-
long startTime = GetTimestamp();
370+
long startTime = Stopwatch.GetTimestamp();
371371

372372
// Polling loop
373373
while (true)
@@ -379,7 +379,7 @@ internal bool WaitForExit(int millisecondsTimeout)
379379
// We're in a polling loop... determine how much time remains
380380
int remainingTimeout = millisecondsTimeout == Timeout.Infinite ?
381381
Timeout.Infinite :
382-
(int)Math.Max(millisecondsTimeout - (GetTimestamp() - startTime), 0);
382+
(int)Math.Max(millisecondsTimeout - ((Stopwatch.GetTimestamp() - startTime) / (double)Stopwatch.Frequency * 1000), 0);
383383

384384
lock (_gate)
385385
{
@@ -511,11 +511,5 @@ internal bool WaitForExit(int millisecondsTimeout)
511511
});
512512
}
513513

514-
/// <summary>Gets a current time stamp.</summary>
515-
private static long GetTimestamp()
516-
{
517-
return Stopwatch.GetTimestamp();
518-
}
519-
520514
}
521515
}

0 commit comments

Comments
 (0)