grpc/grpc-java · error · UnsupportedOperationException

Unsupported operation getLocalPort()

Error message

Unsupported operation getLocalPort()

What it means

UdsSocket wraps an Android LocalSocket where the concept of an IP local port does not exist, so getLocalPort() deliberately throws UnsupportedOperationException. The class exposes getLocalSocketAddress() returning an empty SocketAddress placeholder instead, so port-based logic must not be applied to UDS sockets.

Source

Thrown at android/src/main/java/io/grpc/android/UdsSocket.java:123

      public void close() throws IOException {
        UdsSocket.this.close();
      }
    };
  }

  @Override
  public boolean getKeepAlive() {
    throw new UnsupportedOperationException("Unsupported operation getKeepAlive()");
  }

  @Override
  public InetAddress getLocalAddress() {
    throw new UnsupportedOperationException("Unsupported operation getLocalAddress()");
  }

  @Override
  public int getLocalPort() {
    throw new UnsupportedOperationException("Unsupported operation getLocalPort()");
  }

  @Override
  public SocketAddress getLocalSocketAddress() {
    return new SocketAddress() {};
  }

  @Override
  public boolean getOOBInline() {
    throw new UnsupportedOperationException("Unsupported operation getOOBInline()");
  }

  @Override
  public OutputStream getOutputStream() throws IOException {
    return new FilterOutputStream(localSocket.getOutputStream()) {
      @Override
      public void close() throws IOException {
        UdsSocket.this.close();

View on GitHub (pinned to 64daddc1f3)

Solutions

  1. Do not derive host:port identifiers for UDS connections; key on the UDS path used in forPath().
  2. Guard port reads with a UdsSocket check or catch UnsupportedOperationException in shared code.
  3. Use TCP transport if port semantics are required by surrounding infrastructure.

Example fix

// before
int port = socket.getLocalPort();
// after
Integer port = socket.getClass().getSimpleName().equals("UdsSocket")
    ? null
    : socket.getLocalPort();
Defensive patterns

Strategy: type-guard

Type guard

static boolean isUdsSocket(Socket s) {
  return s.getClass().getName().equals("io.grpc.android.UdsSocket");
}

Try / catch

try {
  int port = socket.getLocalPort();
} catch (UnsupportedOperationException e) {
  // UDS: no ports; key logs/metrics on the UDS path
}

Prevention

When it happens

Trigger: Calling getLocalPort() on the UdsSocket from UdsSocketFactory — e.g. logging frameworks recording local port, or code building host:port strings for every connection.

Common situations: Connection loggers and monitoring dashboards expecting host:port pairs; port-based routing or firewall-style logic; tests asserting socket endpoints.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/45e2d02149bf4f8a. Report an issue: GitHub.