From 8006b2c01f7ecd72744407d6b4c9bb009e2aa3f9 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 13 Feb 2020 12:19:41 -0800 Subject: [PATCH] fix(webkit): avoid UnhandledPromiseRejection (#986) * 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 * fix --- src/platform.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index b4c0fea7ba..7015b89519 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); }