From b60d1419204fb291a396c2cf2bbf29f2141a965f Mon Sep 17 00:00:00 2001 From: Rafael Cepeda Date: Mon, 1 Dec 2025 11:09:56 +0000 Subject: [PATCH] Document when `channel.remoteAddress` field can be `nil` (#3456) Added extra documentation to `channel.remoteAddress` field to capture a scenario where this field might be `nil` ### Motivation: It is somewhat common for users to have code like `channel.remoteAddress!` in their implementation, as it is a reasonable assumption to think a socket connection will have an associated remote address. However, in at least one known situation this might not be the case. When that happens, user's code might crash due to the force unwrap of the optional field. ### Modifications: Introduced more documentation to make it clear that users should be prepared to handle the `nil` scenario. ### Result: Less frequent mishandling of `channel.remoteAddress`. --- Sources/NIOChatClient/main.swift | 2 ++ Sources/NIOChatServer/main.swift | 5 +++++ Sources/NIOCore/Channel.swift | 5 +++++ Sources/NIOHTTP1Client/main.swift | 2 ++ 4 files changed, 14 insertions(+) diff --git a/Sources/NIOChatClient/main.swift b/Sources/NIOChatClient/main.swift index a350bf14b..249a3e64a 100644 --- a/Sources/NIOChatClient/main.swift +++ b/Sources/NIOChatClient/main.swift @@ -82,6 +82,8 @@ let channel = try { () -> Channel in } }() +// In production code, you should check if `channel.remoteAddress` is actually +// present, as in rare situations it can be `nil`. print("ChatClient connected to ChatServer: \(channel.remoteAddress!), happy chatting\n. Press ^D to exit.") while let line = readLine(strippingNewline: false) { diff --git a/Sources/NIOChatServer/main.swift b/Sources/NIOChatServer/main.swift index 791634f1d..5061ab773 100644 --- a/Sources/NIOChatServer/main.swift +++ b/Sources/NIOChatServer/main.swift @@ -54,6 +54,8 @@ final class ChatHandler: ChannelInboundHandler { private var channels: [ObjectIdentifier: Channel] = [:] public func channelActive(context: ChannelHandlerContext) { + // In production code, you should check if `context.remoteAddress` is actually + // present, as in rare situations it can be `nil`. let remoteAddress = context.remoteAddress! let channel = context.channel self.channelsSyncQueue.async { @@ -92,6 +94,9 @@ final class ChatHandler: ChannelInboundHandler { // 64 should be good enough for the ipaddress var buffer = context.channel.allocator.buffer(capacity: read.readableBytes + 64) + + // In production code, you should check if `context.remoteAddress` is actually + // present, as in rare situations it can be `nil`. buffer.writeString("(\(context.remoteAddress!)) - ") buffer.writeBuffer(&read) self.channelsSyncQueue.async { [buffer] in diff --git a/Sources/NIOCore/Channel.swift b/Sources/NIOCore/Channel.swift index d86362f6e..c25ad59c6 100644 --- a/Sources/NIOCore/Channel.swift +++ b/Sources/NIOCore/Channel.swift @@ -122,6 +122,11 @@ public protocol Channel: AnyObject, ChannelOutboundInvoker, _NIOPreconcurrencySe var localAddress: SocketAddress? { get } /// The remote peer's `SocketAddress`. + /// + /// If we end up accepting an already-closed connection, the kernel can end up in a place + /// where it has no remote address to give us. In this situation, `remoteAddress` will be + /// `nil`. It can also be `nil` in cases where it isn't representable in SocketAddress, e.g. if + /// we're talking over a vsock. var remoteAddress: SocketAddress? { get } /// `Channel`s are hierarchical and might have a parent `Channel`. `Channel` hierarchies are in use for certain diff --git a/Sources/NIOHTTP1Client/main.swift b/Sources/NIOHTTP1Client/main.swift index 41dece232..90fa44794 100644 --- a/Sources/NIOHTTP1Client/main.swift +++ b/Sources/NIOHTTP1Client/main.swift @@ -23,6 +23,8 @@ private final class HTTPEchoHandler: ChannelInboundHandler { public typealias OutboundOut = HTTPClientRequestPart public func channelActive(context: ChannelHandlerContext) { + // In production code, you should check if `context.remoteAddress` is actually + // present, as in rare situations it can be `nil`. print("Client connected to \(context.remoteAddress!)") // We are connected. It's time to send the message to the server to initialize the ping-pong sequence.