Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/PuppeteerSharp/Cdp/CdpBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ internal async Task<IPage> CreatePageInContextAsync(string contextId, CreatePage
WindowState = windowBounds?.WindowState,

// Works around crbug.com/454825274.
NewWindow = hasTargets && options?.Type == CreatePageType.Window ? true : null,
// When no targets exist (e.g. all pages were closed in headful mode), Chrome has no
// browser window. We must request a new window so Chrome can create the page.
NewWindow = (!hasTargets || options?.Type == CreatePageType.Window) ? true : null,
Background = options?.Background,
};

Expand All @@ -357,6 +359,7 @@ internal async Task<IPage> CreatePageInContextAsync(string contextId, CreatePage

var targetId = (await Connection.SendAsync<TargetCreateTargetResponse>("Target.createTarget", createTargetRequest)
.ConfigureAwait(false)).TargetId;

var target = await WaitForTargetAsync(t => ((CdpTarget)t).TargetId == targetId).ConfigureAwait(false) as CdpTarget;
await target!.InitializedTask.ConfigureAwait(false);
return await target.PageAsync().ConfigureAwait(false);
Expand Down
13 changes: 12 additions & 1 deletion lib/PuppeteerSharp/Cdp/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,18 @@ public void Dispose()
_callbacks[id] = callback;
}

await RawSendAsync(message, options).ConfigureAwait(false);
try
{
await RawSendAsync(message, options).ConfigureAwait(false);
}
catch (System.Net.WebSockets.WebSocketException)
{
// The WebSocket was aborted (e.g. Chrome exited) between the IsClosed check and the send.
// Treat this as a closed connection so callers get TargetClosedException instead.
Close("WebSocket error");
throw new TargetClosedException($"Protocol error({method}): Target closed.", CloseReason);
}

return waitForCallback ? await callback.TaskWrapper.Task.WithTimeout(ProtocolTimeout).ConfigureAwait(false) : null;
}

Expand Down
Loading