Skip to content

Commit

Permalink
Fix race condition between DisconnectAsync and Dispose (PowerShell#16536
Browse files Browse the repository at this point in the history
)
  • Loading branch information
i3arnon authored Dec 2, 2021
1 parent 64d2fe2 commit 1920b1b
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -813,18 +813,26 @@ private void HandleReadyForDisconnect(object sender, EventArgs args)
// what thread this callback is made from. If it was made from a transport
// callback event then a deadlock may occur when DisconnectAsync is called on
// that same thread.
ThreadPool.QueueUserWorkItem(new WaitCallback(StartDisconnectAsync), RemoteSession);
ThreadPool.QueueUserWorkItem(new WaitCallback(StartDisconnectAsync));
}
}
}

/// <summary>
/// WaitCallback method to start an asynchronous disconnect.
/// </summary>
/// <param name="remoteSession"></param>
private void StartDisconnectAsync(object remoteSession)
/// <param name="state"></param>
private void StartDisconnectAsync(object state)
{
((ClientRemoteSession)remoteSession).DisconnectAsync();
var remoteSession = RemoteSession;
try
{
remoteSession?.DisconnectAsync();
}
catch
{
// remoteSession may have already been disposed resulting in unexpected exceptions.
}
}

/// <summary>
Expand Down

0 comments on commit 1920b1b

Please sign in to comment.