Skip to content

Commit d745ae0

Browse files
fix(route): wait for all active handlers before disabling interception (#43889)
Co-authored-by: microsoft-playwright-automation[bot] <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com>
1 parent 6e3f5ff commit d745ae0

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎packages/playwright-core/src/client/browserContext.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
242242
async _onRoute(route: network.Route) {
243243
route._context = this;
244244
const page = route.request()._safePage();
245-
const routeHandlers = this._routes.slice();
246-
for (const routeHandler of routeHandlers) {
245+
const routeHandlers = this._routes;
246+
for (const routeHandler of routeHandlers.slice()) {
247247
// If the page or the context was closed we stall all requests right away.
248248
if (page?._closeWasCalled || this.isClosed())
249249
return;
@@ -255,7 +255,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
255255
if (routeHandler.willExpire())
256256
this._routes.splice(index, 1);
257257
const handled = await routeHandler.handle(route);
258-
if (!this._routes.length)
258+
// unrouteAll() replaces _routes and updates interception after waiting for handlers.
259+
if (routeHandlers === this._routes && !this._routes.length)
259260
this._disableInterception().catch(() => {});
260261
if (handled)
261262
return;

‎packages/playwright-core/src/client/page.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
211211

212212
private async _onRoute(route: Route) {
213213
route._context = this.context();
214-
const routeHandlers = this._routes.slice();
215-
for (const routeHandler of routeHandlers) {
214+
const routeHandlers = this._routes;
215+
for (const routeHandler of routeHandlers.slice()) {
216216
// If the page was closed we stall all requests right away.
217217
if (this._closeWasCalled || this._browserContext.isClosed())
218218
return;
@@ -224,7 +224,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
224224
if (routeHandler.willExpire())
225225
this._routes.splice(index, 1);
226226
const handled = await routeHandler.handle(route);
227-
if (!this._routes.length)
227+
// unrouteAll() replaces _routes and updates interception after waiting for handlers.
228+
if (routeHandlers === this._routes && !this._routes.length)
228229
this._disableInterception().catch(() => {});
229230
if (handled)
230231
return;

‎tests/library/unroute-behavior.spec.ts‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,30 +304,38 @@ it('RO-199 route.fulfill should not throw if page has been closed', async ({ pag
304304
await route.fulfill();
305305
});
306306

307-
it('RO-200 should not continue requests in flight (page)', async ({ page, server }) => {
307+
it('RO-200 should not continue requests in flight (page)', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43878' } }, async ({ page, server }) => {
308308
const routePromise = new ManualPromise();
309+
let started = 0;
309310
await page.goto(server.EMPTY_PAGE);
310311
await page.route('**/*', async route => {
311-
routePromise.resolve();
312-
await new Promise(f => setTimeout(f, 3000));
312+
if (++started === 2)
313+
routePromise.resolve();
314+
await routePromise;
315+
await new Promise(f => setTimeout(f, route.request().url().endsWith('/a') ? 100 : 1000));
313316
const response = await route.fetch();
314-
await route.fulfill({ response });
317+
await route.fulfill({ response, body: 'mocked' });
315318
});
316-
void page.evaluate(() => fetch('/')).catch(() => {});
319+
const fetchPromise = page.evaluate(() => Promise.all(['/a', '/b'].map(async path => (await fetch(path)).text())));
317320
await routePromise;
318-
await page.unrouteAll({ behavior: 'wait' });
321+
const [, bodies] = await Promise.all([page.unrouteAll({ behavior: 'wait' }), fetchPromise]);
322+
expect(bodies).toEqual(['mocked', 'mocked']);
319323
});
320324

321-
it('RO-201 should not continue requests in flight (context)', async ({ page, context, server }) => {
325+
it('RO-201 should not continue requests in flight (context)', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43878' } }, async ({ page, context, server }) => {
322326
const routePromise = new ManualPromise();
327+
let started = 0;
323328
await page.goto(server.EMPTY_PAGE);
324329
await context.route('**/*', async route => {
325-
routePromise.resolve();
326-
await new Promise(f => setTimeout(f, 3000));
330+
if (++started === 2)
331+
routePromise.resolve();
332+
await routePromise;
333+
await new Promise(f => setTimeout(f, route.request().url().endsWith('/a') ? 100 : 1000));
327334
const response = await route.fetch();
328-
await route.fulfill({ response });
335+
await route.fulfill({ response, body: 'mocked' });
329336
});
330-
void page.evaluate(() => fetch('/')).catch(() => {});
337+
const fetchPromise = page.evaluate(() => Promise.all(['/a', '/b'].map(async path => (await fetch(path)).text())));
331338
await routePromise;
332-
await context.unrouteAll({ behavior: 'wait' });
339+
const [, bodies] = await Promise.all([context.unrouteAll({ behavior: 'wait' }), fetchPromise]);
340+
expect(bodies).toEqual(['mocked', 'mocked']);
333341
});

0 commit comments

Comments
 (0)