2 Commits
1.0.1 ... 1.0.3

Author SHA1 Message Date
wenzuhuai
b014494819 fix: write操作在batchBuffer为nil时也触发重连
- 解决"假连接"边界情况:状态为connected但batchBuffer为nil
- 确保发送消息失败后能自动恢复连接
2026-01-22 09:52:52 +08:00
wenzuhuai
387f6cf273 fix: 修复闭包参数混用导致的编译错误 2026-01-22 09:18:44 +08:00

View File

@@ -937,7 +937,6 @@ class ConnectionHandler: ChannelInboundHandler {
if currentState == .disconnected || currentState == .closed { if currentState == .disconnected || currentState == .closed {
return false // Already in disconnected/closed state return false // Already in disconnected/closed state
} }
$0 = .disconnected
return true return true
} }
@@ -945,6 +944,9 @@ class ConnectionHandler: ChannelInboundHandler {
return return
} }
// Set state to disconnected after check
state.withLockedValue { $0 = .disconnected }
// Clean up pending continuations to prevent leaks // Clean up pending continuations to prevent leaks
if let continuation = serverInfoContinuation.withLockedValue({ cont in if let continuation = serverInfoContinuation.withLockedValue({ cont in
let toResume = cont let toResume = cont
@@ -1054,6 +1056,13 @@ class ConnectionHandler: ChannelInboundHandler {
func write(operation: ClientOp) async throws { func write(operation: ClientOp) async throws {
guard let buffer = self.batchBuffer else { 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") throw NatsError.ClientError.invalidConnection("not connected")
} }
do { do {