From d89787d6c35c8162a6cecf278601707d769e4884 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 13 Feb 2020 12:15:22 -0800 Subject: [PATCH 1/2] fix(webkit): avoid UnhandledPromiseRejection If we close a browser before sending first websocket message, the `this._connect` promise will reject and will not be handled. This surfaced at https://github.com/microsoft/playwright/pull/976/checks?check_run_id=444445350 --- src/platform.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index b4c0fea7baade..0624db79c26ae 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -316,16 +316,16 @@ export class WebSocketTransport implements ConnectionTransport { onmessage?: (message: string) => void; onclose?: () => void; - private _connect: Promise; + private _connectPromise: Promise<(Error|null)>; constructor(url: string) { this._ws = (isNode ? new NodeWebSocket(url, [], { perMessageDeflate: false, maxPayload: 256 * 1024 * 1024, // 256Mb }) : new WebSocket(url)) as WebSocket; - this._connect = new Promise((fulfill, reject) => { - this._ws.addEventListener('open', () => fulfill()); - this._ws.addEventListener('error', event => reject(new Error('WebSocket error: ' + (event as ErrorEvent).message))); + this._connectPromise = new Promise((fulfill => { + this._ws.addEventListener('open', () => fulfill(null)); + this._ws.addEventListener('error', event => fulfill(new Error('WebSocket error: ' + (event as ErrorEvent).message))); }); // The 'ws' module in node sometimes sends us multiple messages in a single task. // In Web, all IO callbacks (e.g. WebSocket callbacks) @@ -349,7 +349,9 @@ export class WebSocketTransport implements ConnectionTransport { } async send(message: string) { - await this._connect; + const error = await this._connectPromise; + if (error) + throw error; this._ws.send(message); } From 8a57c208be8b791c506d2e1f9c15fbb4a9204d83 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 13 Feb 2020 12:17:15 -0800 Subject: [PATCH 2/2] fix --- src/platform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform.ts b/src/platform.ts index 0624db79c26ae..7015b895193c4 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -323,7 +323,7 @@ export class WebSocketTransport implements ConnectionTransport { perMessageDeflate: false, maxPayload: 256 * 1024 * 1024, // 256Mb }) : new WebSocket(url)) as WebSocket; - this._connectPromise = new Promise((fulfill => { + this._connectPromise = new Promise(fulfill => { this._ws.addEventListener('open', () => fulfill(null)); this._ws.addEventListener('error', event => fulfill(new Error('WebSocket error: ' + (event as ErrorEvent).message))); });