Skip to content

[Bug]: StackOverflowError when a page makes thousands of requests under a route #2068

Open

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions