fix(close): fix the browser.close race (#3956)

This commit is contained in:
Pavel Feldman 2020-09-22 12:50:39 -07:00 committed by GitHub
parent c4a2732515
commit 0e76316f82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View file

@ -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;
@ -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;
}
}

View file

@ -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();
});
});