Skip to content

fix: respect wsTimeout when opening WebSocket in JdkHttpClient - #2507

Merged
valfirst merged 3 commits into
appium:masterfrom
Abdelrhman-Ellithy:fix/jdk-http-client-ws-timeout
Nov 24, 2026
Merged

valfirst merged 3 commits into
appium:masterfrom
Abdelrhman-Ellithy:fix/jdk-http-client-ws-timeout

Conversation

@Abdelrhman-Ellithy

@Abdelrhman-Ellithy Abdelrhman-Ellithy commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Change list

  • Store wsTimeout in JdkHttpClient from the provided ClientConfig.
  • Use wsTimeout as the single deterministic WebSocket handshake timeout via socketFuture.get(...), falling back to readTimeout when wsTimeout is zero or negative.
  • Remove the separate WebSocket.Builder#connectTimeout call to avoid a dual-timer race between the JDK internal timer and socketFuture.get(...).
  • Align Javadocs on ClientConfig#wsTimeout() and AppiumClientConfig#wsTimeout(Duration) to consistently document the handshake timeout semantics and fallback behavior.
  • Add unit tests in JdkWebSocketTest verifying handshake timeout, zero-timeout fallback, and negative-timeout fallback.

Types of changes

  • No changes in production code.
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Details

JdkHttpClient previously ignored ClientConfig#wsTimeout() when opening a WebSocket connection via openSocket(...).

Prior to this change:

  1. java.net.http.WebSocket.Builder#connectTimeout(Duration) was hardcoded to connectTimeout (the HTTP connect timeout, default 10s).
  2. The asynchronous handshake future socketFuture.get(...) used readTimeout (the HTTP read timeout, default 10 minutes).
  3. Any custom or default clientConfig.wsTimeout(...) had zero effect on WebSocket connection establishment, despite ClientConfig#wsTimeout() documenting it as "the maximum time to wait for a WebSocket connection to be established."

This change wires wsTimeout as the single deterministic handshake timer:

  • When wsTimeout is positive (wsTimeout.compareTo(Duration.ZERO) > 0), it is used directly as the socketFuture.get(...) deadline.
  • When wsTimeout is zero or negative, the handshake falls back to readTimeout (preserving the pre-PR bounded behavior, default 10 minutes).
  • WebSocket.Builder#connectTimeout(...) is no longer set separately, eliminating the dual-timer race that could produce inconsistent error messages (ExecutionException wrapping HttpTimeoutException vs. TimeoutException). The underlying HttpClient still retains its own connectTimeout for TCP connection establishment.

Note: This intentionally changes the default WebSocket handshake bound from readTimeout (10 min) to wsTimeout (30 s, via AppiumClientConfig.DEFAULT_WS_TIMEOUT), matching the ClientConfig#wsTimeout() interface contract.
If your environment (e.g., slow proxies or remote device farms) relies on the previous 10-minute default, set wsTimeout explicitly on AppiumClientConfig.

Verification

  • timesOutWhenWebSocketHandshakeDoesNotComplete: Confirms a stalled handshake aborts at wsTimeout (200ms) rather than readTimeout (5m) or connectTimeout (10s), and asserts the deterministic "initial request timeout" error message.
  • fallsBackToReadTimeoutWhenWsTimeoutIsZero: Confirms Duration.ZERO falls back to readTimeout (200ms) instead of waiting indefinitely.
  • fallsBackToReadTimeoutWhenWsTimeoutIsNegative: Confirms negative wsTimeout falls back to readTimeout (200ms) instead of failing immediately.

JdkHttpClient previously ignored ClientConfig#wsTimeout() when opening a WebSocket connection, falling back to connectTimeout on the WebSocket builder and readTimeout (default 10 minutes) on the handshake future.

- Store wsTimeout in JdkHttpClient from the provided ClientConfig.

- Configure WebSocket.Builder#connectTimeout to wsTimeout when positive, avoiding IllegalArgumentException on Duration.ZERO.

- Await the handshake future using wsTimeout (or indefinitely if zero).

- Add unit tests verifying both handshake timeout and zero-timeout connection.
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Abdelrhman-Ellithy / name: Abdelrahman Ellithy (2669f0f)

@mykola-mokhnach mykola-mokhnach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [minor] ClientConfig#wsTimeout is not a WebSocket connect or handshake timeout in Selenium. At SeleniumHQ/selenium master, BiDiProvider and RemoteWebDriver only pass it to new BiDi(connection, wsConfig.wsTimeout()), which is the BiDi command timeout. Selenium's own JdkHttpClient still uses connectionTimeout for the WebSocket builder and readTimeout for the handshake get. The PR repurposes the setting, so the default handshake bound drops from 10 min (readTimeout) to 30 s (webdriver.httpclient.wsTimeout). Is that intended? If so, please say it in the PR description, and consider a Javadoc note that connectionTimeout no longer applies to the WebSocket handshake.

@mykola-mokhnach mykola-mokhnach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [minor] AppiumClientConfig.java:55 (DEFAULT_WS_TIMEOUT): this PR changes the default WebSocket handshake bound from readTimeout (10 min) to 30 s. Users on slow proxies or device farms will see initial request timeout after upgrading. The description mentions it, but the PR is ticked as a "non-breaking bugfix" and has no ! marker or BREAKING CHANGE: entry. Is this meant to be treated as a behavior change for release notes? If so, please mark it and add a short migration hint ("set wsTimeout explicitly to keep the old bound"). Arguably it just aligns with the documented wsTimeout contract, so I'm asking rather than insisting.
  • [nit] ClientConfig.java:56 and AppiumClientConfig.java:237: the public API Javadoc links to io.appium.java_client.internal.http.JdkHttpClient. A public interface shouldn't reference an internal implementation class. Please describe the semantics generically (for example "the WebSocket handshake timeout; zero or negative values fall back to readTimeout()") and drop the link.

@valfirst
valfirst merged commit 48f21ed into appium:master Nov 24, 2026
1 check passed
@Abdelrhman-Ellithy
Abdelrhman-Ellithy deleted the fix/jdk-http-client-ws-timeout branch November 24, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants