From 73403b323ec0dd8a08c164cb2c07751451215788 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 12 Apr 2024 11:45:32 +0200 Subject: [PATCH] fix: use setImmediate to reduce flakiness when processing events (#12264) --- .../src/node/NodeWebSocketTransport.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/puppeteer-core/src/node/NodeWebSocketTransport.ts b/packages/puppeteer-core/src/node/NodeWebSocketTransport.ts index f4ac592e4f1..13f1e8349cd 100644 --- a/packages/puppeteer-core/src/node/NodeWebSocketTransport.ts +++ b/packages/puppeteer-core/src/node/NodeWebSocketTransport.ts @@ -41,14 +41,18 @@ export class NodeWebSocketTransport implements ConnectionTransport { constructor(ws: NodeWebSocket) { this.#ws = ws; this.#ws.addEventListener('message', event => { - if (this.onmessage) { - this.onmessage.call(null, event.data); - } + setImmediate(() => { + if (this.onmessage) { + this.onmessage.call(null, event.data); + } + }); }); this.#ws.addEventListener('close', () => { - if (this.onclose) { - this.onclose.call(null); - } + setImmediate(() => { + if (this.onclose) { + this.onclose.call(null); + } + }); }); // Silently ignore all errors - we don't know what to do with them. this.#ws.addEventListener('error', () => {});