From ce969142922da86004142edc892e03c762a89645 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Fri, 16 Apr 2021 11:14:57 -0700 Subject: [PATCH] fix(remote): unregister selectors after client disconnect (#6195) --- src/remote/playwrightServer.ts | 9 +++++++-- src/server/selectors.ts | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/remote/playwrightServer.ts b/src/remote/playwrightServer.ts index b7ae6a6c6f..1ee420b456 100644 --- a/src/remote/playwrightServer.ts +++ b/src/remote/playwrightServer.ts @@ -21,6 +21,7 @@ import { DispatcherConnection, DispatcherScope } from '../dispatchers/dispatcher import { PlaywrightDispatcher } from '../dispatchers/playwrightDispatcher'; import { createPlaywright } from '../server/playwright'; import { gracefullyCloseAll } from '../server/processLauncher'; +import { serverSelectors } from '../server/selectors'; const debugLog = debug('pw:server'); @@ -37,13 +38,17 @@ export class PlaywrightServer { private _delegate: PlaywrightServerDelegate; static async startDefault(port: number = 0): Promise { + const cleanup = async () => { + await gracefullyCloseAll().catch(e => {}); + serverSelectors.unregisterAll(); + }; const delegate: PlaywrightServerDelegate = { path: '/ws', allowMultipleClients: false, - onClose: gracefullyCloseAll, + onClose: cleanup, onConnect: (rootScope: DispatcherScope) => { new PlaywrightDispatcher(rootScope, createPlaywright()); - return () => gracefullyCloseAll().catch(e => {}); + return cleanup; }, }; const server = new PlaywrightServer(delegate); diff --git a/src/server/selectors.ts b/src/server/selectors.ts index f241ff00a8..ce65805ae9 100644 --- a/src/server/selectors.ts +++ b/src/server/selectors.ts @@ -55,6 +55,10 @@ export class Selectors { this._engines.set(name, { source, contentScript }); } + unregisterAll() { + this._engines.clear(); + } + async _query(frame: frames.Frame, selector: string, scope?: dom.ElementHandle): Promise | null> { const info = this._parseSelector(selector); const context = await frame._context(info.world);