Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: leaking route handlers when times is used in Page.route (#9234)

Max Schmitt7c89bc162a60
diff --git a/src/client/browserContext.ts b/src/client/browserContext.ts
index 3990c0fa94..10322ebf25 100644
--- a/src/client/browserContext.ts
+++ b/src/client/browserContext.ts
@@ -141,14 +141,20 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
}
_onRoute(route: network.Route, request: network.Request) {
+ let handled = false;
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
routeHandler.handle(route, request);
- return;
+ handled = true;
+ break;
}
}
- // it can race with BrowserContext.close() which then throws since its closed
- route.continue().catch(() => {});
+ if (!handled) {
+ // it can race with BrowserContext.close() which then throws since its closed
+ route.continue().catch(() => {});
+ } else {
+ this._routes = this._routes.filter(route => !route.expired());
+ }
}
async _onBinding(bindingCall: BindingCall) {
diff --git a/src/client/network.ts b/src/client/network.ts
index 2b4713d3ce..9f31cfced1 100644
--- a/src/client/network.ts
+++ b/src/client/network.ts
@@ -626,15 +626,18 @@ export class RouteHandler {
this.handler = handler;
}
+ public expired(): boolean {
+ return !!this._times && this.handledCount >= this._times;
+ }
+
public matches(requestURL: string): boolean {
- if (this._times && this.handledCount >= this._times)
- return false;
return urlMatches(this._baseURL, requestURL, this.url);
}
public handle(route: Route, request: Request) {
this.handler(route, request);
- this.handledCount++;
+ if (this._times)
+ this.handledCount++;
}
}
diff --git a/src/client/page.ts b/src/client/page.ts
index 20ee4c9326..fdedacc84f 100644
--- a/src/client/page.ts
+++ b/src/client/page.ts
@@ -169,13 +169,18 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
}
private _onRoute(route: Route, request: Request) {
+ let handled = false;
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
routeHandler.handle(route, request);
- return;
+ handled = true;
+ break;
}
}
- this._browserContext._onRoute(route, request);
+ if (!handled)
+ this._browserContext._onRoute(route, request);
+ else
+ this._routes = this._routes.filter(route => !route.expired());
}
async _onBinding(bindingCall: BindingCall) {