Description
Version
1.65.1 (also 1.64.0)
Steps to reproduce
Save the file below as RouteBurst.java and run it with the Playwright jars on the classpath:
java -cp "<playwright 1.65.1 classpath>" RouteBurst.java 3000
import com.microsoft.playwright.*;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
public class RouteBurst {
public static void main(String[] args) throws Exception {
int n = args.length > 0 ? Integer.parseInt(args[0]) : 3000;
boolean routed = args.length < 2 || !"noroute".equals(args[1]);
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/r", ex -> {
byte[] body = "ok".getBytes(StandardCharsets.UTF_8);
ex.sendResponseHeaders(200, body.length);
ex.getResponseBody().write(body);
ex.close();
});
server.createContext("/", ex -> {
String html = "<script>window.done = false;"
+ "Promise.allSettled(Array.from({length: " + n + "}, (_, i) => fetch('/r?i=' + i)))"
+ ".then(() => { window.done = true; });</script>";
byte[] body = html.getBytes(StandardCharsets.UTF_8);
ex.getResponseHeaders().add("Content-Type", "text/html");
ex.sendResponseHeaders(200, body.length);
ex.getResponseBody().write(body);
ex.close();
});
server.setExecutor(Executors.newFixedThreadPool(16));
server.start();
int[] handled = {0};
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
if (routed) {
page.route("**/*", route -> { handled[0]++; route.resume(); });
}
page.navigate("http://localhost:" + server.getAddress().getPort() + "/");
page.waitForFunction("window.done === true");
System.out.println("OK requests=" + n + " handled=" + handled[0]);
} catch (StackOverflowError e) {
System.out.println("StackOverflowError requests=" + n + " handled=" + handled[0]);
} finally {
server.stop(0);
}
System.exit(0);
}
}The page makes 3000 fetch calls. One page.route("**/*", ...) handler resumes each request.
Expected behavior
OK requests=3000 handled=3001, as for 1000 requests.
Actual behavior
StackOverflowError requests=3000 handled=1554
5 of 5 runs fail, each after about 1550 handled routes. Without the route (RouteBurst.java 3000 noroute) the run passes. The same occurs with browserContext.route.
With a pool of 100 workers in the page in place of one burst, the failure is intermittent (2 of 6 runs at 5000 to 10000 requests), because the depth depends on how many route events are in the queue.
Cause
After each handled route, PageImpl.handleEvent and BrowserContextImpl.handleRoute call updateInterceptionPatterns(). That call sends setNetworkInterceptionPatterns and blocks until the answer arrives. While it blocks, runUntil dispatches the next message, which is the next route event. So each queued route event runs one level deeper on the same stack. A stack trace from inside the handler (1.65.1) shows the cycle:
user handler
com.microsoft.playwright.impl.Router$RouteInfo.handle(Router.java:48)
com.microsoft.playwright.impl.Router.handle(Router.java:86)
com.microsoft.playwright.impl.PageImpl.handleEvent(PageImpl.java:226)
com.microsoft.playwright.impl.Connection.dispatch(Connection.java:347)
com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:261)
com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:154)
com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:157)
com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:142)
com.microsoft.playwright.impl.PageImpl.updateInterceptionPatterns(PageImpl.java:1438)
com.microsoft.playwright.impl.PageImpl.handleEvent(PageImpl.java:229)
com.microsoft.playwright.impl.Connection.dispatch(Connection.java:347)
com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:261)
... the same 7 frames again for each queued route event
This is the defect class of #678, where Route.resume and Route.abort stopped waiting for the answer. The pattern update after each route still waits.
The Node.js client does this differently in Page._onRoute: it updates the patterns only when no route remains, and it does not wait for the answer:
if (!this._routes.length)
this._updateInterceptionPatterns({ internal: true }).catch(() => {});A possible fix is the same condition in Java (send the update only when the set of patterns changed, for example when a handler with times expired), or a send that does not wait.
A second effect makes it worse: when PLAYWRIGHT_JAVA_SRC is set, each API call captures a stack trace, and the cost of that grows with the nesting depth. Then the client falls further behind and the nesting grows faster.
Additional context
Found through Blockether/spel#135, where a daemon with one catch-all route failed on pages with many requests. A workaround for a client is a route pattern that matches fewer requests, or a thread with a larger stack.
Environment
- Operating System: macOS 27.2.1 (arm64)
- JDK: OpenJDK 25.0.3
- Browser: Chromium (bundled with 1.65.1), headless
Activity
microsoft-playwright-automation commentedon Nov 24, 2026
microsoft-playwright-automation commentedon Nov 24, 2026
Hi, I'm the Playwright bot. I reproduced this on current
main(42838330048c561c90edbd3b5619b782c6b8a0f9) on macOS with Chromium, Firefox and WebKit.The stack overflow is still present. Current
mainalready avoids pattern updates while handlers remain, but route actions themselves now wait for driver replies and dispatch the next route recursively. The proposed fix defers route events during those waits, then processes them in order, without discarding driver errors or changing close handling.Condensed repro
Using
page.context().routeinstead gives the same failure. The initial navigation happens before routing, so this repro expects 3000 callbacks rather than 3001.What I ran
macOS, Temurin JDK 21.0.13, Java main
42838330048c561c90edbd3b5619b782c6b8a0f9, driver61775c1ae65409859ebc4e60dec3a4a3a9cc3f92(1.66.0-next), headless:allSettledThe permanent regression extends
TestPageRoute.shouldWorkWithEqualRequestswith one simultaneous 3000-request burst, reusing its resume/abort handler.mvn test -f playwright/pom.xml -Dtest=TestPageRoute*,TestBrowserContextRoute,TestPageRequestIntercept,TestPageInterception --no-transfer-progresspassed: 404 invocations, no failures or errors, 16 existing skips.An exploratory stricter
Promise.allcheck failed once in Chromium withFailed to fetch, not a stack overflow. The reportedPromise.allSettledcheck passed with all 3000 callbacks. I tested current main, not the published 1.64.0 or 1.65.1 jars.Draft PR: #2072
Triaged on macOS by the Playwright bot - agent run