From 4be3f5f1f9ff8bd0333033d6ad9c99711da03f96 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 20:26:39 +0200 Subject: Add Accept-Language header support for per-request localization --- .../HttpServer/WebSocketConnection.cs | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index 373b0994a6..6dc6d9d289 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -1,5 +1,7 @@ using System; using System.Buffers; +using System.Collections.Generic; +using System.Globalization; using System.IO.Pipelines; using System.Net; using System.Net.WebSockets; @@ -7,6 +9,7 @@ using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using Emby.Server.Implementations.Localization; using Jellyfin.Extensions.Json; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net.WebSocketMessages; @@ -69,6 +72,17 @@ namespace Emby.Server.Implementations.HttpServer /// public IPAddress? RemoteEndPoint { get; } + /// + /// Gets or initializes the culture fallback chain captured from the + /// Accept-Language header of the upgrade request. + /// + public IReadOnlyList? RequestCultureFallback { get; init; } + + /// + /// Gets or initializes the UI culture name captured from the upgrade request. + /// + public string? RequestUICulture { get; init; } + /// public Func? OnReceive { get; set; } @@ -81,6 +95,28 @@ namespace Emby.Server.Implementations.HttpServer /// public WebSocketState State => _socket.State; + /// + public void ApplyRequestCulture() + { + if (RequestCultureFallback is not null) + { + LocalizationManager.RequestCultureFallback = RequestCultureFallback; + } + + if (!string.IsNullOrEmpty(RequestUICulture)) + { + try + { + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); + } + catch (CultureNotFoundException) + { + // Jellyfin culture codes (e.g. "es_419") aren't always valid .NET cultures — + // skip setting CurrentUICulture; RequestCultureFallback above carries the chain. + } + } + } + /// public async Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken) { -- cgit v1.2.3 From 5cfb379aa63689435077c8f1ebc10c98f625238c Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 21:57:11 +0200 Subject: Use native middleware --- .../HttpServer/WebSocketConnection.cs | 28 ++--- .../HttpServer/WebSocketManager.cs | 4 +- .../Localization/LocalizationManager.cs | 63 ++++------ .../Middleware/AcceptLanguageMiddleware.cs | 137 --------------------- Jellyfin.Server/Startup.cs | 14 ++- .../Localization/LocalizationManagerTests.cs | 86 ++----------- 6 files changed, 54 insertions(+), 278 deletions(-) delete mode 100644 Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index 6dc6d9d289..17070c39ba 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -1,6 +1,5 @@ using System; using System.Buffers; -using System.Collections.Generic; using System.Globalization; using System.IO.Pipelines; using System.Net; @@ -9,7 +8,6 @@ using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; using Jellyfin.Extensions.Json; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net.WebSocketMessages; @@ -72,12 +70,6 @@ namespace Emby.Server.Implementations.HttpServer /// public IPAddress? RemoteEndPoint { get; } - /// - /// Gets or initializes the culture fallback chain captured from the - /// Accept-Language header of the upgrade request. - /// - public IReadOnlyList? RequestCultureFallback { get; init; } - /// /// Gets or initializes the UI culture name captured from the upgrade request. /// @@ -98,22 +90,18 @@ namespace Emby.Server.Implementations.HttpServer /// public void ApplyRequestCulture() { - if (RequestCultureFallback is not null) + if (string.IsNullOrEmpty(RequestUICulture)) { - LocalizationManager.RequestCultureFallback = RequestCultureFallback; + return; } - if (!string.IsNullOrEmpty(RequestUICulture)) + try { - try - { - CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); - } - catch (CultureNotFoundException) - { - // Jellyfin culture codes (e.g. "es_419") aren't always valid .NET cultures — - // skip setting CurrentUICulture; RequestCultureFallback above carries the chain. - } + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); + } + catch (CultureNotFoundException) + { + // Codes that aren't valid .NET cultures are ignored. } } diff --git a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs index 3b5f6d1d09..dcdfda5472 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs @@ -8,7 +8,6 @@ using System.Globalization; using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Net; using Microsoft.AspNetCore.Http; @@ -50,7 +49,7 @@ namespace Emby.Server.Implementations.HttpServer WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false); - // Capture the culture context set by AcceptLanguageMiddleware so it can be + // Capture the culture set by RequestLocalizationMiddleware so it can be // restored both when processing incoming messages and when periodic // listeners produce server-initiated payloads on background tasks. var connection = new WebSocketConnection( @@ -59,7 +58,6 @@ namespace Emby.Server.Implementations.HttpServer authorizationInfo, context.GetNormalizedRemoteIP()) { - RequestCultureFallback = LocalizationManager.RequestCultureFallback, RequestUICulture = CultureInfo.CurrentUICulture.Name }; connection.OnReceive = result => diff --git a/Emby.Server.Implementations/Localization/LocalizationManager.cs b/Emby.Server.Implementations/Localization/LocalizationManager.cs index 2a7a3388aa..5c2376ea00 100644 --- a/Emby.Server.Implementations/Localization/LocalizationManager.cs +++ b/Emby.Server.Implementations/Localization/LocalizationManager.cs @@ -8,7 +8,6 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; -using System.Threading; using System.Threading.Tasks; using Jellyfin.Extensions; using Jellyfin.Extensions.Json; @@ -32,10 +31,9 @@ namespace Emby.Server.Implementations.Localization private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly; private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"]; - /// - /// Gets the mapping from BCP-47 hyphenated culture codes to Jellyfin's underscore-based codes. - /// - public static readonly FrozenDictionary Bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + // Maps BCP-47 hyphenated culture codes (set by ASP.NET Core's RequestLocalizationMiddleware + // and used as CurrentUICulture.Name) to Jellyfin's underscore-based resource file codes. + private static readonly FrozenDictionary _bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) { ["es-419"] = "es_419", ["es-DO"] = "es_DO", @@ -47,8 +45,6 @@ namespace Emby.Server.Implementations.Localization private readonly Dictionary> _allParentalRatings = new(StringComparer.OrdinalIgnoreCase); - private static readonly AsyncLocal?> _requestCultureFallback = new(); - private readonly ConcurrentDictionary> _cultureOnlyDictionaries = new(StringComparer.OrdinalIgnoreCase); private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; @@ -76,24 +72,28 @@ namespace Emby.Server.Implementations.Localization } /// - /// Gets or sets the per-request culture fallback chain resolved from Accept-Language. - /// Each entry is a Jellyfin culture code (e.g. "de", "nl", "en-US") in priority order. + /// Gets the supported UI cultures. /// - public static IReadOnlyList? RequestCultureFallback + /// A list of objects covering every embedded translation. + public static IList GetSupportedUICultures() { - get => _requestCultureFallback.Value; - set => _requestCultureFallback.Value = value; - } + var cultures = new List(); + foreach (var option in _localizationOptions) + { + // Resource files use underscores for some variants (e.g. es_419); + // CultureInfo only accepts hyphenated BCP-47 codes. + var code = option.Value.Replace('_', '-'); + try + { + cultures.Add(CultureInfo.GetCultureInfo(code)); + } + catch (CultureNotFoundException) + { + // Skip novelty codes (e.g. "pr" Pirate, "jbo" Lojban) that .NET cannot resolve. + } + } - /// - /// Checks whether a translation resource file exists for the given culture code. - /// - /// The culture code to check (e.g. "de", "pt-BR", "es_419"). - /// true if an embedded translation resource exists for the culture. - public static bool HasTranslation(string culture) - { - var resourceName = CoreResourcePrefix + GetResourceFilename(culture); - return _assembly.GetManifestResourceInfo(resourceName) is not null; + return cultures; } private static void OnConfigurationUpdated(object? sender, EventArgs e) @@ -472,21 +472,6 @@ namespace Emby.Server.Implementations.Localization /// public string GetLocalizedString(string phrase) { - var fallback = _requestCultureFallback.Value; - if (fallback is not null) - { - foreach (var culture in fallback) - { - var dict = GetLocalizationDictionary(culture); - if (dict.TryGetValue(phrase, out var value)) - { - return value; - } - } - - return phrase; - } - return GetLocalizedString(phrase, CultureInfo.CurrentUICulture.Name); } @@ -510,7 +495,7 @@ namespace Emby.Server.Implementations.Localization } // Normalize BCP-47 hyphenated codes to Jellyfin's underscore-based codes - if (Bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) + if (_bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) { culture = mapped; } @@ -626,7 +611,7 @@ namespace Emby.Server.Implementations.Localization private static string GetDisplayName(string cultureCode) { // Handle Jellyfin-specific codes that aren't valid CultureInfo names - if (Bcp47ToJellyfinMap.Values.Contains(cultureCode)) + if (_bcp47ToJellyfinMap.Values.Contains(cultureCode)) { // Convert underscore to hyphen for CultureInfo lookup var normalized = cultureCode.Replace('_', '-'); diff --git a/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs b/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs deleted file mode 100644 index 57390ae005..0000000000 --- a/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; -using MediaBrowser.Controller.Configuration; -using Microsoft.AspNetCore.Http; - -namespace Jellyfin.Server.Middleware; - -/// -/// Middleware that resolves the Accept-Language request header -/// to an ordered list of Jellyfin-supported cultures, sets the fallback chain -/// on , and writes -/// the Content-Language response header. -/// -public class AcceptLanguageMiddleware -{ - private readonly RequestDelegate _next; - - /// - /// Initializes a new instance of the class. - /// - /// Next request delegate. - public AcceptLanguageMiddleware(RequestDelegate next) - { - _next = next; - } - - /// - /// Invoke request. - /// - /// Request context. - /// The server configuration manager. - /// Task. - public async Task Invoke(HttpContext context, IServerConfigurationManager configurationManager) - { - var chain = ResolveLanguages(context.Request, configurationManager); - if (chain is not null) - { - LocalizationManager.RequestCultureFallback = chain; - CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(chain[0]); - } - - context.Response.OnStarting( - static state => - { - var (ctx, languages) = ((HttpContext, IReadOnlyList?))state; - if (languages is not null) - { - ctx.Response.Headers.ContentLanguage = string.Join(", ", languages); - } - else - { - var culture = CultureInfo.CurrentUICulture.Name; - if (!string.IsNullOrEmpty(culture)) - { - ctx.Response.Headers.ContentLanguage = culture; - } - } - - return Task.CompletedTask; - }, - (context, chain)); - - try - { - await _next(context).ConfigureAwait(false); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - private static IReadOnlyList? ResolveLanguages(HttpRequest request, IServerConfigurationManager configurationManager) - { - var acceptLanguageHeader = request.GetTypedHeaders().AcceptLanguage; - if (acceptLanguageHeader is null || acceptLanguageHeader.Count == 0) - { - return null; - } - - var languages = acceptLanguageHeader - .OrderByDescending(h => h.Quality ?? 1) - .Select(h => h.Value.ToString()); - - var chain = new List(); - var seen = new HashSet(StringComparer.OrdinalIgnoreCase); - - foreach (var lang in languages) - { - TryAddCulture(lang, chain, seen); - } - - // Append server default culture if not already present - var serverCulture = configurationManager.Configuration.UICulture; - if (!string.IsNullOrEmpty(serverCulture)) - { - TryAddCulture(serverCulture, chain, seen); - } - - // Ensure en-US is always the final fallback - TryAddCulture("en-US", chain, seen); - - return chain; - } - - private static void TryAddCulture(string lang, List chain, HashSet seen) - { - // Direct match - if (LocalizationManager.HasTranslation(lang) && seen.Add(lang)) - { - chain.Add(lang); - return; - } - - // BCP-47 to Jellyfin underscore mapping (e.g. es-419 -> es_419) - if (LocalizationManager.Bcp47ToJellyfinMap.TryGetValue(lang, out var mapped) && seen.Add(mapped)) - { - chain.Add(mapped); - return; - } - - // Parent culture fallback (e.g. de-DE -> de) - var dashIndex = lang.IndexOf('-', StringComparison.Ordinal); - if (dashIndex > 0) - { - var parent = lang[..dashIndex]; - if (LocalizationManager.HasTranslation(parent) && seen.Add(parent)) - { - chain.Add(parent); - } - } - } -} diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index ea677cf6d9..f1c8b532aa 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -7,6 +7,7 @@ using System.Net.Http.Headers; using System.Net.Mime; using System.Text; using Emby.Server.Implementations.EntryPoints; +using Emby.Server.Implementations.Localization; using Jellyfin.Api.Middleware; using Jellyfin.Database.Implementations; using Jellyfin.LiveTv.Extensions; @@ -17,13 +18,13 @@ using Jellyfin.Networking.HappyEyeballs; using Jellyfin.Server.Extensions; using Jellyfin.Server.HealthChecks; using Jellyfin.Server.Implementations.Extensions; -using Jellyfin.Server.Middleware; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Extensions; using MediaBrowser.XbmcMetadata; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -137,6 +138,15 @@ namespace Jellyfin.Server CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(serverUICulture); + services.Configure(options => + { + var supportedUICultures = LocalizationManager.GetSupportedUICultures(); + options.SupportedCultures = supportedUICultures; + options.SupportedUICultures = supportedUICultures; + options.DefaultRequestCulture = new RequestCulture(serverUICulture); + options.ApplyCurrentCultureToResponseHeaders = true; + }); + services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); @@ -178,7 +188,7 @@ namespace Jellyfin.Server mainApp.UseCors(); - mainApp.UseMiddleware(); + mainApp.UseRequestLocalization(); if (config.RequireHttps && _serverApplicationHost.ListenWithHttps) { diff --git a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs index 1c4d0d4a8a..3b8fe5ca60 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs @@ -367,72 +367,7 @@ namespace Jellyfin.Server.Implementations.Tests.Localization } [Fact] - public void GetLocalizedString_FallbackChain_UsesFirstAvailableCulture() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - // Set fallback chain: de -> fr -> en-US - // "Artists" exists in de as "Interpreten", should use de (first in chain) - LocalizationManager.RequestCultureFallback = new[] { "de", "fr", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString("Artists"); - Assert.Equal("Interpreten", translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_FallbackChain_SkipsMissingAndUsesNext() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - // "zz" has no translation file so the key won't be found there, - // should fall through to de which has "Artists" as "Interpreten" - LocalizationManager.RequestCultureFallback = new[] { "zz", "de", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString("Artists"); - Assert.Equal("Interpreten", translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_FallbackChain_ReturnsKeyWhenNoTranslation() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - var key = "CompletelyNonExistentKey"; - LocalizationManager.RequestCultureFallback = new[] { "de", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString(key); - Assert.Equal(key, translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_NoFallbackChain_UsesCurrentUICulture() + public void GetLocalizedString_UsesCurrentUICulture() { var localizationManager = Setup(new ServerConfiguration { @@ -443,8 +378,6 @@ namespace Jellyfin.Server.Implementations.Tests.Localization try { CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("de"); - LocalizationManager.RequestCultureFallback = null; - var translated = localizationManager.GetLocalizedString("Artists"); Assert.Equal("Interpreten", translated); } @@ -454,16 +387,15 @@ namespace Jellyfin.Server.Implementations.Tests.Localization } } - [Theory] - [InlineData("de", true)] - [InlineData("en-US", true)] - [InlineData("fr", true)] - [InlineData("es_419", true)] - [InlineData("nonexistent", false)] - [InlineData("zz-ZZ", false)] - public void HasTranslation_ReturnsExpected(string culture, bool expected) + [Fact] + public void GetSupportedUICultures_IncludesCommonCultures() { - Assert.Equal(expected, LocalizationManager.HasTranslation(culture)); + var supported = LocalizationManager.GetSupportedUICultures(); + Assert.Contains(supported, c => c.Name.Equals("de", StringComparison.OrdinalIgnoreCase)); + Assert.Contains(supported, c => c.Name.Equals("en-US", StringComparison.OrdinalIgnoreCase)); + Assert.Contains(supported, c => c.Name.Equals("fr", StringComparison.OrdinalIgnoreCase)); + // Underscore variants get normalized to BCP-47 hyphen form for CultureInfo compatibility. + Assert.Contains(supported, c => c.Name.Equals("es-419", StringComparison.OrdinalIgnoreCase)); } private LocalizationManager Setup(ServerConfiguration config) -- cgit v1.2.3 From 93d7a1cf20442e4a82b2819f8a5bdbe4f6c0ae97 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 23:57:34 +0200 Subject: Cleanup --- .../HttpServer/WebSocketConnection.cs | 15 ++++----------- .../HttpServer/WebSocketManager.cs | 6 +----- 2 files changed, 5 insertions(+), 16 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index 17070c39ba..e9bf3b93a7 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -71,9 +71,9 @@ namespace Emby.Server.Implementations.HttpServer public IPAddress? RemoteEndPoint { get; } /// - /// Gets or initializes the UI culture name captured from the upgrade request. + /// Gets or initializes the UI culture captured from the upgrade request. /// - public string? RequestUICulture { get; init; } + public CultureInfo? RequestUICulture { get; init; } /// public Func? OnReceive { get; set; } @@ -90,19 +90,12 @@ namespace Emby.Server.Implementations.HttpServer /// public void ApplyRequestCulture() { - if (string.IsNullOrEmpty(RequestUICulture)) + if (RequestUICulture is null) { return; } - try - { - CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); - } - catch (CultureNotFoundException) - { - // Codes that aren't valid .NET cultures are ignored. - } + CultureInfo.CurrentUICulture = RequestUICulture; } /// diff --git a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs index dcdfda5472..072034c4bf 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs @@ -48,17 +48,13 @@ namespace Emby.Server.Implementations.HttpServer _logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress); WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false); - - // Capture the culture set by RequestLocalizationMiddleware so it can be - // restored both when processing incoming messages and when periodic - // listeners produce server-initiated payloads on background tasks. var connection = new WebSocketConnection( _loggerFactory.CreateLogger(), webSocket, authorizationInfo, context.GetNormalizedRemoteIP()) { - RequestUICulture = CultureInfo.CurrentUICulture.Name + RequestUICulture = CultureInfo.CurrentUICulture }; connection.OnReceive = result => { -- cgit v1.2.3 From 8f3eb3205d61d71638a1c695372cef273e76d2b3 Mon Sep 17 00:00:00 2001 From: Enea D'Angiò Date: Thu, 2 Jul 2026 19:36:48 +0200 Subject: Close sessions for lost WebSockets to prevent zombie SyncPlay groups (#17079) Close sessions for lost WebSockets to prevent zombie SyncPlay groups --- .../HttpServer/WebSocketConnection.cs | 6 +- .../Session/SessionWebSocketListener.cs | 15 ++- .../SyncPlayLostWebSocketTests.cs | 140 +++++++++++++++++++++ 3 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 tests/Jellyfin.Server.Integration.Tests/SyncPlayLostWebSocketTests.cs (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index e9bf3b93a7..dc7f972c13 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -127,8 +127,12 @@ namespace Emby.Server.Implementations.HttpServer { receiveResult = await _socket.ReceiveAsync(memory, cancellationToken).ConfigureAwait(false); } - catch (WebSocketException ex) + catch (Exception ex) when (ex is WebSocketException or ObjectDisposedException or OperationCanceledException) { + // ObjectDisposedException/OperationCanceledException: the socket was torn + // down underneath us (e.g. by the keep-alive watchdog after the connection + // was declared lost). Fall through so Closed is still raised and the + // session can release this connection. _logger.LogWarning("WS {IP} error receiving data: {Message}", RemoteEndPoint, ex.Message); break; } diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs index 6a26e92e14..2582ed9df0 100644 --- a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs +++ b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs @@ -246,8 +246,21 @@ namespace Emby.Server.Implementations.Session _logger.LogInformation("Lost {0} WebSockets.", lost.Count); foreach (var webSocket in lost) { - // TODO: handle session relative to the lost webSocket RemoveWebSocket(webSocket); + + // The connection stopped answering keep-alives, so a close frame will + // never arrive and the pending receive loop would hang forever, keeping + // the session (and e.g. its SyncPlay group membership) alive. Disposing + // the connection aborts the receive loop, which raises Closed and lets + // the session end normally. + try + { + webSocket.Dispose(); + } + catch (Exception exception) + { + _logger.LogWarning(exception, "Error disposing lost WebSocket from {RemoteEndPoint}.", webSocket.RemoteEndPoint); + } } } } diff --git a/tests/Jellyfin.Server.Integration.Tests/SyncPlayLostWebSocketTests.cs b/tests/Jellyfin.Server.Integration.Tests/SyncPlayLostWebSocketTests.cs new file mode 100644 index 0000000000..fa15b33af6 --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/SyncPlayLostWebSocketTests.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Json; +using System.Net.WebSockets; +using System.Reflection; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Emby.Server.Implementations.Session; +using Jellyfin.Api.Models.SyncPlayDtos; +using Jellyfin.Extensions.Json; +using MediaBrowser.Controller.Net; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Jellyfin.Server.Integration.Tests; + +public sealed class SyncPlayLostWebSocketTests : IClassFixture +{ + private readonly JellyfinApplicationFactory _factory; + + public SyncPlayLostWebSocketTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task LostWebSocket_EndsSession_And_RemovesEmptySyncPlayGroup() + { + var cancellationToken = TestContext.Current.CancellationToken; + var client = _factory.CreateClient(); + var accessToken = await AuthHelper.CompleteStartupAsync(client); + client.DefaultRequestHeaders.AddAuthHeader(accessToken); + + var wsClient = _factory.Server.CreateWebSocketClient(); + wsClient.ConfigureRequest = request => + request.Headers.Authorization = AuthHelper.DummyAuthHeader + $", Token={accessToken}"; + + var webSocket = await wsClient.ConnectAsync( + new UriBuilder(_factory.Server.BaseAddress) + { + Scheme = "ws", + Path = "websocket" + }.Uri, + cancellationToken); + + _ = DrainAsync(webSocket, cancellationToken); + + var watched = await WaitForWatchedWebSocketsAsync(TimeSpan.FromSeconds(10), cancellationToken); + var connection = Assert.Single(watched); + + using var createResponse = await client.PostAsync( + "SyncPlay/New", + JsonContent.Create(new NewGroupRequestDto { GroupName = "ZombieGroupRepro" }, options: JsonDefaults.Options), + cancellationToken); + Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode); + Assert.Equal(1, await WaitForGroupCountAsync(client, 1, TimeSpan.FromSeconds(10), cancellationToken)); + + connection.LastKeepAliveDate = DateTime.UtcNow - TimeSpan.FromSeconds(180); + + var groupCount = await WaitForGroupCountAsync(client, 0, TimeSpan.FromSeconds(45), cancellationToken); + Assert.True( + groupCount == 0, + $"SyncPlay group still listed {groupCount} group(s) after the WebSocket was lost: " + + "the keep-alive watchdog removed the socket from its watchlist without closing " + + "the session, leaving a zombie participant in the group (SessionWebSocketListener)."); + } + + private static async Task DrainAsync(WebSocket webSocket, CancellationToken cancellationToken) + { + var buffer = new byte[4096]; + try + { + while (webSocket.State == WebSocketState.Open) + { + await webSocket.ReceiveAsync(buffer, cancellationToken); + } + } + catch + { + // The server tears the connection down once the watchdog gives up on it. + } + } + + private async Task> WaitForWatchedWebSocketsAsync(TimeSpan timeout, CancellationToken cancellationToken) + { + var listener = _factory.Services.GetRequiredService>() + .OfType() + .Single(); + var watchlistField = typeof(SessionWebSocketListener) + .GetField("_webSockets", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(watchlistField); + var watchlist = (IEnumerable)watchlistField.GetValue(listener)!; + + var stopwatch = Stopwatch.StartNew(); + while (true) + { + try + { + var snapshot = watchlist.ToArray(); + if (snapshot.Length > 0 || stopwatch.Elapsed >= timeout) + { + return snapshot; + } + } + catch (InvalidOperationException) + { + // The watchdog mutated the set during enumeration; retry. + } + + await Task.Delay(100, cancellationToken); + } + } + + private static async Task WaitForGroupCountAsync(HttpClient client, int expected, TimeSpan timeout, CancellationToken cancellationToken) + { + var stopwatch = Stopwatch.StartNew(); + var count = -1; + while (stopwatch.Elapsed < timeout) + { + using var response = await client.GetAsync("SyncPlay/List", cancellationToken); + response.EnsureSuccessStatusCode(); + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken); + count = document.RootElement.GetArrayLength(); + if (count == expected) + { + return count; + } + + await Task.Delay(500, cancellationToken); + } + + return count; + } +} -- cgit v1.2.3 From 9266d45b5606731767359d44be0cabc4b65e5ada Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Tue, 15 Sep 2026 11:16:12 -0400 Subject: Backport pull request #17958 from jellyfin/release-12.z Don't let a torn-down WebSocket take down the request handler Original-merge: f4c76aabad7523d263a8a1f1a1232b27187e7584 Merged-by: crobibero Backported-by: Cody Robibero --- .../HttpServer/WebSocketConnection.cs | 31 ++++++-- .../Session/SessionWebSocketListener.cs | 4 +- .../Outbound/ForceKeepAliveMessage.cs | 4 +- .../HttpServer/WebSocketConnectionTests.cs | 90 ++++++++++++++++++++++ 4 files changed, 120 insertions(+), 9 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index dc7f972c13..d7319f80f4 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -127,7 +127,7 @@ namespace Emby.Server.Implementations.HttpServer { receiveResult = await _socket.ReceiveAsync(memory, cancellationToken).ConfigureAwait(false); } - catch (Exception ex) when (ex is WebSocketException or ObjectDisposedException or OperationCanceledException) + catch (Exception ex) when (IsConnectionGone(ex)) { // ObjectDisposedException/OperationCanceledException: the socket was torn // down underneath us (e.g. by the keep-alive watchdog after the connection @@ -158,7 +158,15 @@ namespace Emby.Server.Implementations.HttpServer if (receiveResult.EndOfMessage) { - await ProcessInternal(pipe.Reader).ConfigureAwait(false); + try + { + await ProcessInternal(pipe.Reader).ConfigureAwait(false); + } + catch (Exception ex) when (IsConnectionGone(ex)) + { + _logger.LogWarning("WS {IP} error sending data: {Message}", RemoteEndPoint, ex.Message); + break; + } } } while ((_socket.State == WebSocketState.Open || _socket.State == WebSocketState.Connecting) @@ -170,13 +178,24 @@ namespace Emby.Server.Implementations.HttpServer || _socket.State == WebSocketState.CloseReceived || _socket.State == WebSocketState.CloseSent) { - await _socket.CloseAsync( - WebSocketCloseStatus.NormalClosure, - string.Empty, - cancellationToken).ConfigureAwait(false); + try + { + await _socket.CloseAsync( + WebSocketCloseStatus.NormalClosure, + string.Empty, + cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (IsConnectionGone(ex)) + { + // The peer is already gone, there is nobody left to send the close frame to. + _logger.LogDebug("WS {IP} error closing connection: {Message}", RemoteEndPoint, ex.Message); + } } } + private static bool IsConnectionGone(Exception ex) + => ex is WebSocketException or ObjectDisposedException or OperationCanceledException; + private async Task ProcessInternal(PipeReader reader) { ReadResult result = await reader.ReadAsync().ConfigureAwait(false); diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs index e81edc82c6..2100c23c45 100644 --- a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs +++ b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs @@ -171,7 +171,7 @@ namespace Emby.Server.Implementations.Session { await SendForceKeepAlive(webSocket).ConfigureAwait(false); } - catch (WebSocketException exception) + catch (Exception exception) when (exception is WebSocketException or ObjectDisposedException or OperationCanceledException) { _logger.LogWarning(exception, "Cannot send ForceKeepAlive message to WebSocket {0}.", webSocket); } @@ -232,7 +232,7 @@ namespace Emby.Server.Implementations.Session { await SendForceKeepAlive(webSocket).ConfigureAwait(false); } - catch (WebSocketException exception) + catch (Exception exception) when (exception is WebSocketException or ObjectDisposedException or OperationCanceledException) { _logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket."); lost.Add(webSocket); diff --git a/MediaBrowser.Controller/Net/WebSocketMessages/Outbound/ForceKeepAliveMessage.cs b/MediaBrowser.Controller/Net/WebSocketMessages/Outbound/ForceKeepAliveMessage.cs index ca55340a05..50ed76f762 100644 --- a/MediaBrowser.Controller/Net/WebSocketMessages/Outbound/ForceKeepAliveMessage.cs +++ b/MediaBrowser.Controller/Net/WebSocketMessages/Outbound/ForceKeepAliveMessage.cs @@ -4,7 +4,9 @@ using MediaBrowser.Model.Session; namespace MediaBrowser.Controller.Net.WebSocketMessages.Outbound; /// -/// Force keep alive websocket messages. +/// Force keep alive websocket messages. The data is the timeout in seconds after which the +/// server considers the connection lost; clients are expected to answer with a KeepAlive +/// message and to keep sending one at least every half of that timeout. /// public class ForceKeepAliveMessage : OutboundWebSocketMessage { diff --git a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs index 22667ee82d..b9ae16255e 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs @@ -1,7 +1,11 @@ using System; using System.Buffers; using System.IO; +using System.Net.WebSockets; +using System.Text; using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; using Emby.Server.Implementations.HttpServer; using Microsoft.Extensions.Logging.Abstractions; using Xunit; @@ -48,6 +52,92 @@ namespace Jellyfin.Server.Implementations.Tests.HttpServer Assert.Throws(() => con.DeserializeWebSocketMessage(new ReadOnlySequence(bytes), out var bytesConsumed)); } + [Fact] + public async Task ReceiveAsync_SocketTornDownWhileAnswering_RaisesClosedWithoutThrowing() + { + // The keep-alive watchdog can dispose a connection while the receive loop is + // answering a message on it. The failing answer must not escape into the request + // handler, as that would skip the Closed event the session needs to release it. + var socket = new DisposedOnSendWebSocket(Encoding.UTF8.GetBytes("{\"MessageType\":\"KeepAlive\"}")); + var con = new WebSocketConnection(new NullLogger(), socket, null!, null!) + { + OnReceive = _ => Task.CompletedTask + }; + + var closed = false; + con.Closed += (_, _) => closed = true; + + await con.ReceiveAsync(TestContext.Current.CancellationToken); + + Assert.True(closed); + Assert.Equal(1, socket.SendAttempts); + } + + /// + /// A socket that hands out a single message and then behaves like a socket that was + /// disposed underneath the receive loop. + /// + internal sealed class DisposedOnSendWebSocket : WebSocket + { + private readonly byte[] _message; + private bool _received; + + public DisposedOnSendWebSocket(byte[] message) + { + _message = message; + } + + public int SendAttempts { get; private set; } + + public override WebSocketCloseStatus? CloseStatus => null; + + public override string? CloseStatusDescription => null; + + public override string? SubProtocol => null; + + public override WebSocketState State => SendAttempts == 0 ? WebSocketState.Open : WebSocketState.Closed; + + public override void Abort() + { + } + + public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) + => Task.CompletedTask; + + public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken) + => Task.CompletedTask; + + public override void Dispose() + { + } + + public override ValueTask ReceiveAsync(Memory buffer, CancellationToken cancellationToken) + { + ObjectDisposedException.ThrowIf(_received, this); + + _received = true; + _message.CopyTo(buffer); + return ValueTask.FromResult(new ValueWebSocketReceiveResult(_message.Length, WebSocketMessageType.Text, true)); + } + + public override Task ReceiveAsync(ArraySegment buffer, CancellationToken cancellationToken) + => throw new NotImplementedException(); + + public override ValueTask SendAsync(ReadOnlyMemory buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) + => throw FailSend(); + + public override Task SendAsync(ArraySegment buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) + => throw FailSend(); + + private WebSocketException FailSend() + { + SendAttempts++; + return new WebSocketException( + WebSocketError.InvalidState, + "The WebSocket is in an invalid state ('Closed') for this operation. Valid states are: 'Open, CloseReceived'"); + } + } + internal sealed class BufferSegment : ReadOnlySequenceSegment { public BufferSegment(Memory memory) -- cgit v1.2.3