Skip to content

Commit 161c0f4

Browse files
authored
Process exit improvements (#145)
* Check if process is running before killing it. * Grab the output from the output variable instead of trying to read the stream again.
1 parent 62c583a commit 161c0f4

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
6+
namespace PuppeteerSharp.Tests.Issues
7+
{
8+
[Collection("PuppeteerLoaderFixture collection")]
9+
public class Issue0128
10+
{
11+
[Fact]
12+
public async Task LauncherShouldFailGracefully()
13+
{
14+
await Assert.ThrowsAsync<ChromeProcessException>(async () =>
15+
{
16+
var options = TestConstants.DefaultBrowserOptions();
17+
options.Args = new[] { "-remote-debugging-port=-2" };
18+
await PuppeteerSharp.Puppeteer.LaunchAsync(options, TestConstants.ChromiumRevision);
19+
});
20+
}
21+
}
22+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
namespace PuppeteerSharp
1+
using System;
2+
3+
namespace PuppeteerSharp
24
{
35
public class ChromeProcessException : PuppeteerException
46
{
57
public ChromeProcessException(string message) : base(message)
68
{
79
}
10+
11+
public ChromeProcessException(string message, Exception innerException) : base(message, innerException)
12+
{
13+
14+
}
815
}
916
}

lib/PuppeteerSharp/Launcher.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Launcher
4646
private LaunchOptions _options;
4747
private TaskCompletionSource<bool> _waitForChromeToClose;
4848
private static int _processCount = 0;
49-
49+
private bool _processLoaded;
5050
private const string UserDataDirArgument = "--user-data-dir";
5151
#endregion
5252

@@ -153,6 +153,7 @@ public async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevisi
153153
var keepAliveInterval = options.KeepAliveInterval;
154154

155155
_connection = await Connection.Create(browserWSEndpoint, connectionDelay, keepAliveInterval);
156+
_processLoaded = true;
156157

157158
if (options.LogProcess)
158159
{
@@ -164,11 +165,11 @@ public async Task<Browser> LaunchAsync(LaunchOptions options, int chromiumRevisi
164165
catch (Exception ex)
165166
{
166167
ForceKillChrome();
167-
throw new Exception("Failed to create connection", ex);
168+
throw new ChromeProcessException("Failed to create connection", ex);
168169
}
169170

170171
}
171-
172+
172173
public async Task TryDeleteUserDataDir(int times = 10, TimeSpan? delay = null)
173174
{
174175
if (!IsChromeClosed)
@@ -225,7 +226,7 @@ public static string GetTemporaryDirectory()
225226
#endregion
226227

227228
#region Private methods
228-
229+
229230
private Task<string> WaitForEndpoint(Process chromeProcess, int timeout, bool dumpio)
230231
{
231232
var taskWrapper = new TaskCompletionSource<string>();
@@ -236,10 +237,14 @@ private Task<string> WaitForEndpoint(Process chromeProcess, int timeout, bool du
236237

237238
EventHandler exitedEvent = (sender, e) =>
238239
{
240+
if (_options.LogProcess && !_processLoaded)
241+
{
242+
Console.WriteLine($"PROCESS COUNT: {Interlocked.Increment(ref _processCount)}");
243+
}
244+
239245
CleanUp();
240246

241-
var error = chromeProcess.StandardError.ReadToEnd();
242-
taskWrapper.SetException(new ChromeProcessException($"Failed to launch chrome! {error}"));
247+
taskWrapper.SetException(new ChromeProcessException($"Failed to launch chrome! {output}"));
243248
};
244249

245250
chromeProcess.ErrorDataReceived += (sender, e) =>
@@ -340,7 +345,7 @@ private void ForceKillChrome()
340345
{
341346
try
342347
{
343-
if (_chromeProcess.Id != 0 && Process.GetProcessById(_chromeProcess.Id) != null)
348+
if (_chromeProcess.Id != 0 && !_chromeProcess.HasExited && Process.GetProcessById(_chromeProcess.Id) != null)
344349
{
345350
_chromeProcess.Kill();
346351
_chromeProcess.WaitForExit();

0 commit comments

Comments
 (0)