From d7fe64e588305f8583152826a94f0a3d8608056c Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:10:52 +0000 Subject: [PATCH] fix(route): avoid recursive dispatch during route action waits Fixes: https://github.com/microsoft/playwright-java/issues/2068 --- .../playwright/impl/ChannelOwner.java | 7 ++++++- .../microsoft/playwright/impl/Connection.java | 18 +++++++++++++++++- .../playwright/impl/LongStandingScope.java | 11 ++++++++++- .../microsoft/playwright/impl/RouteImpl.java | 3 ++- .../microsoft/playwright/TestPageRoute.java | 6 ++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java b/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java index a5c944ed9..c7e647dc8 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java @@ -362,11 +362,16 @@ public void sendMessage(String method, Object params, Double timeout) { // java: pumps the connection until the waitable is done; upstream awaits promises. T runUntil(Runnable code, Waitable waitable) { + return runUntil(code, waitable, true); + } + + // java: a route action waits for its reply without entering another route handler. + T runUntil(Runnable code, Waitable waitable, boolean dispatchRoutes) { try { code.run(); while (!waitable.isDone()) { try { - connection.processOneMessage(); + connection.processOneMessage(dispatchRoutes); } catch (PlaywrightException e) { // java: a closed pipe throws from the pump right after it dispatched the close events; the outcome // of the waitable wins, as the promise settled by those events does upstream. diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java index 8783b790d..023ffa284 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java @@ -33,9 +33,11 @@ import java.io.IOException; import java.time.Duration; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; +import java.util.Deque; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -116,6 +118,7 @@ public class Connection extends EventEmitter { // java: everything below has no counterpart in connection.ts. private final Transport transport; + private final Deque deferredRouteEvents = new ArrayDeque<>(); private final StackTraceCollector stackTraceCollector; private final Gson protocolGson = gson().newBuilder() .registerTypeAdapterFactory(new ChannelOwnerAdapterFactory()) @@ -557,13 +560,26 @@ JsonElement sendMessage(ChannelOwner object, String method, JsonObject params, C // java: upstream receives messages through onmessage(); here the pump polls the transport. boolean processOneMessage() { + return processOneMessage(true); + } + + // java: route-action waits defer route events so queued requests do not nest on the Java stack. + boolean processOneMessage(boolean dispatchRoutes) { + if (dispatchRoutes && !deferredRouteEvents.isEmpty()) { + dispatch(deferredRouteEvents.removeFirst()); + return true; + } JsonObject message = transport.poll(Duration.ofMillis(10)); if (message == null) { return false; } Gson gson = gson(); Message messageObj = gson.fromJson(message, Message.class); - dispatch(messageObj); + if (!dispatchRoutes && messageObj.id == null && "route".equals(messageObj.method)) { + deferredRouteEvents.addLast(messageObj); + } else { + dispatch(messageObj); + } return true; } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LongStandingScope.java b/playwright/src/main/java/com/microsoft/playwright/impl/LongStandingScope.java index e5055b024..80fd68fd0 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LongStandingScope.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LongStandingScope.java @@ -90,6 +90,11 @@ T safeRace(Waitable promise, T defaultValue) { return await(race(Collections.singletonList(promise), true, defaultValue)); } + // java: route actions keep synchronous replies and close races, but must not recursively dispatch routes. + T safeRaceWithoutRouteEvents(Waitable promise) { + return await(race(Collections.singletonList(promise), true, null), false); + } + private Waitable race(List> promises, boolean safe, T defaultValue) { WaitableResult terminatePromise = new WaitableResult<>(); StackTraceElement[] frames = new Exception().getStackTrace(); @@ -135,8 +140,12 @@ private static PlaywrightException cloneError(PlaywrightException error, StackTr // java: await of a race; the finally block of _race disposes it. private T await(Waitable race) { + return await(race, true); + } + + private T await(Waitable race, boolean dispatchRoutes) { try { - return owner.runUntil(() -> {}, race); + return owner.runUntil(() -> {}, race, dispatchRoutes); } finally { race.dispose(); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java index 1558ef5bc..a009a2773 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -65,7 +65,8 @@ private void raceWithTargetClose(WaitableResult promise) { // When page closes or crashes, we catch any potential rejects from this Route. // Note that page could be missing when routing popup's initial request that // does not have a Page initialized just yet. - request().targetClosedScope().safeRace(promise); + // java: waiting for an acknowledgment must not dispatch the next route on this stack. + request().targetClosedScope().safeRaceWithoutRouteEvents(promise); } WaitableResult startHandling() { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index 30484ffd0..9cbcb5ddb 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -426,6 +426,7 @@ void shouldWorkWithRedirectsForSubresources(Page page, Server server, BrowserNam @PlaywrightTest void shouldWorkWithEqualRequests(Page page, Server server) { + // https://github.com/microsoft/playwright-java/issues/2068 page.navigate(server.EMPTY_PAGE); AtomicInteger responseCount = new AtomicInteger(1); server.setRoute("/zzz", exchange -> { @@ -436,8 +437,10 @@ void shouldWorkWithEqualRequests(Page page, Server server) { }); boolean[] spinner = {false}; + int[] intercepted = {0}; // Cancel 2nd request. page.route("**/*", route -> { + ++intercepted[0]; if (spinner[0]) { route.abort(); } else { @@ -450,6 +453,9 @@ void shouldWorkWithEqualRequests(Page page, Server server) { results.add(page.evaluate("() => fetch('/zzz').then(response => response.text()).catch(e => 'FAILED')")); } assertEquals(asList("11", "FAILED", "22"), results); + assertEquals(3000, page.evaluate("() => Promise.allSettled(Array.from({length: 3000}, " + + "() => fetch('/zzz').then(response => response.text()))).then(results => results.length)")); + assertEquals(3003, intercepted[0]); } @PlaywrightTest