Correctly dispose WebSocketSharpListener

This commit is contained in:
Bond-009 2019-02-13 17:36:10 +01:00
parent 46897aab4f
commit 2cb747651b

View File

@ -223,38 +223,39 @@ namespace Jellyfin.Server.SocketSharp
public Task Stop() public Task Stop()
{ {
_disposeCancellationTokenSource.Cancel(); _disposeCancellationTokenSource.Cancel();
_listener?.Close();
if (_listener != null)
{
_listener.Close();
}
return Task.CompletedTask; return Task.CompletedTask;
} }
/// <summary>
/// Releases the unmanaged resources and disposes of the managed resources used.
/// </summary>
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
GC.SuppressFinalize(this);
} }
private bool _disposed; private bool _disposed;
private readonly object _disposeLock = new object();
/// <summary>
/// Releases the unmanaged resources and disposes of the managed resources used.
/// </summary>
/// <param name="disposing">Whether or not the managed resources should be disposed</param>
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (_disposed) return; if (_disposed)
lock (_disposeLock)
{ {
if (_disposed) return; return;
if (disposing)
{
Stop();
}
// release unmanaged resources here...
_disposed = true;
} }
if (disposing)
{
Stop().GetAwaiter().GetResult();
}
_disposed = true;
} }
} }
} }