Skip to content

Commit

Permalink
Start MessageListener with ThreadAbstraction.ExecuteThreadLongRunning (
Browse files Browse the repository at this point in the history
…#902)

* Fix Thread pool exhaustion due to MessageListener running on ThreadPool
* Mark long running thread as background
  • Loading branch information
IgorMilavec authored Nov 28, 2021
1 parent 583a9ce commit 30d79c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Renci.SshNet/Abstractions/ThreadAbstraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ public static void Sleep(int millisecondsTimeout)

public static void ExecuteThreadLongRunning(Action action)
{
if (action == null)
throw new ArgumentNullException("action");

#if FEATURE_THREAD_TAP
var taskCreationOptions = System.Threading.Tasks.TaskCreationOptions.LongRunning;
System.Threading.Tasks.Task.Factory.StartNew(action, taskCreationOptions);
#else
var thread = new System.Threading.Thread(() => action());
thread.Start();
new System.Threading.Thread(() => action())
{
IsBackground = true
}.Start();
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ public void Connect()
_messageListenerCompleted.Reset();

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

// Wait for key exchange to be completed
WaitOnHandle(_keyExchangeCompletedWaitHandle);
Expand Down

0 comments on commit 30d79c7

Please sign in to comment.