{"record":{"id":"8a6858b84d49a47e","repo":"apache/shenyu","slug":"remoteaddress-is-null","errorCode":null,"errorMessage":"remoteAddress is null","messagePattern":"remoteAddress is null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"shenyu-protocol/shenyu-protocol-tcp/src/main/java/org/apache/shenyu/protocol/tcp/TcpBootstrapServer.java","lineNumber":115,"sourceCode":"        LOG.info(\"Starting proxy client ={}\", serverConn);\n        SocketAddress socketAddress = serverConn.channel().remoteAddress();\n        ActivityConnectionObserver connectionObserver = new ActivityConnectionObserver(\"TcpClient\");\n        eventBus.register(connectionObserver);\n        serverConn.onDispose(() -> eventBus.unregister(connectionObserver));\n        Mono<Connection> client = connectionContext.getTcpClientConnection(getIp(socketAddress), connectionObserver);\n        client.subscribe(\n            clientConn -> bridge.bridge(serverConn, clientConn),\n            error -> {\n                LOG.error(\"Failed to establish client connection for {}\", serverConn, error);\n                eventBus.unregister(connectionObserver);\n                serverConn.dispose();\n            }\n        );\n    }\n\n    private String getIp(final SocketAddress socketAddress) {\n        if (Objects.isNull(socketAddress)) {\n            throw new NullPointerException(\"remoteAddress is null\");\n        }\n        if (socketAddress instanceof InetSocketAddress) {\n            return ((InetSocketAddress) socketAddress).getHostString();\n        }\n        LOG.error(\"Unsupported SocketAddress type: {}\", socketAddress.getClass().getName());\n        throw new IllegalArgumentException(\"Unsupported SocketAddress type: \" + socketAddress.getClass().getName());\n    }\n\n    /**\n     * doOnUpdate.\n     *\n     * @param removeList removeList\n     */\n    @Override\n    public void removeCommonUpstream(final List<DiscoveryUpstreamData> removeList) {\n        eventBus.post(removeList);\n    }\n","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-protocol/shenyu-protocol-tcp/src/main/java/org/apache/shenyu/protocol/tcp/TcpBootstrapServer.java#L97-L133","documentation":"TcpBootstrapServer.getIp extracts the host string from the connection's remote SocketAddress, but first asserts the address is non-null, throwing NullPointerException('remoteAddress is null') otherwise. A null remote address means the gateway never obtained peer information for the TCP connection, so no IP-based processing can continue.","triggerScenarios":"The TCP client/connection handling path invokes getIp with the exchange/connection's remoteAddress (called from the client handler) when that attribute is null — typically a connection that was never fully established or was closed before the address was captured.","commonSituations":"Race conditions where the peer disconnects immediately after connect; custom/derived channel setups that don't populate remoteAddress; connection accepted through an exotic transport that supplies no socket address.","solutions":["Guard the caller: check that the connection/exchange has a non-null remoteAddress before invoking the TCP protocol handling, and drop such connections early.","Log the channel/connection id at error time to find the client or component producing address-less connections.","Verify the TCP listener is set up with the standard Reactor Netty transport so remoteAddress is populated on connect.","Add a connection-level handler that closes sockets without remote address information immediately."],"exampleFix":"// before\nString ip = getIp(exchange.getRequest().remoteAddress());\n// after\nSocketAddress addr = exchange.getRequest().remoteAddress();\nif (addr == null) { return; }\nString ip = getIp(addr);","handlingStrategy":"type-guard","validationCode":"SocketAddress addr = connection.remoteAddress();\nif (addr == null) { /* drop connection early */ }","typeGuard":"static boolean hasRemoteAddress(SocketAddress addr) {\n    return addr != null;\n}\n// usage\nif (!hasRemoteAddress(exchange.getRequest().remoteAddress())) { return; }","tryCatchPattern":"try {\n    String ip = getIp(socketAddress);\n} catch (NullPointerException e) {\n    if (\"remoteAddress is null\".equals(e.getMessage())) { /* close/drop the connection */ }\n    throw e;\n}","preventionTips":["Close connections lacking remote address info at the transport layer before protocol handling.","Log connection ids to trace which clients produce address-less connections."],"tags":["tcp","network","null-check","connection"],"backgroundTag":"null-argument","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}