diff options
| author | Cody Robibero <cody@robibe.ro> | 2024-03-30 17:28:03 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-30 17:28:03 -0600 |
| commit | ed82d796473455c3b89e640802eee24242801a3b (patch) | |
| tree | fe711fc0ec0bd350cb34f615c8bff31ccdfa58bf /src | |
| parent | 000395e03682ff796de56c76be09575f07853be1 (diff) | |
Catch exceptions in auto discovery (#11252)
Diffstat (limited to 'src')
| -rw-r--r-- | src/Jellyfin.Networking/AutoDiscoveryHost.cs | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/src/Jellyfin.Networking/AutoDiscoveryHost.cs b/src/Jellyfin.Networking/AutoDiscoveryHost.cs index 5624c4ed1..2be57d7a1 100644 --- a/src/Jellyfin.Networking/AutoDiscoveryHost.cs +++ b/src/Jellyfin.Networking/AutoDiscoveryHost.cs @@ -78,28 +78,36 @@ public sealed class AutoDiscoveryHost : BackgroundService private async Task ListenForAutoDiscoveryMessage(IPAddress address, CancellationToken cancellationToken) { - using var udpClient = new UdpClient(new IPEndPoint(address, PortNumber)); - udpClient.MulticastLoopback = false; - - while (!cancellationToken.IsCancellationRequested) + try { - try + using var udpClient = new UdpClient(new IPEndPoint(address, PortNumber)); + udpClient.MulticastLoopback = false; + + while (!cancellationToken.IsCancellationRequested) { - var result = await udpClient.ReceiveAsync(cancellationToken).ConfigureAwait(false); - var text = Encoding.UTF8.GetString(result.Buffer); - if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase)) + try { - await RespondToV2Message(udpClient, result.RemoteEndPoint, cancellationToken).ConfigureAwait(false); + var result = await udpClient.ReceiveAsync(cancellationToken).ConfigureAwait(false); + var text = Encoding.UTF8.GetString(result.Buffer); + if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase)) + { + await RespondToV2Message(udpClient, result.RemoteEndPoint, cancellationToken).ConfigureAwait(false); + } + } + catch (SocketException ex) + { + _logger.LogError(ex, "Failed to receive data from socket"); } } - catch (SocketException ex) - { - _logger.LogError(ex, "Failed to receive data from socket"); - } - catch (OperationCanceledException) - { - _logger.LogDebug("Broadcast socket operation cancelled"); - } + } + catch (OperationCanceledException) + { + _logger.LogDebug("Broadcast socket operation cancelled"); + } + catch (Exception ex) + { + // Exception in this function will prevent the background service from restarting in-process. + _logger.LogError(ex, "Unable to bind to {Address}:{Port}", address, PortNumber); } } |
