Skip to content

fix: update executor and driver server URL state on directConnect - #2500

Open
hoanle0126 wants to merge 10 commits into
appium:masterfrom
hoanle0126:fix/direct-connect-server-url-state
Open

hoanle0126 wants to merge 10 commits into
appium:masterfrom
hoanle0126:fix/direct-connect-server-url-state

Conversation

@hoanle0126

@hoanle0126 hoanle0126 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Change list

  • Keep the active client config, HTTP client, and remote server URL snapshot behind a volatile field in AppiumCommandExecutor, populate the base URL from AppiumDriverLocalService when omitted, wrap URI conversion failures from overrideServerUrl into SessionNotCreatedException inside setDirectConnect, and guard swaps with a private lock outside DNS validation and old client cleanup.
  • Delegate AppiumDriver.getRemoteAddress to the command executor so driver callers see the updated server URL after directConnect or subsequent overrides, while SeleniumBridge reads the updated base URI from the executor config.
  • Extract LogBroadcastEndpoints under io.appium.java_client.remote so logcat and syslog listeners share WebSocket setup while keeping 0-arg startLogcatBroadcast() delegating to startLogcatBroadcast(host, port), leaving startSyslogBroadcast() on its documented localhost:4723 default, and choosing wss or ws from the connected server when targeting its host and port or from package-private getInitialServerUrl() otherwise.
  • Add Strings.formatHost for IPv6 brackets, accept uppercase https in setDirectConnect, and update DirectConnect.getUrl() to prepend a leading slash to relative paths, parse ASCII digits for ports in 0-65535, verify host, port, and userinfo on the parsed URL, and throw MalformedURLException on invalid values.

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

After the command executor became standalone in v11, overriding the server URL created a new HTTP client without updating the stored client config or cached remote address, so getRemoteAddress() and SeleniumBridge stayed on the initial hub URL.

Added unit tests covering direct connect URL and config updates, Selenium bridge propagation, local service fallback, nullable service compatibility, 0-arg logcat delegation to subclass overrides, HTTP-hub-to-HTTPS-node scheme selection across logcat and syslog overloads, host and port validation in broadcast URIs, IPv6, underscore hostnames, paths with spaces throwing SessionNotCreatedException, and malformed or blank direct-connect fields.

@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] ListensToSyslogMessages.java:69 and ListensToLogcatMessages.java:68 aren't in the diff, but this change affects them. They choose ws or wss from getAddressOfRemoteServer(), which now switches to https after directConnect. The host and port arguments still come from the caller, usually the original hub. startSyslogBroadcast(hubHost, 4723) now opens wss://hubHost:4723/... against a plain-WS port, where before this PR it used ws. Should these helpers also take the host and port from the updated URL, or should the scheme stay tied to the caller's endpoint?

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Updated ListensToLogcatMessages and ListensToSyslogMessages so the no-arg startBroadcast() methods resolve host and port from getAddressOfRemoteServer() (falling back to 127.0.0.1:4723 / localhost:4723), while startBroadcast(host, port) only uses wss when (host, port) matches an https serverUrl (keeping explicit hub host/port calls on plain ws). Added unit tests covering both Android and iOS broadcast URIs.

@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] The no-arg startLogcatBroadcast() and startSyslogBroadcast() now use the server's host and port instead of 127.0.0.1:4723 / localhost:4723. The one-arg overloads (startLogcatBroadcast(String host) and the syslog equivalent) still hard-code 4723. Against http://hub:4444, startLogcatBroadcast() connects to hub:4444, but startLogcatBroadcast("hub") connects to hub:4723. Should the one-arg overloads also default to the server port? The PR is marked non-breaking bugfix, but this is a behavior change for callers of the no-arg methods. If it's intended, please mention it in the description.
  • [minor] SessionHelpers is about session-address parsing. It now also holds log-broadcast URI logic: resolveBroadcastHost, resolveBroadcastPort, buildBroadcastUri and matchesEndpoint. The no-arg start logic is also copy-pasted between ListensToLogcatMessages.java:47 and ListensToSyslogMessages, down to the cast chain. Consider a small dedicated helper in io.appium.java_client.internal (for example a LogBroadcastEndpoints class) that resolves host and port from the executor and builds the URI. A fix like the wildcard-host one would then land in one place.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Addressed all review feedback in commit 8ee115c:

  • Extracted io.appium.java_client.internal.LogBroadcastEndpoints shared by ListensToLogcatMessages and ListensToSyslogMessages (removing duplicated cast/URI code from both interfaces and restoring SessionHelpers to session-address parsing only).
  • Updated 1-arg startLogcatBroadcast(host) / startSyslogBroadcast(host) to default to the active server port instead of hardcoded 4723, and documented the 0-arg / 1-arg server host & port defaulting behavior in the PR description.
  • Preserved wss for any https server unless targeting a replaced plain-http endpoint, mapped wildcard bind addresses (0.0.0.0 / [::]) to loopback, shared DirectConnect.formatHost, and restored loud SessionNotCreatedException failure on blank direct-connect fields.
  • Verified with ./gradlew test checkstyleMain checkstyleTest javadoc :selenium-bridge:check. Ready for re-review @mykola-mokhnach.

@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.

  • [critical] Undeclared breaking change (also anchored inline, comment 1). The PR is marked "Bugfix (non-breaking)" with no ! in the title and no BREAKING CHANGE: entry. The default host and port of the log-broadcast overloads change (details in the inline comment).

@hoanle0126

Copy link
Copy Markdown
Contributor Author

Thanks @mykola-mokhnach! Updated PR #2500 to address all four review points:

  1. Backwards compatibility (non-breaking): Restored the original 127.0.0.1:4723 / localhost:4723 defaults and host/port contract on the 0-arg and 1-arg startLogcatBroadcast and startSyslogBroadcast overloads so existing callers keep the exact previous behavior and there is no breaking change.
  2. Simplified scheme derivation (LogBroadcastEndpoints): Removed chooseScheme, matchesEndpoint, and replaced-URL matching heuristics. buildBroadcastUri now derives ws / wss solely from the active server URL (https gives wss).
  3. Removed executor URL history API (AppiumCommandExecutor): Removed replacedHttpServerUrls from ClientState and dropped getReplacedHttpServerUrls() so no log-broadcast state or extra public API leaks out of the command executor.
  4. Moved formatHost (Strings): Moved formatHost from DirectConnect to io.appium.java_client.internal.Strings and called it from both DirectConnect and LogBroadcastEndpoints.

Updated the unit tests in LogBroadcastEndpointsTest, DirectConnectTest, and AppiumCommandExecutorTest as well as the PR description, and re-verified ./gradlew test checkstyleMain checkstyleTest javadoc :selenium-bridge:check. Ready for re-review!

@Nullable SessionId sessionId,
String deviceLogType
) {
String scheme = serverUrl != null && "https".equalsIgnoreCase(serverUrl.getProtocol()) ? "wss" : "ws";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Restoring the host and port defaults still leaves a compatibility issue here. Suppose the session starts at http://hub:4723, direct-connects to an HTTPS node, and the caller uses startLogcatBroadcast("hub", 4723) to reach the original plain-WS endpoint. Before this PR that call opened ws://hub:4723/.... This line now selects wss://hub:4723/..., so the TLS handshake fails. The same applies to syslog. Could the existing overloads retain the original scheme and have a regression test for this case? If the change is intentional, please document the compatibility impact and how callers can still select ws.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! Updated LogBroadcastEndpoints to pick the WebSocket scheme from the executor's initial server URL (before any directConnect or URL override) via an internal bridge so AppiumCommandExecutor does not add new public API.

Sessions starting on plain HTTP now stay on ws for both logcat and syslog broadcasts after direct-connecting to HTTPS, while initial HTTPS sessions keep using wss. Added regression tests covering both cases plus null initial base URL in AppiumCommandExecutorTest.

@mykola-mokhnach

Copy link
Copy Markdown
Contributor

@hoanle0126 could you please resolve merge conflicts?

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@mykola-mokhnach Merged master and resolved the conflict in ListensToLogcatMessages while preserving the deprecation annotations and the connected-server 0-arg logcat behavior (#2484). Explicit-host logcat and syslog overloads continue using the initial server URL scheme before any overrides, and all unit tests, checkstyle, javadoc, and selenium-bridge checks pass locally.

@mykola-mokhnach

Copy link
Copy Markdown
Contributor

@hoanle0126 Are you sure we still would like to invest into the deprecated logging APIs?
These are anyway going to be removed after the next major bump

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@mykola-mokhnach That part was added for @KazuCocoa's P2 review so explicit logcat and syslog calls keep their initial ws/wss scheme after directConnect until those APIs are removed in the next major bump. Since it's already implemented and tested, keeping it avoids an 11.x regression unless you prefer dropping it.

@mykola-mokhnach

Copy link
Copy Markdown
Contributor

@mykola-mokhnach That part was added for @KazuCocoa's P2 review so explicit logcat and syslog calls keep their initial ws/wss scheme after directConnect until those APIs are removed in the next major bump. Since it's already implemented and tested, keeping it avoids an 11.x regression unless you prefer dropping it.

This makes sense, although it would be still necessary to check which parts are only used by deprecated entities and mark them as deprecated as well, so we don't have unexpected leftovers after the future cleanup.

@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] The PR title and description are about syncing executor and driver server URL state after directConnect. The PR also adds the Strings.formatHost IPv6 bracketing, new DirectConnect.getUrl() normalization and validation, and the LogBroadcastEndpoints refactor of the logcat and syslog listeners. The first of these rides along with the others in a fix: PR, so it can't be reverted separately. Consider splitting the logcat/syslog refactor and the DirectConnect hardening into their own PRs.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@mykola-mokhnach Done in b6e90f5. Marked LogBroadcastEndpoints, CommandExecutorInternals, and the internal initial-URL tracking hooks in AppiumCommandExecutor as @Deprecated(since = "11.1.0", forRemoval = true) with matching deprecation Javadoc. Left Strings.formatHost non-deprecated since DirectConnect shares it.

@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.

  • No general findings. Two inline points need attention: a DirectConnect.getUrl() regression and an API-surface question.

@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] ListensToSyslogMessages.java:55 isn't in the diff. The PR description says the 0-arg and explicit-host overloads consistently follow getAddressOfRemoteServer(). startSyslogBroadcast() still delegates to startSyslogBroadcast("localhost"), so its host and port stay localhost:4723 while the scheme now follows the connected server. After a direct connect to https://203.0.113.1:4443, the 0-arg syslog call goes to wss://localhost:4723/..., while the 0-arg logcat call follows the server. Is the localhost default intended for syslog (the Javadoc documents it)? If so, please reword the description. If not, mirror the logcat change.

ClientState previousState;
synchronized (clientStateLock) {
previousState = this.clientState;
AppiumClientConfig updatedConfig = previousState.config.baseUrl(serverUrl);

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] DirectConnect.getUrl() now accepts paths with spaces (/wd/my hub, covered by DirectConnectTest). AppiumClientConfig.baseUrl(URL) calls url.toURI(), which fails on a space and rethrows as a bare RuntimeException. With a server returning directConnectPath: "/wd/my hub", getUrl() succeeds and overrideServerUrl throws a RuntimeException instead of SessionNotCreatedException. The session already exists on the server and is leaked. Either make getUrl() reject (or percent-encode) such paths, or catch the failure in setDirectConnect and rethrow it as SessionNotCreatedException. Please add an end-to-end test for this case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ae9ec84: wrapped URISyntaxException failures from overrideServerUrl() in setDirectConnect() into SessionNotCreatedException so paths with spaces like /wd/my hub never leak a bare RuntimeException, and added an end-to-end test in AppiumCommandExecutorTest alongside an underscore-host baseUrl check.

}

private static void startBroadcast(
AppiumRemoteWebDriver remoteWebDriver,

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.

[nit] The private startBroadcast takes both remoteWebDriver and driver, which are the same object cast two ways. Both public overloads also repeat the driver and executor casts. Pass one driver and resolve the executor and URL in a single private helper, so the two arguments can't diverge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ae9ec84: removed the private helper taking both driver types and resolved the remote driver, executor, and URI inside startBroadcast() with a single cast.

String deviceLogType
) {
String protocol = serverUrl == null ? null : serverUrl.getProtocol();
String scheme = "https".equalsIgnoreCase(protocol) || "wss".equalsIgnoreCase(protocol) ? "wss" : "ws";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Deriving the scheme from the active server reintroduces the explicit-host regression discussed earlier. If a session starts at http://hub:4723 and direct-connects to an HTTPS node, startLogcatBroadcast("hub", 4723) now attempts a TLS handshake with the original plain-WebSocket listener. The same applies to syslog. Could callers retain a way to reach that listener over ws, with a regression test for the HTTP-hub-to-HTTPS-node case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ae9ec84: moved LogBroadcastEndpoints to the remote package so it uses package-private getInitialServerUrl() without adding a public method to AppiumCommandExecutor. Explicit-host calls now use the connected scheme only when targeting its host and port, and otherwise keep the initial server scheme, covered for both logcat and syslog in AppiumCommandExecutorTest.

default void startLogcatBroadcast() {
URL serverUrl = getRemoteServerUrl();
startLogcatBroadcast(serverUrl.getHost(), serverUrl.getPort());
LogBroadcastEndpoints.startBroadcast(this, this::getLogcatClient, "logcat");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] The zero-argument overload previously resolved the server address and called startLogcatBroadcast(host, port). A driver subclass can override that method to route the socket through a tunnel. Calling the static helper here bypasses the override and opens the default connection instead. Could this overload delegate to the two-argument method again, with a test that overrides it and records the host and port?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ae9ec84: restored startLogcatBroadcast() delegation to startLogcatBroadcast(host, port) using LogBroadcastEndpoints.requireServerUrl(), and added a subclass override test in AppiumCommandExecutorTest.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@mykola-mokhnach Thanks, the localhost:4723 default in startSyslogBroadcast() is intentional to match its Javadoc. I moved LogBroadcastEndpoints to the remote package and updated the PR description to state that clearly along with the 0-arg logcat delegation and initial-scheme fallback for explicit hosts.

This branch has not been deployed

No deployments
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