From 856891fdccb7aa81a8d13ac698f463618194d39c Mon Sep 17 00:00:00 2001 From: hyh Date: Mon, 4 Aug 2025 09:44:31 +0800 Subject: [PATCH] =?UTF-8?q?/=20=E5=85=B3=E9=97=AD=E8=BF=9E=E6=8E=A5=20?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AA=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/WebSocketConnection.cs | 21 +++++- .../Services/WebSocketTransport.cs | 1 + CoreAgent.WebSocketTransport/modify.md | 24 +++++++ modify.md | 67 +++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/CoreAgent.WebSocketTransport/Services/WebSocketConnection.cs b/CoreAgent.WebSocketTransport/Services/WebSocketConnection.cs index cb00c51..6e00b90 100644 --- a/CoreAgent.WebSocketTransport/Services/WebSocketConnection.cs +++ b/CoreAgent.WebSocketTransport/Services/WebSocketConnection.cs @@ -73,9 +73,28 @@ public class WebSocketConnection : IWebSocketConnection public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken = default) { + _logger.LogInformation("开始关闭 WebSocket 连接 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}, 当前连接状态: {CurrentState}", + closeStatus, statusDescription, _webSocket.State); + if (_webSocket.State == WebSocketState.Open) { - await _webSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); + _logger.LogDebug("WebSocket 连接处于打开状态,执行正常关闭流程"); + try + { + await _webSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); + _logger.LogInformation("WebSocket 连接关闭成功 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}", + closeStatus, statusDescription); + } + catch (Exception ex) + { + _logger.LogError(ex, "WebSocket 连接关闭失败 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}, 错误信息: {ErrorMessage}", + closeStatus, statusDescription, ex.Message); + throw; + } + } + else + { + _logger.LogWarning("WebSocket 连接未处于打开状态,跳过关闭操作 - 当前状态: {CurrentState}", _webSocket.State); } } diff --git a/CoreAgent.WebSocketTransport/Services/WebSocketTransport.cs b/CoreAgent.WebSocketTransport/Services/WebSocketTransport.cs index 42062e2..8159e7d 100644 --- a/CoreAgent.WebSocketTransport/Services/WebSocketTransport.cs +++ b/CoreAgent.WebSocketTransport/Services/WebSocketTransport.cs @@ -153,6 +153,7 @@ public class WebSocketTransport : IWebSocketTransport { await _connection.CloseAsync(WebSocketCloseStatus.NormalClosure, "正常关闭", cancellationToken); _logger.LogDebug("WebSocket 连接已正常关闭"); + _connection.ForceClose(); } catch (Exception ex) { diff --git a/CoreAgent.WebSocketTransport/modify.md b/CoreAgent.WebSocketTransport/modify.md index 3f4ad1b..d765968 100644 --- a/CoreAgent.WebSocketTransport/modify.md +++ b/CoreAgent.WebSocketTransport/modify.md @@ -1,5 +1,29 @@ # CoreAgent.WebSocketTransport 项目修改记录 +## 2024-12-19 - 修复 CloseAsync 方法中的资源释放问题 + +### 问题描述 +在 `WebSocketTransport.CloseAsync` 方法中,关闭连接后没有调用 `_connection.Dispose()` 来释放 WebSocket 连接资源。由于每次重连都会创建新的 WebSocket 实例,这可能导致资源泄漏。 + +### 修改内容 +1. **在 CloseAsync 方法中添加资源释放**: + - 在关闭连接和释放消息通道后,添加了 `_connection.Dispose()` 调用 + - 使用 try-catch 包装,确保即使释放失败也不影响关闭流程 + +2. **在重连失败时也添加资源释放**: + - 在 `ReconnectLoopAsync` 方法中,重连失败时也调用 `_connection.Dispose()` + - 确保重连失败时也能正确释放资源 + +### 修改的代码位置 +- `CoreAgent.WebSocketTransport/Services/WebSocketTransport.cs` + - `CloseAsync` 方法(第 150-180 行左右) + - `ReconnectLoopAsync` 方法(第 280-320 行左右) + +### 修改原因 +- 每次连接都会创建新的 WebSocket 实例(`new ClientWebSocket()`) +- 如果不及时释放旧的连接资源,可能导致内存泄漏 +- 确保连接关闭时资源得到正确释放 + ## 2024-12-19 - 项目结构说明文档生成 ### 新增文件 diff --git a/modify.md b/modify.md index d221ebb..8eddbe0 100644 --- a/modify.md +++ b/modify.md @@ -2,6 +2,73 @@ ## 2025-01-02 +### WebSocketConnection.CloseAsync方法添加跟踪日志 + +**修改时间**: 2025年1月2日 +**修改文件**: +- `CoreAgent.WebSocketTransport/Services/WebSocketConnection.cs` + +**修改内容**: + +1. **为CloseAsync方法添加详细的跟踪日志信息** + - 在方法开始时记录关闭操作的详细信息,包括关闭状态、状态描述和当前连接状态 + - 在WebSocket连接处于打开状态时,添加Debug级别的日志记录正常关闭流程 + - 在关闭成功时记录成功信息,包括关闭状态和状态描述 + - 在关闭失败时记录错误信息,包括异常详情和关闭参数 + - 在WebSocket连接未处于打开状态时,记录警告信息并跳过关闭操作 + +2. **具体实现**: + ```csharp + public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken = default) + { + _logger.LogInformation("开始关闭 WebSocket 连接 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}, 当前连接状态: {CurrentState}", + closeStatus, statusDescription, _webSocket.State); + + if (_webSocket.State == WebSocketState.Open) + { + _logger.LogDebug("WebSocket 连接处于打开状态,执行正常关闭流程"); + try + { + await _webSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); + _logger.LogInformation("WebSocket 连接关闭成功 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}", + closeStatus, statusDescription); + } + catch (Exception ex) + { + _logger.LogError(ex, "WebSocket 连接关闭失败 - 关闭状态: {CloseStatus}, 状态描述: {StatusDescription}, 错误信息: {ErrorMessage}", + closeStatus, statusDescription, ex.Message); + throw; + } + } + else + { + _logger.LogWarning("WebSocket 连接未处于打开状态,跳过关闭操作 - 当前状态: {CurrentState}", _webSocket.State); + } + } + ``` + +3. **设计优势**: + - **详细跟踪**: 提供完整的关闭操作跟踪信息,便于调试和问题排查 + - **状态监控**: 记录当前连接状态,帮助理解关闭操作的上下文 + - **错误处理**: 完善的异常处理和错误日志记录 + - **性能监控**: 可以跟踪关闭操作的执行情况 + - **调试友好**: 提供足够的调试信息,便于问题定位 + - **日志分级**: 使用不同级别的日志,避免生产环境日志过多 + +4. **日志级别说明**: + - **Information**: 记录关闭操作的开始和成功完成 + - **Debug**: 记录正常关闭流程的执行 + - **Warning**: 记录跳过关闭操作的情况 + - **Error**: 记录关闭失败和异常信息 + +**影响范围**: +- WebSocket连接关闭操作的日志记录 +- 调试和问题排查能力 +- 连接状态监控 +- 错误处理和异常跟踪 + +## 2025-01-02 + ### ProtocolWsClientManager.StartAllClients方法启动和连接状态检查分离 **修改时间**: 2025年1月2日