From 8477105e09ee12ffc1e9aed01301c2f6a411a920 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 29 Jul 2022 12:42:25 +0200 Subject: [PATCH] chore: refactor init to make sure it works with paused targets (#8710) --- src/common/NetworkManager.ts | 41 ++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/common/NetworkManager.ts b/src/common/NetworkManager.ts index 12673d96d32..e675b31dca1 100644 --- a/src/common/NetworkManager.ts +++ b/src/common/NetworkManager.ts @@ -22,7 +22,12 @@ import {Frame} from './FrameManager.js'; import {HTTPRequest} from './HTTPRequest.js'; import {HTTPResponse} from './HTTPResponse.js'; import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js'; -import {debugError, isString} from './util.js'; +import { + debugError, + isString, + createDeferredPromiseWithTimer, + DeferredPromise, +} from './util.js'; /** * @public @@ -95,6 +100,7 @@ export class NetworkManager extends EventEmitter { download: -1, latency: 0, }; + #deferredInitPromise?: DeferredPromise; constructor( client: CDPSession, @@ -131,13 +137,34 @@ export class NetworkManager extends EventEmitter { ); } - async initialize(): Promise { - await this.#client.send('Network.enable'); - if (this.#ignoreHTTPSErrors) { - await this.#client.send('Security.setIgnoreCertificateErrors', { - ignore: true, - }); + /** + * Initialize calls should avoid async dependencies between CDP calls as those + * might not resolve until after the target is resumed causing a deadlock. + */ + initialize(): Promise { + if (this.#deferredInitPromise) { + return this.#deferredInitPromise.promise; } + this.#deferredInitPromise = createDeferredPromiseWithTimer( + 'NetworkManager initialization timed out', + 30000 + ); + const init = Promise.all([ + this.#ignoreHTTPSErrors + ? this.#client.send('Security.setIgnoreCertificateErrors', { + ignore: true, + }) + : null, + this.#client.send('Network.enable'), + ]); + init + .then(() => { + this.#deferredInitPromise?.resolve(); + }) + .catch(err => { + return this.#deferredInitPromise?.reject(err); + }); + return this.#deferredInitPromise.promise; } async authenticate(credentials?: Credentials): Promise {