diff --git a/src/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs b/src/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs index 31761149..08819713 100644 --- a/src/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs +++ b/src/Renci.SshNet.Tests/Classes/ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Threading; diff --git a/src/Renci.SshNet/ForwardedPortDynamic.NET.cs b/src/Renci.SshNet/ForwardedPortDynamic.NET.cs index 9f5f27cb..477e88d6 100644 --- a/src/Renci.SshNet/ForwardedPortDynamic.NET.cs +++ b/src/Renci.SshNet/ForwardedPortDynamic.NET.cs @@ -79,9 +79,22 @@ namespace Renci.SshNet // only accept new connections while we are started if (IsStarted) { - if (!_listener.AcceptAsync(e)) + try { - AcceptCompleted(null, e); + if (!_listener.AcceptAsync(e)) + { + AcceptCompleted(null, e); + } + } + catch (ObjectDisposedException) + { + if (_status == ForwardedPortStatus.Stopped || _status == ForwardedPortStatus.Stopped) + { + // ignore ObjectDisposedException while stopping or stopped + return; + } + + throw; } } }