Skip to content
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

Fix for InvalidOperationException thrown by Process.Exited #941

Merged
merged 5 commits into from Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -50,7 +50,15 @@ public object LaunchProcess(string processPath, string arguments, string working
{
// Call WaitForExit again to ensure all streams are flushed
var p = sender as Process;
p.WaitForExit();
try
{
p.WaitForExit();
}
catch (InvalidOperationException)
{
}

// If exit callback has code that access Process object, ensure that the exceptions handling should be done properly.
exitCallBack(p);
};
}
Expand Down Expand Up @@ -104,10 +112,16 @@ public string GetProcessName(int processId)
public bool TryGetExitCode(object process, out int exitCode)
{
var proc = process as Process;
if (proc != null && proc.HasExited)
try
{
if (proc != null && proc.HasExited)
{
exitCode = proc.ExitCode;
return true;
}
}
catch (InvalidOperationException)
{
exitCode = proc.ExitCode;
return true;
}

exitCode = 0;
Expand All @@ -121,18 +135,24 @@ public void SetExitCallback(int processId, Action callbackAction)

process.EnableRaisingEvents = true;
process.Exited += (sender, args) =>
{
callbackAction.Invoke();
};
{
callbackAction.Invoke();
};
}

/// <inheritdoc/>
public void TerminateProcess(object process)
{
var proc = process as Process;
if (proc != null && !proc.HasExited)
try
{
if (proc != null && !proc.HasExited)
{
proc.Kill();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return Win32Exception as well if the process is terminating?
Shouldn't we catch that too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, we should bubble up Win32Exception because this exception can also be thrown if process cannot be terminated.

}
}
catch (InvalidOperationException)
{
proc.Kill();
}
}
}
Expand Down
Expand Up @@ -61,7 +61,16 @@ public static void ErrorReceivedCallback(StringBuilder testHostProcessStdError,
EqtTrace.Error("Test host exited with error: '{0}'", testHostProcessStdErrorStr);
}

onHostExited(new HostProviderEventArgs(testHostProcessStdErrorStr, exitCode, (process as Process).Id));
int procId = -1;
try
{
procId = (process as Process).Id;
}
catch (InvalidOperationException)
{
}

onHostExited(new HostProviderEventArgs(testHostProcessStdErrorStr, exitCode, procId));
}
}
}