Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});