ShellStream.Dispose deadlocks when the server closes the channel at the same time
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 69/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
- 领域
- backend, networking
调研方向
Start by reading src/Renci.SshNet/ShellStream.cs around Dispose(bool) and Channel_Closed, then trace the Close paths in src/Renci.SshNet/Channels/Channel.cs. Check how locks and event handlers interact in the reported race, and add a regression test that reproduces concurrent disposal and remote channel closure. Done means the test completes reliably without deadlocking.
由索引模型根据 Issue 内容生成。
描述
Version
SSH.NET 2026.0.0 (tag 2026.0.0, 7b2fd3d). The same code is on develop today. .NET 10, Linux x64 and arm64.
Summary
Disposing a ShellStream while the server closes its channel can deadlock forever. The two threads take the same two locks in opposite order, and neither wait has a timeout:
| Thread | Holds | Waits for |
|---|---|---|
Caller: ShellStream.Dispose() |
ShellStream._sync |
Channel._messagingLock |
Session listener: SSH_MSG_CHANNEL_CLOSE received |
Channel._messagingLock |
ShellStream._sync |
The two paths
Caller thread, disposing the stream:
ShellStream.Dispose(bool)takeslock (_sync)(src/Renci.SshNet/ShellStream.cs:970).- Still inside that lock it calls
_channel.Dispose()(ShellStream.cs:986). Channel.Dispose(bool)callsClose()(src/Renci.SshNet/Channels/Channel.cs:844), which takeslock (_messagingLock)(Channel.cs:534).
Listener thread, handling the server's SSH_MSG_CHANNEL_CLOSE:
OnChannelClosecallsOnClose()(Channel.cs:698), which callsClose()(Channel.cs:426) and takeslock (_messagingLock)(Channel.cs:534).- Still inside that lock it raises
Closed(Channel.cs:578). ShellStream.Channel_Closedhandles it by callingDispose()(ShellStream.cs:1006), which waits forlock (_sync)(ShellStream.cs:970).
If the caller enters step 1 of its path before the listener reaches step 3 of its own, each thread holds the lock the other one needs. A stack dump of a hung process shows exactly these two frames.
Repro
The race is timing-dependent. A simplified loop against a real OpenSSH server (a sketch of our test, not run as written here):
for (var i = 0; i < 100; i++)
{
using var client = new SshClient(host, port, user, password);
client.Connect();
var stream = client.CreateShellStream("xterm", 80, 24, 0, 0, 4096);
stream.WriteLine("exit"); // the server closes the channel shortly after
stream.Dispose(); // races with the listener's Channel_Closed -> Dispose
}
In our test suite this hung 4 of 15 runs: a test class whose Dispose() disposed the ShellStream while the remote shell exited. The process never recovered; CI killed it after an hour.
Workaround we use
Call SshClient.Disconnect() before disposing the ShellStream. That stops the listener thread, so no Closed event can arrive while Dispose holds _sync. With it, 12 of 12 runs passed.
Possible fixes
- In
ShellStream.Dispose, detach the handlers and dispose_channeloutsidelock (_sync): take the lock only to set_disposed, then release it before calling_channel.Dispose(). - Or, in
Channel.Close, raiseClosedafter leavinglock (_messagingLock), so that no user code runs while that lock is held.
- 主要语言
- C#
- 星标
- 4.4k
- 派生
- 992
- 平均合并
- 4 天 22 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sshnet/SSH.NET 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 55/100
-
难度 5/5 一周以上 新手友好度 45/100
-
ShellStream.Expect over-discards on undecodable bytes, driving ArrayBuffer.ActiveLength negative未关闭
难度 4/5 3-5 天 新手友好度 52/100
-
难度 3/5 1-2 天 新手友好度 68/100
-
难度 4/5 3-5 天 新手友好度 48/100
相似的 Issue
-
dev/release: verify-release-candidate.sh runs 0 Java tests with Debian's Maven可能已有人在做 @kou 今天认领。 未关闭Type: bug
难度 2/5 1-3 小时 新手友好度 72/100
apache/arrow-adbc#4947 ·
维护者通常 1 天内回复
-
:watch: Not Triaged CAC
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/AspNetCore.Docs#38001 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
Nimblesite/SharpLsp#468 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
elastic/ecs-dotnet#575 ·
-
alerts bug effort:S P2
难度 2/5 1-3 小时 新手友好度 76/100
nightscout/nocturne#2511 ·
维护者通常 1 天内回复