Skip to content

Commit c64803a

Browse files
IgorMilavecdrieseng
authored andcommitted
Start MessageListener with ThreadAbstraction.ExecuteThreadLongRunning (#902)
* Fix Thread pool exhaustion due to MessageListener running on ThreadPool * Mark long running thread as background
1 parent 814843e commit c64803a

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/Renci.SshNet/Abstractions/ThreadAbstraction.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ public static void Sleep(int millisecondsTimeout)
2121

2222
public static void ExecuteThreadLongRunning(Action action)
2323
{
24+
if (action == null)
25+
throw new ArgumentNullException("action");
26+
2427
#if FEATURE_THREAD_TAP
2528
var taskCreationOptions = System.Threading.Tasks.TaskCreationOptions.LongRunning;
2629
System.Threading.Tasks.Task.Factory.StartNew(action, taskCreationOptions);
2730
#else
28-
var thread = new System.Threading.Thread(() => action());
29-
thread.Start();
31+
new System.Threading.Thread(() => action())
32+
{
33+
IsBackground = true
34+
}.Start();
3035
#endif
3136
}
3237

src/Renci.SshNet/Session.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ public void Connect()
618618
_messageListenerCompleted.Reset();
619619

620620
// Start incoming request listener
621-
ThreadAbstraction.ExecuteThread(() => MessageListener());
621+
// ToDo: Make message pump async, to not consume a thread for every session
622+
ThreadAbstraction.ExecuteThreadLongRunning(() => MessageListener());
622623

623624
// Wait for key exchange to be completed
624625
WaitOnHandle(_keyExchangeCompletedWaitHandle);

0 commit comments

Comments
 (0)