Skip to content

Commit 883825e

Browse files
authored
fix(edge): cancel downloads before closing the context (#40034)
1 parent 72310c7 commit 883825e

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

packages/playwright-core/src/server/chromium/crBrowser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ export class CRBrowserContext extends BrowserContext<CREventsMap> {
558558
return 'close-browser';
559559
}
560560

561+
// Ongoing downloads cause crashes in Edge, so cancel them first.
562+
await Promise.all([...this._downloads].map(download => download.cancel().catch(() => {})));
563+
561564
await this._browser._session.send('Target.disposeBrowserContext', { browserContextId: this._browserContextId });
562565
this._browser._contexts.delete(this._browserContextId);
563566
for (const [targetId, serviceWorker] of this._browser._serviceWorkers) {

packages/playwright-core/src/server/download.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,28 @@ import { Artifact } from './artifact';
2323
export class Download {
2424
readonly artifact: Artifact;
2525
readonly url: string;
26+
private _uuid: string;
2627
private _page: Page;
2728
private _suggestedFilename: string | undefined;
2829

2930
constructor(page: Page, downloadsPath: string, uuid: string, url: string, suggestedFilename?: string, downloadFilename?: string) {
3031
const unaccessibleErrorMessage = page.browserContext._options.acceptDownloads === 'deny' ? 'Pass { acceptDownloads: true } when you are creating your browser context.' : undefined;
3132
const downloadPath = path.join(downloadsPath, downloadFilename ?? uuid);
32-
this.artifact = new Artifact(page, downloadPath, unaccessibleErrorMessage, () => {
33-
return this._page.browserContext.cancelDownload(uuid);
34-
});
33+
this.artifact = new Artifact(page, downloadPath, unaccessibleErrorMessage, () => this.cancel());
3534
this._page = page;
3635
this.url = url;
36+
this._uuid = uuid;
3737
this._suggestedFilename = suggestedFilename;
38+
// Note: downloads are never removed from the context, so that we can delete them upon context closure.
3839
page.browserContext._downloads.add(this);
3940
if (suggestedFilename !== undefined)
4041
this._fireDownloadEvent();
4142
}
4243

44+
cancel() {
45+
return this._page.browserContext.cancelDownload(this._uuid);
46+
}
47+
4348
filenameSuggested(suggestedFilename: string) {
4449
assert(this._suggestedFilename === undefined);
4550
this._suggestedFilename = suggestedFilename;

0 commit comments

Comments
 (0)