From ed33f2301ebb6cb1d17ef694c370c785acf2f988 Mon Sep 17 00:00:00 2001 From: blakeli Date: Mon, 23 Mar 2026 15:20:52 -0400 Subject: [PATCH 1/3] fix: Handle null server address --- .../google/api/gax/rpc/EndpointContext.java | 12 +++++++++--- .../api/gax/rpc/EndpointContextTest.java | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java index 09e105cceb..d21f775808 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java @@ -134,6 +134,7 @@ public static EndpointContext getDefaultInstance() { public abstract String resolvedEndpoint(); + @Nullable public abstract String resolvedServerAddress(); @Nullable @@ -410,7 +411,7 @@ private Integer parseServerPort(String endpoint) { return null; } HostAndPort hostAndPort = parseServerHostAndPort(endpoint); - if (!hostAndPort.hasPort()) { + if (hostAndPort == null || !hostAndPort.hasPort()) { return null; } return hostAndPort.getPort(); @@ -466,8 +467,13 @@ public EndpointContext build() throws IOException { setResolvedUniverseDomain(determineUniverseDomain()); String endpoint = determineEndpoint(); setResolvedEndpoint(endpoint); - setResolvedServerAddress(parseServerAddress(resolvedEndpoint())); - setResolvedServerPort(parseServerPort(resolvedEndpoint())); + try { + setResolvedServerAddress(parseServerAddress(resolvedEndpoint())); + setResolvedServerPort(parseServerPort(resolvedEndpoint())); + } catch (Throwable throwable) { + // Server address and server port are only used for observability. + // We should ignore any errors parsing them and not affect the main client requests. + } setUseS2A(shouldUseS2A()); return autoBuild(); } diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java index 99a8f63fcf..d52a883674 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java @@ -641,7 +641,9 @@ void endpointContextBuild_resolvesServerAddress() throws IOException { @Test void endpointContextBuild_resolvesPort() throws IOException { + String endpoint = "http://localhost:7469"; + EndpointContext endpointContext = defaultEndpointContextBuilder .setClientSettingsEndpoint(endpoint) @@ -649,7 +651,7 @@ void endpointContextBuild_resolvesPort() throws IOException { .build(); Truth.assertThat(endpointContext.resolvedServerPort()).isEqualTo(7469); - endpoint = "localhost:7469"; + endpoint = "localhost:-1"; endpointContext = defaultEndpointContextBuilder .setClientSettingsEndpoint(endpoint) @@ -683,4 +685,19 @@ void endpointContextBuild_resolvesPort() throws IOException { .build(); Truth.assertThat(endpointContext.resolvedServerPort()).isNull(); } + + @Test + void endpointContextBuild_resolvesInvalidEndpointAndPort() throws Exception { + + String endpoint = "localhost:-1"; + + EndpointContext endpointContext = + defaultEndpointContextBuilder + .setClientSettingsEndpoint(endpoint) + .setTransportChannelProviderEndpoint(null) + .build(); + + Truth.assertThat(endpointContext.resolvedServerAddress()).isNull(); + Truth.assertThat(endpointContext.resolvedServerPort()).isNull(); + } } From 1960273f9ee0edb50e66b648a6693f527387baff Mon Sep 17 00:00:00 2001 From: blakeli Date: Mon, 23 Mar 2026 15:28:49 -0400 Subject: [PATCH 2/3] fix: fix tests --- .../test/java/com/google/api/gax/rpc/EndpointContextTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java index d52a883674..d517614ba4 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java @@ -641,9 +641,7 @@ void endpointContextBuild_resolvesServerAddress() throws IOException { @Test void endpointContextBuild_resolvesPort() throws IOException { - String endpoint = "http://localhost:7469"; - EndpointContext endpointContext = defaultEndpointContextBuilder .setClientSettingsEndpoint(endpoint) @@ -651,7 +649,7 @@ void endpointContextBuild_resolvesPort() throws IOException { .build(); Truth.assertThat(endpointContext.resolvedServerPort()).isEqualTo(7469); - endpoint = "localhost:-1"; + endpoint = "localhost:7469"; endpointContext = defaultEndpointContextBuilder .setClientSettingsEndpoint(endpoint) From f7848deb9545dfd4932f6dec0df71ba9e3b52e92 Mon Sep 17 00:00:00 2001 From: blakeli Date: Mon, 23 Mar 2026 15:33:14 -0400 Subject: [PATCH 3/3] fix: Use Exception instead --- .../src/main/java/com/google/api/gax/rpc/EndpointContext.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java index d21f775808..2a775ec4dd 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java @@ -470,7 +470,7 @@ public EndpointContext build() throws IOException { try { setResolvedServerAddress(parseServerAddress(resolvedEndpoint())); setResolvedServerPort(parseServerPort(resolvedEndpoint())); - } catch (Throwable throwable) { + } catch (Exception throwable) { // Server address and server port are only used for observability. // We should ignore any errors parsing them and not affect the main client requests. }