From 6b3ca0c6175057df6f0faba817af628c5e6cf6a7 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 24 Nov 2026 13:58:08 -0800 Subject: [PATCH 1/3] fix(route): do not wait for the reply of route.resume() Fixes: https://github.com/microsoft/playwright-java/issues/2068 --- .../com/microsoft/playwright/impl/RouteImpl.java | 10 +++++++++- .../com/microsoft/playwright/TestPageRoute.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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..18f082d42 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -258,7 +258,15 @@ void innerContinue(boolean isFallback) { params.headers = options.headers != null ? headersObjectToArray(options.headers) : null; params.postData = options.postDataBuffer; params.isFallback = isFallback; - raceWithTargetClose(sendMessageAsync("continue", params)); + WaitableResult promise = sendMessageAsync("continue", params); + // java: waiting for the reply pumps the connection, which dispatches the next route event on this stack, and + // a burst of requests overflows it. The driver rejects a continue only for its overrides, and the callers of + // a fallback ignore the error, so the reply is awaited only for a continue() with overrides. + boolean hasOverrides = params.url != null || params.method != null || params.headers != null || params.postData != null; + if (isFallback || !hasOverrides) { + return; + } + raceWithTargetClose(promise); } // java: members with no counterpart upstream; handlerReturned is set by RouteHandler when the handler diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index 30484ffd0..01101cd50 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -424,6 +424,21 @@ void shouldWorkWithRedirectsForSubresources(Page page, Server server, BrowserNam assertNull(r); } + @PlaywrightTest + void shouldResumeABurstOfRequests(Page page, Server server) { + // https://github.com/microsoft/playwright-java/issues/2068 + page.navigate(server.EMPTY_PAGE); + int[] handled = {0}; + page.route("**/*", route -> { + ++handled[0]; + route.resume(); + }); + assertEquals(3000, page.evaluate("() => Promise.allSettled(" + + "Array.from({length: 3000}, (_, i) => fetch('/empty.html?i=' + i)))" + + ".then(results => results.length)")); + assertEquals(3000, handled[0]); + } + @PlaywrightTest void shouldWorkWithEqualRequests(Page page, Server server) { page.navigate(server.EMPTY_PAGE); From 5a2e449adba146a84f3c6d026f4b549f376331f9 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 24 Nov 2026 14:10:33 -0800 Subject: [PATCH 2/3] fix(route): do not wait for the reply of abort() and fulfill() Fixes: https://github.com/microsoft/playwright-java/issues/2068 --- .../playwright/impl/RequestImpl.java | 5 +++ .../microsoft/playwright/impl/RouteImpl.java | 40 +++++++++++++------ .../microsoft/playwright/TestPageRoute.java | 18 +++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java index 9e8e4532b..5e23ba3c8 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java @@ -306,6 +306,11 @@ void applyFallbackOverrides(FallbackOverrides overrides) { } } + // java: the URL without the fallback overrides. + String originalUrl() { + return requestInitializer.url; + } + SerializedFallbackOverrides fallbackOverridesForContinue() { return fallbackOverrides; } 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 18f082d42..375c31a71 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -30,6 +30,8 @@ import com.microsoft.playwright.impl.protocol.RouteRedirectNavigationRequestParams; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -60,12 +62,10 @@ public RequestImpl request() { return routeInitializer.request; } - // java: takes the pending reply of a message sent with sendMessageAsync. + // java: takes the pending reply of a message sent with sendMessageAsync and does not wait for it. Waiting + // pumps the connection, which dispatches the next route event on this stack, and a burst of requests + // overflows it. An error in the reply is lost; innerContinue() runs the check of the driver itself. 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); } WaitableResult startHandling() { @@ -225,6 +225,11 @@ private void innerFulfill(FulfillOptions options, JsonElement json) { params.body = (String) body; params.isBase64 = isBase64; params.fetchResponseUid = fetchResponseUid; + // java: the check of fulfill() in server/webkit/wkInterceptableRequest.ts, its error would be lost with the reply. + if (context != null && context.browser() != null && "webkit".equals(context.browser().browserName) + && 300 <= params.status && params.status < 400) { + throw new PlaywrightException("Cannot fulfill with redirect status: " + params.status); + } raceWithTargetClose(sendMessageAsync("fulfill", params)); } @@ -258,15 +263,13 @@ void innerContinue(boolean isFallback) { params.headers = options.headers != null ? headersObjectToArray(options.headers) : null; params.postData = options.postDataBuffer; params.isFallback = isFallback; - WaitableResult promise = sendMessageAsync("continue", params); - // java: waiting for the reply pumps the connection, which dispatches the next route event on this stack, and - // a burst of requests overflows it. The driver rejects a continue only for its overrides, and the callers of - // a fallback ignore the error, so the reply is awaited only for a continue() with overrides. - boolean hasOverrides = params.url != null || params.method != null || params.headers != null || params.postData != null; - if (isFallback || !hasOverrides) { - return; + // java: the check of Route.continue() in server/network.ts, its error would be lost with the reply. + if (options.url != null && !options.url.isEmpty()) { + if (!protocol(request().originalUrl()).equals(protocol(options.url))) { + throw new PlaywrightException("New URL must have same protocol as overridden URL"); + } } - raceWithTargetClose(promise); + raceWithTargetClose(sendMessageAsync("continue", params)); } // java: members with no counterpart upstream; handlerReturned is set by RouteHandler when the handler @@ -282,6 +285,17 @@ private static RequestImpl.FallbackOverrides fallbackOverrides(String url, Strin return overrides; } + private static String protocol(String url) { + try { + String scheme = new URI(url).getScheme(); + if (scheme != null) { + return scheme.toLowerCase(); + } + } catch (URISyntaxException e) { + } + throw new PlaywrightException("Invalid URL"); + } + private static byte[] readFile(Path path) { try { return Files.readAllBytes(path); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index 01101cd50..e7d88dca6 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -439,6 +439,24 @@ void shouldResumeABurstOfRequests(Page page, Server server) { assertEquals(3000, handled[0]); } + @PlaywrightTest + void shouldAbortAndFulfillABurstOfRequests(Page page, Server server) { + // https://github.com/microsoft/playwright-java/issues/2068 + page.navigate(server.EMPTY_PAGE); + int[] handled = {0}; + page.route("**/*", route -> { + if (++handled[0] % 2 == 0) { + route.abort(); + } else { + route.fulfill(new Route.FulfillOptions().setBody("ok")); + } + }); + assertEquals(1500, page.evaluate("() => Promise.allSettled(" + + "Array.from({length: 3000}, (_, i) => fetch('/empty.html?i=' + i)))" + + ".then(results => results.filter(r => r.status === 'fulfilled').length)")); + assertEquals(3000, handled[0]); + } + @PlaywrightTest void shouldWorkWithEqualRequests(Page page, Server server) { page.navigate(server.EMPTY_PAGE); From ab675aa60db80d62525d2ec544b74904b25157ff Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 24 Nov 2026 14:24:08 -0800 Subject: [PATCH 3/3] chore(route): remove raceWithTargetClose --- .../microsoft/playwright/impl/RequestImpl.java | 1 - .../microsoft/playwright/impl/RouteImpl.java | 18 +++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java index 5e23ba3c8..724495812 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java @@ -306,7 +306,6 @@ void applyFallbackOverrides(FallbackOverrides overrides) { } } - // java: the URL without the fallback overrides. String originalUrl() { return requestInitializer.url; } 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 375c31a71..480503aa2 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -62,11 +62,7 @@ public RequestImpl request() { return routeInitializer.request; } - // java: takes the pending reply of a message sent with sendMessageAsync and does not wait for it. Waiting - // pumps the connection, which dispatches the next route event on this stack, and a burst of requests - // overflows it. An error in the reply is lost; innerContinue() runs the check of the driver itself. - private void raceWithTargetClose(WaitableResult promise) { - } + // java: _raceWithTargetClose is not ported, route actions do not wait for the reply. WaitableResult startHandling() { handlingPromise = new WaitableResult<>(); @@ -96,7 +92,7 @@ public void abort(String errorCode) { handleRoute(() -> { RouteAbortParams params = new RouteAbortParams(); params.errorCode = errorCode; - raceWithTargetClose(sendMessageAsync("abort", params)); + sendMessageAsync("abort", params); }); } @@ -104,7 +100,7 @@ void redirectNavigationRequest(String url) { handleRoute(() -> { RouteRedirectNavigationRequestParams params = new RouteRedirectNavigationRequestParams(); params.url = url; - raceWithTargetClose(sendMessageAsync("redirectNavigationRequest", params)); + sendMessageAsync("redirectNavigationRequest", params); }); } @@ -225,12 +221,12 @@ private void innerFulfill(FulfillOptions options, JsonElement json) { params.body = (String) body; params.isBase64 = isBase64; params.fetchResponseUid = fetchResponseUid; - // java: the check of fulfill() in server/webkit/wkInterceptableRequest.ts, its error would be lost with the reply. + // java: the check of wkInterceptableRequest.ts. if (context != null && context.browser() != null && "webkit".equals(context.browser().browserName) && 300 <= params.status && params.status < 400) { throw new PlaywrightException("Cannot fulfill with redirect status: " + params.status); } - raceWithTargetClose(sendMessageAsync("fulfill", params)); + sendMessageAsync("fulfill", params); } // java: resume() is continue() of the Node.js API. @@ -263,13 +259,13 @@ void innerContinue(boolean isFallback) { params.headers = options.headers != null ? headersObjectToArray(options.headers) : null; params.postData = options.postDataBuffer; params.isFallback = isFallback; - // java: the check of Route.continue() in server/network.ts, its error would be lost with the reply. + // java: the check of server/network.ts. if (options.url != null && !options.url.isEmpty()) { if (!protocol(request().originalUrl()).equals(protocol(options.url))) { throw new PlaywrightException("New URL must have same protocol as overridden URL"); } } - raceWithTargetClose(sendMessageAsync("continue", params)); + sendMessageAsync("continue", params); } // java: members with no counterpart upstream; handlerReturned is set by RouteHandler when the handler