diff options
| author | Claus Vium <cvium@users.noreply.github.com> | 2024-03-30 17:30:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-30 10:30:00 -0600 |
| commit | 4201079b349c34372aa9375791aa86d7e90572f1 (patch) | |
| tree | 94e72aad47f0390ff6e693b7771f6042635b4471 /MediaBrowser.Controller/Net | |
| parent | fe88a484d18ddbb2fb6859b93ff2e05a166359f1 (diff) | |
fix: use a reentrant lock when accessing active connections (#11256)
Diffstat (limited to 'MediaBrowser.Controller/Net')
| -rw-r--r-- | MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs | 37 |
1 files changed, 6 insertions, 31 deletions
diff --git a/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs b/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs index 219da309e..06386f2b8 100644 --- a/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs +++ b/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs @@ -33,7 +33,7 @@ namespace MediaBrowser.Controller.Net SingleWriter = false }); - private readonly SemaphoreSlim _lock = new(1, 1); + private readonly object _activeConnectionsLock = new(); /// <summary> /// The _active connections. @@ -126,15 +126,10 @@ namespace MediaBrowser.Controller.Net InitialDelayMs = dueTimeMs }; - _lock.Wait(); - try + lock (_activeConnectionsLock) { _activeConnections.Add((message.Connection, cancellationTokenSource, state)); } - finally - { - _lock.Release(); - } } protected void SendData(bool force) @@ -153,8 +148,7 @@ namespace MediaBrowser.Controller.Net (IWebSocketConnection Connection, CancellationTokenSource CancellationTokenSource, TStateType State)[] tuples; var now = DateTime.UtcNow; - await _lock.WaitAsync().ConfigureAwait(false); - try + lock (_activeConnectionsLock) { if (_activeConnections.Count == 0) { @@ -174,10 +168,6 @@ namespace MediaBrowser.Controller.Net }) .ToArray(); } - finally - { - _lock.Release(); - } if (tuples.Length == 0) { @@ -240,8 +230,7 @@ namespace MediaBrowser.Controller.Net /// <param name="message">The message.</param> private void Stop(WebSocketMessageInfo message) { - _lock.Wait(); - try + lock (_activeConnectionsLock) { var connection = _activeConnections.FirstOrDefault(c => c.Connection == message.Connection); @@ -250,10 +239,6 @@ namespace MediaBrowser.Controller.Net DisposeConnection(connection); } } - finally - { - _lock.Release(); - } } /// <summary> @@ -283,15 +268,10 @@ namespace MediaBrowser.Controller.Net Logger.LogError(ex, "Error disposing websocket"); } - _lock.Wait(); - try + lock (_activeConnectionsLock) { _activeConnections.Remove(connection); } - finally - { - _lock.Release(); - } } protected virtual async ValueTask DisposeAsyncCore() @@ -306,18 +286,13 @@ namespace MediaBrowser.Controller.Net Logger.LogError(ex, "Disposing the message consumer failed"); } - await _lock.WaitAsync().ConfigureAwait(false); - try + lock (_activeConnectionsLock) { foreach (var connection in _activeConnections.ToArray()) { DisposeConnection(connection); } } - finally - { - _lock.Release(); - } } /// <inheritdoc /> |
