登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
轻量养虾,开箱即用!低 Token + 稳定算力,Gitee & 模力方舟联合出品的 PocketClaw 正式开售!点击了解详情
代码拉取完成,页面将自动刷新
开源项目
>
程序开发
>
网络开发包
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
351
Star
2.9K
Fork
850
GVP
若汝棋茗
/
TouchSocket
代码
Issues
27
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
开发画像分析
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
[WebSocket]服务端WebSocket使用AsyncClose()主动关闭连接时引发NullReferenceException异常
已完成
#IC1FZ8
千层饼
创建于
2025-04-15 22:53
### 组件 WebSocket ### 版本号 3.0.25 ### .NET SDK .NET6 ### 项目类型 Console ### 操作系统 Windows 11 ### 运行环境 开发环境 (Development) ### 这个问题是否已经存在? - [x] 我已经搜索过现有的问题 (https://gitee.com/RRQM_Home/TouchSocket/issues) ### 如何复现 在HttpService中创建websocket服务器。对Online的IWebSocket进行CloseAsync()会引发NullReferenceException 对于下面提供的demo仓库中的最小复现代码: 1. 启动程序 2. apifox连接ws://127.0.0.1:11451/ws, 并发送任意文本 3. 控制台打印错误 ### 预期结果 对一个Online的IWebSocket进行CloseAsync()不会引发异常 ### 实际结果 对一个Online的IWebSocket进行CloseAsync()报错 ### 异常信息 ## 异常信息: ```csharp System.NullReferenceException: Object reference not set to an instance of an object. at TouchSocket.Http.WebSockets.InternalWebSocket.CloseAsync(WebSocketCloseStatus closeStatus, String statusDescription) in D:\OpenCode\TouchSocketPro\Src\TouchSocket.Http\WebSockets\Common\InternalWebSocket.cs:line 79 at TouchSocketTest.WebSocketTestPlugin.OnWebSocketReceived(IWebSocket webSocket, WSDataFrameEventArgs e) in E:\workspace\MCSLTeam\touch-socket-exception-demo\TouchSocketTest\WebSocketTestPlugin.cs:line 18 ``` ## 截图:  ## 调用过程: ```csharp using TouchSocket.Core; using TouchSocket.Http.WebSockets; namespace TouchSocketTest; public class WebSocketTestPlugin : PluginBase, IWebSocketReceivedPlugin { public async Task OnWebSocketReceived(IWebSocket webSocket, WSDataFrameEventArgs e) { if (e.DataFrame.IsText) { await webSocket.SendAsync(e.DataFrame.ToText()); } try { // 使用CloseAsync关闭连接, 会报错: NullReferenceException await webSocket.CloseAsync(string.Empty); } catch (Exception exception) { Console.WriteLine(exception); throw; } // await e.InvokeNext(); } } ``` 代码执行到 ```csharp webSocket.CloseAsync(string.Empty) ``` 时, 会执行到[InternalWebSocket::CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription)](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs#L57)。 在 ```csharp // 第79行 await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); ``` 处因为this.m_httpClientBase为null而引发空指针异常, [(代码导航)](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs#L79)。 ## 可能的解决方案 [InternalWebSocket](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs)的2种构造函数可能分别对应WebSocket客户端和服务端的构造函数, 这导致字段m_httpClientBase和m_httpSocketClient二者不会同时被赋值, 但是字段m_isServer提供了判断依据, 因此我认为可能的解决方案如下(可能有不对的地方): 将 ```csharp internal sealed partial class InternalWebSocket : IWebSocket { private readonly HttpClientBase m_httpClientBase; private readonly HttpSessionClient m_httpSocketClient; private readonly bool m_isServer; // ... public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription) { // ... // 第79行开始 await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpClientBase.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); await this.m_httpSocketClient.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpSocketClient.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } // ... } ``` 更改为 ```csharp internal sealed partial class InternalWebSocket : IWebSocket { private readonly HttpClientBase m_httpClientBase; private readonly HttpSessionClient m_httpSocketClient; private readonly bool m_isServer; // ... public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription) { // ... // 第79行开始 if (this.m_isServer) { await this.m_httpSocketClient.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpSocketClient.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } else { await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpClientBase.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } } // ... } ``` ### Demo 地址(仅限Git地址) https://gitee.com/ares_connor/touch-socket-exception-demo ### 承诺支持 - [x] 我确定我已经对TouchSocket项目进行了“Star”操作。 ### 承诺规范 - [x] 我确定已完整阅读[Issue提问规范](https://touchsocket.net/docs/current/troubleshootissue),并按照要求填写。 ### 承诺友好 - [x] 我承诺将本着相互尊重、理解和友善的态度进行交流,共同维护好 TouchSocket 来之不易的良好的社区氛围。
### 组件 WebSocket ### 版本号 3.0.25 ### .NET SDK .NET6 ### 项目类型 Console ### 操作系统 Windows 11 ### 运行环境 开发环境 (Development) ### 这个问题是否已经存在? - [x] 我已经搜索过现有的问题 (https://gitee.com/RRQM_Home/TouchSocket/issues) ### 如何复现 在HttpService中创建websocket服务器。对Online的IWebSocket进行CloseAsync()会引发NullReferenceException 对于下面提供的demo仓库中的最小复现代码: 1. 启动程序 2. apifox连接ws://127.0.0.1:11451/ws, 并发送任意文本 3. 控制台打印错误 ### 预期结果 对一个Online的IWebSocket进行CloseAsync()不会引发异常 ### 实际结果 对一个Online的IWebSocket进行CloseAsync()报错 ### 异常信息 ## 异常信息: ```csharp System.NullReferenceException: Object reference not set to an instance of an object. at TouchSocket.Http.WebSockets.InternalWebSocket.CloseAsync(WebSocketCloseStatus closeStatus, String statusDescription) in D:\OpenCode\TouchSocketPro\Src\TouchSocket.Http\WebSockets\Common\InternalWebSocket.cs:line 79 at TouchSocketTest.WebSocketTestPlugin.OnWebSocketReceived(IWebSocket webSocket, WSDataFrameEventArgs e) in E:\workspace\MCSLTeam\touch-socket-exception-demo\TouchSocketTest\WebSocketTestPlugin.cs:line 18 ``` ## 截图:  ## 调用过程: ```csharp using TouchSocket.Core; using TouchSocket.Http.WebSockets; namespace TouchSocketTest; public class WebSocketTestPlugin : PluginBase, IWebSocketReceivedPlugin { public async Task OnWebSocketReceived(IWebSocket webSocket, WSDataFrameEventArgs e) { if (e.DataFrame.IsText) { await webSocket.SendAsync(e.DataFrame.ToText()); } try { // 使用CloseAsync关闭连接, 会报错: NullReferenceException await webSocket.CloseAsync(string.Empty); } catch (Exception exception) { Console.WriteLine(exception); throw; } // await e.InvokeNext(); } } ``` 代码执行到 ```csharp webSocket.CloseAsync(string.Empty) ``` 时, 会执行到[InternalWebSocket::CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription)](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs#L57)。 在 ```csharp // 第79行 await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); ``` 处因为this.m_httpClientBase为null而引发空指针异常, [(代码导航)](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs#L79)。 ## 可能的解决方案 [InternalWebSocket](https://gitee.com/RRQM_Home/TouchSocket/blob/master/src/TouchSocket.Http/WebSockets/Common/InternalWebSocket.cs)的2种构造函数可能分别对应WebSocket客户端和服务端的构造函数, 这导致字段m_httpClientBase和m_httpSocketClient二者不会同时被赋值, 但是字段m_isServer提供了判断依据, 因此我认为可能的解决方案如下(可能有不对的地方): 将 ```csharp internal sealed partial class InternalWebSocket : IWebSocket { private readonly HttpClientBase m_httpClientBase; private readonly HttpSessionClient m_httpSocketClient; private readonly bool m_isServer; // ... public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription) { // ... // 第79行开始 await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpClientBase.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); await this.m_httpSocketClient.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpSocketClient.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } // ... } ``` 更改为 ```csharp internal sealed partial class InternalWebSocket : IWebSocket { private readonly HttpClientBase m_httpClientBase; private readonly HttpSessionClient m_httpSocketClient; private readonly bool m_isServer; // ... public async Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription) { // ... // 第79行开始 if (this.m_isServer) { await this.m_httpSocketClient.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpSocketClient.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } else { await this.m_httpClientBase.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both); await this.m_httpClientBase.SafeCloseAsync(statusDescription).ConfigureAwait(EasyTask.ContinueOnCapturedContext); } } // ... } ``` ### Demo 地址(仅限Git地址) https://gitee.com/ares_connor/touch-socket-exception-demo ### 承诺支持 - [x] 我确定我已经对TouchSocket项目进行了“Star”操作。 ### 承诺规范 - [x] 我确定已完整阅读[Issue提问规范](https://touchsocket.net/docs/current/troubleshootissue),并按照要求填写。 ### 承诺友好 - [x] 我承诺将本着相互尊重、理解和友善的态度进行交流,共同维护好 TouchSocket 来之不易的良好的社区氛围。
评论 (
1
)
登录
后才可以发表评论
状态
已完成
待办的
进行中
已完成
已关闭
负责人
未设置
标签
bug
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (
-
)
标签 (
-
)
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(2)
C#
1
https://gitee.com/RRQM_Home/TouchSocket.git
git@gitee.com:RRQM_Home/TouchSocket.git
RRQM_Home
TouchSocket
TouchSocket
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册