{"record":{"id":"45e2d02149bf4f8a","repo":"grpc/grpc-java","slug":"unsupported-operation-getlocalport","errorCode":null,"errorMessage":"Unsupported operation getLocalPort()","messagePattern":"Unsupported operation getLocalPort\\(\\)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"android/src/main/java/io/grpc/android/UdsSocket.java","lineNumber":123,"sourceCode":"      public void close() throws IOException {\n        UdsSocket.this.close();\n      }\n    };\n  }\n\n  @Override\n  public boolean getKeepAlive() {\n    throw new UnsupportedOperationException(\"Unsupported operation getKeepAlive()\");\n  }\n\n  @Override\n  public InetAddress getLocalAddress() {\n    throw new UnsupportedOperationException(\"Unsupported operation getLocalAddress()\");\n  }\n\n  @Override\n  public int getLocalPort() {\n    throw new UnsupportedOperationException(\"Unsupported operation getLocalPort()\");\n  }\n\n  @Override\n  public SocketAddress getLocalSocketAddress() {\n    return new SocketAddress() {};\n  }\n\n  @Override\n  public boolean getOOBInline() {\n    throw new UnsupportedOperationException(\"Unsupported operation getOOBInline()\");\n  }\n\n  @Override\n  public OutputStream getOutputStream() throws IOException {\n    return new FilterOutputStream(localSocket.getOutputStream()) {\n      @Override\n      public void close() throws IOException {\n        UdsSocket.this.close();","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/android/src/main/java/io/grpc/android/UdsSocket.java#L105-L141","documentation":"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.","triggerScenarios":"Calling getLocalPort() on the UdsSocket from UdsSocketFactory — e.g. logging frameworks recording local port, or code building host:port strings for every connection.","commonSituations":"Connection loggers and monitoring dashboards expecting host:port pairs; port-based routing or firewall-style logic; tests asserting socket endpoints.","solutions":["Do not derive host:port identifiers for UDS connections; key on the UDS path used in forPath().","Guard port reads with a UdsSocket check or catch UnsupportedOperationException in shared code.","Use TCP transport if port semantics are required by surrounding infrastructure."],"exampleFix":"// before\nint port = socket.getLocalPort();\n// after\nInteger port = socket.getClass().getSimpleName().equals(\"UdsSocket\")\n    ? null\n    : socket.getLocalPort();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isUdsSocket(Socket s) {\n  return s.getClass().getName().equals(\"io.grpc.android.UdsSocket\");\n}","tryCatchPattern":"try {\n  int port = socket.getLocalPort();\n} catch (UnsupportedOperationException e) {\n  // UDS: no ports; key logs/metrics on the UDS path\n}","preventionTips":["Avoid host:port formatting for connections whose transport is UDS.","Make local-port fields nullable/optional in telemetry schemas.","Check the socket type before any port-based logic."],"tags":["grpc","android","unix-domain-socket","socket","unsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}