Skip to content

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

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

hoanle0126 wants to merge 3 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

  • Store the active client configuration, HTTP client, and any replaced plain-HTTP server URLs in one immutable holder behind a single volatile field in AppiumCommandExecutor, synchronize overrideServerUrl, and read getAddressOfRemoteServer() directly from that configuration (populating the base URL from AppiumDriverLocalService when omitted).
  • Delegate AppiumDriver.getRemoteAddress directly to the command executor so callers (including SeleniumBridge and log broadcast listeners) see the updated server URL after directConnect or subsequent URL overrides.
  • Extract LogBroadcastEndpoints under io.appium.java_client.internal shared by ListensToLogcatMessages and ListensToSyslogMessages (keeping SessionHelpers focused on session-address parsing), mapping wildcard bind hosts (0.0.0.0 / [::]) to loopback fallback and sharing DirectConnect.formatHost.
  • Preserve wss for any HTTPS server across all overloads (only falling back to ws when targeting a plain-HTTP endpoint replaced by override or directConnect), and default both 0-arg and 1-arg broadcast methods to the active server port.
  • Keep DirectConnect.isValid() checking non-null fields so blank protocol, host, or port values returned by a server still fail loudly in getUrl() with SessionNotCreatedException rather than silently falling back to the hub URL.

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 with the overridden base URL without updating the stored client configuration. That left the executor, driver remote address, and Selenium bridge configuration pointing to the initial hub URL after direct connect.

Note on log broadcast defaults: the 0-arg startLogcatBroadcast() / startSyslogBroadcast() and 1-arg startLogcatBroadcast(host) / startSyslogBroadcast(host) overloads now resolve default host (mapping 0.0.0.0 / [::] to the loopback fallback) and default port from the active server URL rather than hardcoding 127.0.0.1:4723 / localhost:4723. Callers that need another endpoint should pass both host and port explicitly.

Added unit tests covering atomic executor state updates, Selenium bridge client configuration propagation, local service base URL fallback, nullable service constructor compatibility, HTTPS log broadcast scheme preservation vs replaced plain-HTTP hub endpoints, wildcard bind host mapping, 1-arg server port defaulting, IPv6 / path normalization, and loud failure on 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.

@Nullable SessionId sessionId,
String deviceLogType
) {
String scheme = serverUrl != null

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.

[major] wss is now chosen only when the host and port exactly match the https server URL. Before, any https server gave wss. Take an https hub at https://grid.example.com (443) with a separate log host, for example startLogcatBroadcast("10.0.0.5", 443) or an IP-vs-DNS alias. Another case is a TLS proxy on a non-Appium port, called through startLogcatBroadcast(host), which uses port 4723 and never matches. Those calls now produce ws://, and a TLS-only endpoint rejects it. This is a silent regression for existing callers. Is it intended? If so, it needs the ! marker and a BREAKING CHANGE: entry with the migration path. Otherwise, could it keep wss for any https server and only fix the direct-connect 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.

Addressed in 8ee115c: preserved wss for any https server across all overloads (separate log hosts, TLS proxies, IP-vs-DNS aliases), while tracking replaced plain-http server URLs in AppiumCommandExecutor.ClientState so only calls matching a plain-http endpoint overridden by directConnect (and not the active serverUrl) keep ws. Covered in LogBroadcastEndpointsTest and AppiumCommandExecutorTest.

* @param fallbackHost the host to return when {@code serverUrl} has no host
* @return the resolved broadcast host
*/
public static String resolveBroadcastHost(@Nullable URL serverUrl, String fallbackHost) {

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] resolveBroadcastHost returns the raw server host. AppiumDriverLocalService.getUrl() can return http://0.0.0.0:4723 or http://[::]:4723, and the service already normalizes those in fixBroadcastAddresses. The executor now takes its base URL from the service, so the no-arg startLogcatBroadcast() would build ws://0.0.0.0:4723/.... It used to go through 127.0.0.1, and connecting to a wildcard address fails on Windows and some stacks. Could wildcard hosts map to the loopback fallback here?

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.

Addressed in 8ee115c: LogBroadcastEndpoints.resolveHost now maps wildcard bind hosts (0.0.0.0, [::], [0:0:0:0:0:0:0:0]) to the loopback fallback (127.0.0.1 / localhost), with unit tests in LogBroadcastEndpointsTest and AppiumCommandExecutorTest.

* Returns true if the {@link DirectConnect} instance member has valid values.
* @return true if protocol, host and port are non-blank and path is non-null
*/
public boolean isValid() {

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] isValid() now returns false for blank protocol, host or port, and setDirectConnect silently returns on invalid. A response such as directConnectHost: "x" with directConnectPort: "" used to fail with SessionNotCreatedException. Now it is silently ignored and the session keeps using the hub URL, which hides the server misconfiguration. Should blank values still fail loudly, or at least be logged?

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.

Addressed in 8ee115c: restored DirectConnect.isValid() to check non-null fields and moved the non-blank validation for protocol, host, and port into getUrl(), so blank direct-connect values (e.g. directConnectPort: "") fail loudly with SessionNotCreatedException again instead of silently falling back to the hub URL.

return trimmed;
}

private static String formatHost(String host) {

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] This duplicates DirectConnect.formatHost, so the two can drift apart. Could the two share one helper?

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.

Addressed in 8ee115c: exposed DirectConnect.formatHost as public static and reused it in LogBroadcastEndpoints.buildBroadcastUri, removing the duplicate helper.

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

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.

2 participants