From b01449481912b07f4e551ad2460ea1a045e7c98f Mon Sep 17 00:00:00 2001 From: wenzuhuai Date: Thu, 22 Jan 2026 09:52:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20write=E6=93=8D=E4=BD=9C=E5=9C=A8batchBuf?= =?UTF-8?q?fer=E4=B8=BAnil=E6=97=B6=E4=B9=9F=E8=A7=A6=E5=8F=91=E9=87=8D?= =?UTF-8?q?=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 解决"假连接"边界情况:状态为connected但batchBuffer为nil - 确保发送消息失败后能自动恢复连接 --- Sources/Nats/NatsConnection.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/Nats/NatsConnection.swift b/Sources/Nats/NatsConnection.swift index 1bc4e8a..08820a6 100644 --- a/Sources/Nats/NatsConnection.swift +++ b/Sources/Nats/NatsConnection.swift @@ -1056,6 +1056,13 @@ class ConnectionHandler: ChannelInboundHandler { func write(operation: ClientOp) async throws { guard let buffer = self.batchBuffer else { + // If state is connected but batchBuffer is nil, this is a "fake connection" state + // Trigger reconnect to recover + let currentState = state.withLockedValue { $0 } + if currentState == .connected { + logger.error("Write failed: batchBuffer is nil but state is connected, triggering reconnect") + handleDisconnect() + } throw NatsError.ClientError.invalidConnection("not connected") } do {