From 0f8644fd654889c38b96179e00ba3cfd7feaaed8 Mon Sep 17 00:00:00 2001 From: Alex Osborne Date: Thu, 12 Nov 2026 13:12:41 +0900 Subject: [PATCH] LocalWebDriverBiDi: make the command timeout configurable --- .../net/webdriver/LocalWebDriverBiDi.java | 17 ++++++++++++++--- .../crawler/processor/BrowserProcessorTest.java | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/commons/src/main/java/org/archive/net/webdriver/LocalWebDriverBiDi.java b/commons/src/main/java/org/archive/net/webdriver/LocalWebDriverBiDi.java index 10c73d0e..ded6b59c 100644 --- a/commons/src/main/java/org/archive/net/webdriver/LocalWebDriverBiDi.java +++ b/commons/src/main/java/org/archive/net/webdriver/LocalWebDriverBiDi.java @@ -32,6 +32,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.WebSocket; import java.nio.file.Path; +import java.time.Duration; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicLong; @@ -58,6 +59,7 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable { private final Thread shutdownHook; private final Object sendLock = new Object(); private CompletableFuture sendChain = CompletableFuture.completedFuture(null); + private volatile Duration commandTimeout = Duration.ofSeconds(30); private static final int LAUNCH_TIMEOUT_SECONDS = 60; private static final String[] BROWSERS = new String[]{ "firefox", "/Applications/Firefox.app/Contents/MacOS/firefox", "chromedriver"}; @@ -175,6 +177,14 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable { return future; } + /** + * How long to wait for the reply to a command before throwing {@link WebDriverTimeoutException}. + * Commands that return a {@link CompletableFuture} aren't affected. + */ + public void setCommandTimeout(Duration commandTimeout) { + this.commandTimeout = commandTimeout; + } + @Override public boolean isAlive() { return process.isAlive() && !webSocket.isInputClosed() && !webSocket.isOutputClosed(); @@ -247,9 +257,10 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable { parameterizedType.getActualTypeArguments()[0] instanceof Class typeClass) { yield future.thenApply(result -> BiDiJson.fromJson(result, typeClass)); } else { - int timeout = 30; + Duration timeout = commandTimeout; try { - yield BiDiJson.fromJson(future.get(timeout, TimeUnit.SECONDS), method.getReturnType()); + yield BiDiJson.fromJson(future.get(timeout.toMillis(), TimeUnit.MILLISECONDS), + method.getReturnType()); } catch (ExecutionException e) { if (e.getCause() instanceof RuntimeException re) throw re; throw e; @@ -258,7 +269,7 @@ public class LocalWebDriverBiDi implements WebDriverBiDi, Closeable { logger.log(System.Logger.Level.WARNING, "Browser stopped responding, killing it"); kill(); } - throw new WebDriverTimeoutException(prefix + methodName + " took more than " + timeout + " seconds"); + throw new WebDriverTimeoutException(prefix + methodName + " took more than " + timeout.toSeconds() + " seconds"); } } } diff --git a/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java b/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java index a69b56d0..4ab1cb23 100644 --- a/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java +++ b/engine/src/test/java/org/archive/crawler/processor/BrowserProcessorTest.java @@ -24,6 +24,7 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.util.*; import java.util.zip.GZIPOutputStream; @@ -129,6 +130,7 @@ class BrowserProcessorTest { .findFirst().orElseThrow(); processor.setBehaviors(List.of(page -> { if (browserProcess.isAlive()) { + processor.pool.get(0).driver.setCommandTimeout(Duration.ofSeconds(2)); suspend(browserProcess); page.eval("() => 1"); }