Skip to content

Commit

Permalink
Catch NotSupportedException on Process.GetProcessesByName
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Oct 19, 2023
1 parent d944777 commit a3ba686
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
57 changes: 35 additions & 22 deletions AdvancedSharpAdbClient/AdbCommandLineClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,34 @@ public virtual async Task StartServerAsync(CancellationToken cancellationToken =
}

#if HAS_PROCESS && !WINDOWS_UWP
// Starting the adb server failed for whatever reason. This can happen if adb.exe
// is running but is not accepting requests. In that case, try to kill it & start again.
// It kills all processes named "adb", so let's hope nobody else named their process that way.
foreach (Process adbProcess in Process.GetProcessesByName("adb"))
try
{
try
{
adbProcess.Kill();
}
catch (Win32Exception)
// Starting the adb server failed for whatever reason. This can happen if adb.exe
// is running but is not accepting requests. In that case, try to kill it & start again.
// It kills all processes named "adb", so let's hope nobody else named their process that way.
foreach (Process adbProcess in Process.GetProcessesByName("adb"))
{
// The associated process could not be terminated
// or
// The process is terminating.
}
catch (InvalidOperationException)
{
// The process has already exited.
// There is no process associated with this Process object.
try
{
adbProcess.Kill();
}
catch (Win32Exception)
{
// The associated process could not be terminated
// or
// The process is terminating.
}
catch (InvalidOperationException)
{
// The process has already exited.
// There is no process associated with this Process object.
}
}
}
catch (NotSupportedException)
{
// This platform does not support getting a list of processes.
}
#endif

// Try again. This time, we don't call "Inner", and an exception will be thrown if the start operation fails
Expand Down Expand Up @@ -159,23 +166,29 @@ protected virtual async Task<int> RunProcessAsync(string filename, string comman
#if NET5_0_OR_GREATER
using (CancellationTokenSource completionSource = new(TimeSpan.FromMilliseconds(5000)))
{
await process.WaitForExitAsync(completionSource.Token).ConfigureAwait(false);
if (!process.HasExited)
try
{
await process.WaitForExitAsync(completionSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException) when (completionSource.IsCancellationRequested)
{
process.Kill();
if (!process.HasExited)
{
process.Kill();
}
}
}
#else
// get the return code from the process
if (!process.WaitForExit(5000))
{
process.Kill();
}
#endif
// get the return code from the process
return process.ExitCode;
#else
TaskCompletionSource<int> source = new();
source.SetException(new PlatformNotSupportedException());
source.SetException(new PlatformNotSupportedException("This platform is not support System.Diagnostics.Process. You can start adb server by running `adb start-server` manually."));
return await source.Task.ConfigureAwait(false);
#endif
}
Expand Down
43 changes: 25 additions & 18 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,34 @@ public virtual void StartServer()
}

#if HAS_PROCESS && !WINDOWS_UWP
// Starting the adb server failed for whatever reason. This can happen if adb.exe
// is running but is not accepting requests. In that case, try to kill it & start again.
// It kills all processes named "adb", so let's hope nobody else named their process that way.
foreach (Process adbProcess in Process.GetProcessesByName("adb"))
try
{
try
// Starting the adb server failed for whatever reason. This can happen if adb.exe
// is running but is not accepting requests. In that case, try to kill it & start again.
// It kills all processes named "adb", so let's hope nobody else named their process that way.
foreach (Process adbProcess in Process.GetProcessesByName("adb"))
{
adbProcess.Kill();
}
catch (Win32Exception)
{
// The associated process could not be terminated
// or
// The process is terminating.
}
catch (InvalidOperationException)
{
// The process has already exited.
// There is no process associated with this Process object.
try
{
adbProcess.Kill();
}
catch (Win32Exception)
{
// The associated process could not be terminated
// or
// The process is terminating.
}
catch (InvalidOperationException)
{
// The process has already exited.
// There is no process associated with this Process object.
}
}
}
catch (NotSupportedException)
{
// This platform does not support getting a list of processes.
}
#endif

// Try again. This time, we don't call "Inner", and an exception will be thrown if the start operation fails
Expand Down Expand Up @@ -268,7 +275,7 @@ protected virtual int RunProcess(string filename, string command, ICollection<st

return process.ExitCode;
#else
throw new PlatformNotSupportedException();
throw new PlatformNotSupportedException("This platform is not support System.Diagnostics.Process. You can start adb server by running `adb start-server` manually.");
#endif
}

Expand Down

0 comments on commit a3ba686

Please sign in to comment.