diff --git a/agent/agent.go b/agent/agent.go index 45d4bb0ac2..229585b40e 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -882,12 +882,16 @@ const ( ) func (a *agent) reportConnection(id uuid.UUID, connectionType proto.Connection_Type, ip string) (disconnected func(code int, reason string)) { - // Remove the port from the IP because ports are not supported in coderd. - if host, _, err := net.SplitHostPort(ip); err != nil { - a.logger.Error(a.hardCtx, "split host and port for connection report failed", slog.F("ip", ip), slog.Error(err)) - } else { - // Best effort. - ip = host + // A blank IP can unfortunately happen if the connection is broken in a data race before we get to introspect it. We + // still report it, and the recipient can handle a blank IP. + if ip != "" { + // Remove the port from the IP because ports are not supported in coderd. + if host, _, err := net.SplitHostPort(ip); err != nil { + a.logger.Error(a.hardCtx, "split host and port for connection report failed", slog.F("ip", ip), slog.Error(err)) + } else { + // Best effort. + ip = host + } } // If the IP is "localhost" (which it can be in some cases), set it to