-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid throwing an Exception in ToString when a process has already exited #43597
Changes from all commits
6c65eb7
7857375
9dba39a
b535d34
661b963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,6 +460,21 @@ public void TestHasExited() | |
} | ||
} | ||
|
||
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
public void TestToString_OnExitedProcess() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, does this test fail on master branch? With .NET5, this program: using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Process p = Process.Start("bash", "-c \"sleep 100000\"");
Console.WriteLine(p.ProcessName);
p.Kill();
Console.WriteLine(p.ToString());
}
} displays:
on Linux and macOS, with return code 0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @am11 sorry for the delay - yes, the above looks right, in the test we do:
In your example, the behaviour is expected, because when you call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @avodovnik, ah I missed the WaitForExit. With
however, with subprocess
(and without WaitForExit, it returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the PR follows our own guidelines on not throwing exceptions in |
||
{ | ||
Process p = CreateDefaultProcess(); | ||
var name = p.ProcessName; | ||
Assert.Equal($"System.Diagnostics.Process ({name})", p.ToString()); | ||
|
||
p.Kill(); | ||
Assert.True(p.WaitForExit(WaitInMS)); | ||
|
||
// Ensure ToString does not throw an exception, but still returns | ||
// a representation of the object. | ||
Assert.Equal("System.Diagnostics.Process", p.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void HasExited_GetNotStarted_ThrowsInvalidOperationException() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this try/catch still needed? What might throw here if the process exits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub
ProcessManager.GetProcessInfo
can still throw an exception, IIRC - that's why I left thecatch
block there