Skip to content

Commit 56048b5

Browse files
authored
[browser][wbt] Detect when browser gets disconnected and do not try to dispose. (#101530)
1 parent 5e41dde commit 56048b5

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,17 @@ public async Task<IBrowser> SpawnBrowserAsync(
109109
// codespaces: ignore certificate error -> Microsoft.Playwright.PlaywrightException : net::ERR_CERT_AUTHORITY_INVALID
110110
string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}", "--ignore-certificate-errors" };
111111
_testOutput.WriteLine($"Launching chrome ('{s_chromePath.Value}') via playwright with args = {string.Join(',', chromeArgs)}");
112-
return Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
112+
Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
113113
ExecutablePath = s_chromePath.Value,
114114
Headless = headless,
115115
Args = chromeArgs
116116
});
117+
Browser.Disconnected += (sender, e) =>
118+
{
119+
Browser = null;
120+
_testOutput.WriteLine("Browser has been disconnected");
121+
};
122+
return Browser;
117123
}
118124

119125
// FIXME: options
@@ -196,8 +202,21 @@ public async Task WaitForProcessExitAsync(TimeSpan timeout)
196202

197203
public async ValueTask DisposeAsync()
198204
{
199-
if (Browser is not null)
200-
await Browser.DisposeAsync();
201-
Playwright?.Dispose();
205+
try
206+
{
207+
if (Browser is not null)
208+
{
209+
await Browser.DisposeAsync();
210+
Browser = null;
211+
}
212+
}
213+
catch (PlaywrightException ex)
214+
{
215+
_testOutput.WriteLine($"PlaywrightException occurred during DisposeAsync: {ex.Message}");
216+
}
217+
finally
218+
{
219+
Playwright?.Dispose();
220+
}
202221
}
203222
}

0 commit comments

Comments
 (0)