Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

ShellStream.Dispose deadlocks when the server closes the channel at the same time

未關閉
#1,851 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
69/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
csharp

研究方向

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:

  1. ShellStream.Dispose(bool) takes lock (_sync) (src/Renci.SshNet/ShellStream.cs:970).
  2. Still inside that lock it calls _channel.Dispose() (ShellStream.cs:986).
  3. Channel.Dispose(bool) calls Close() (src/Renci.SshNet/Channels/Channel.cs:844), which takes lock (_messagingLock) (Channel.cs:534).

Listener thread, handling the server's SSH_MSG_CHANNEL_CLOSE:

  1. OnChannelClose calls OnClose() (Channel.cs:698), which calls Close() (Channel.cs:426) and takes lock (_messagingLock) (Channel.cs:534).
  2. Still inside that lock it raises Closed (Channel.cs:578).
  3. ShellStream.Channel_Closed handles it by calling Dispose() (ShellStream.cs:1006), which waits for lock (_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 _channel outside lock (_sync): take the lock only to set _disposed, then release it before calling _channel.Dispose().
  • Or, in Channel.Close, raise Closed after leaving lock (_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 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

sshnet/SSH.NET 的其他 Issue

查看 sshnet/SSH.NET 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。