private static async Task StopSessionAsync(EventPipeSession session) { // Cancel after a generous amount of time if process ended before command is sent. using CancellationTokenSource cancellationSource = new(IpcClient.ConnectTimeout); try { await session.StopAsync(cancellationSource.Token).ConfigureAwait(false); } catch (EndOfStreamException) { // If the app we're monitoring exits abruptly, this may throw in which case we just swallow the exception and exit gracefully. } // We may time out if the process ended before we sent StopTracing command. We can just exit in that case. catch (TimeoutException) { } // We may time out if the process ended before we sent StopTracing command. We can just exit in that case. catch (OperationCanceledException) { } // On Unix platforms, we may actually get a PNSE since the pipe is gone with the process, and Runtime Client Library // does not know how to distinguish a situation where there is no pipe to begin with, or where the process has exited // before collection started and got rid of a pipe that once existed. // Since we are catching this at the end of a session we know that the pipe once existed (otherwise the exception would've // been thrown at the beginning directly) catch (PlatformNotSupportedException) { } // On non-abrupt exits, the socket may be already closed by the runtime and we won't be able to send a stop request through it. catch (ServerNotAvailableException) { } }