Skip to content

Commit

Permalink
fix(close): fix the browser.close race (#3956)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Sep 22, 2020
1 parent c4a2732 commit 0e76316
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/client/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { headersObjectToArray } from '../utils/utils';
export class Browser extends ChannelOwner<channels.BrowserChannel, channels.BrowserInitializer> {
readonly _contexts = new Set<BrowserContext>();
private _isConnected = true;
private _isClosedOrClosing = false;
private _closedPromise: Promise<void>;
_isRemote = false;

Expand Down Expand Up @@ -83,18 +82,22 @@ export class Browser extends ChannelOwner<channels.BrowserChannel, channels.Brow
}

async close(): Promise<void> {
return this._wrapApiCall('browser.close', async () => {
if (!this._isClosedOrClosing) {
this._isClosedOrClosing = true;
try {
await this._wrapApiCall('browser.close', async () => {
await this._channel.close();
}
await this._closedPromise;
});
await this._closedPromise;
});
} catch (e) {
if (e.message === 'browser.close: Browser has been closed')
return;
if (e.message === 'browser.close: Target browser or context has been closed')
return;
throw e;
}
}

_didClose() {
this._isConnected = false;
this.emit(Events.Browser.Disconnected);
this._isClosedOrClosing = true;
}
}
10 changes: 10 additions & 0 deletions test/browsertype-connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,14 @@ describe('connect', suite => {

await browser1.close();
});

it('should not throw on close after disconnect', async ({browserType, remoteServer, server}) => {
const remote = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
await remote.newPage();
await Promise.all([
new Promise(f => remote.on('disconnected', f)),
remoteServer.close()
]);
await remote.close();
});
});

0 comments on commit 0e76316

Please sign in to comment.