jellyfin/Emby.Server.Implementations
Paolo Antinori 5d580abb08 fix: avoid NRE when sorting by user-dependent keys without a user
A query sorted by a user-dependent key (PlayCount, IsFavoriteOrLiked,
DatePlayed, IsPlayed, IsUnplayed) but carrying no User caused a
NullReferenceException inside UserDataManager.GetUserData, surfacing as
"Failed to compare two elements in the array" (InvalidOperationException
wrapping the NRE from the LINQ sort) and 500-ing the /Items request.

Root cause: LibraryManager.GetComparer assigned comparer.User = user
without a null guard, so PlayCountComparer.GetValue called
UserDataManager.GetUserData(null, item), dereferencing user.Id.

Two-part fix:
- LibraryManager.GetComparer: when user is null and the sort key requires a
  user (IUserBaseItemComparer), substitute the SortName comparer so the
  result stays deterministic instead of 500-ing. SortName is the project's
  canonical tiebreaker (ItemsController injects it for album-by-artist).
- UserDataManager.GetUserData: ArgumentNullException.ThrowIfNull(user) as
  defense in depth (matches the existing guards on the SaveUserData
  overloads in the same file). On master this overload was rewritten to use
  ResolveUserDataRow, so the NRE dereferences user.Id rather than
  user.InternalId as on the release branch — same bug, different line.

Also fixes DateLastMediaAddedComparer being statically mis-tagged as
IUserBaseItemComparer: its GetDate is static and never reads User, so it
does not need one. Without this, the SortName fallback above would wrongly
engage for DateLastContentAdded on anonymous queries (returning SortName
order instead of date order). Re-tagged to IBaseItemComparer and dropped the
unused User/UserManager/UserDataManager properties.

Tests:
- UserDataManagerTests.GetUserData_NullUser_ThrowsArgumentNullException:
  reproduces the crash (NRE -> now ArgumentNullException). Added to master's
  existing UserDataManagerTests.
- LibraryManagerSortTests.Sort_UserDependentKey_NullUser_FallsBackToSortNameWithoutThrowing:
  Sort with a user-dependent key + null user no longer throws and returns
  items ordered by the SortName fallback (direction preserved).
- LibraryManagerSortTests.Sort_DateLastContentAdded_NullUser_OrdersByDateNotSortName:
  guards that DateLastContentAdded still sorts by date with no user (fixture
  chosen so date-desc and SortName-desc disagree, so a revert is caught).

Full Jellyfin.Server.Implementations.Tests suite: 642 passed, 0 failed.

Fixes #17393
2026-07-22 07:43:58 +02:00
..
AppBase Run tree-wide dotnet format 2026-05-21 20:48:41 +02:00
Branding
Chapters Run tree-wide dotnet format 2026-05-21 20:48:41 +02:00
Collections Add a collection API for Included In feature (#15516) 2026-05-29 20:00:34 +02:00
Configuration
Cryptography
Data
Devices Add XML docs to DeviceId and remove CS1591 suppression 2026-07-15 11:58:29 +02:00
Dto Cleanup PreferEpisodeParentPoster) 2026-07-09 12:07:13 +02:00
EntryPoints
HttpServer Close sessions for lost WebSockets to prevent zombie SyncPlay groups (#17079) 2026-07-02 19:36:48 +02:00
Images Restore music collection image override 2026-07-07 10:24:18 -04:00
IO Fix ghost entries when deleting library paths 2026-07-03 14:12:56 -04:00
Library fix: avoid NRE when sorting by user-dependent keys without a user 2026-07-22 07:43:58 +02:00
Localization Translated using Weblate (Icelandic) 2026-07-21 00:52:58 +00:00
Playlists
Plugins Fix integrated provider images 2026-05-15 18:42:11 +02:00
Properties
QuickConnect
ScheduledTasks Fix potential garbled text in FFmpeg logs on Windows 2026-07-10 14:46:27 +08:00
Serialization
Session Merge pull request #17044 from Shadowghost/version-model-and-handling 2026-07-05 16:21:02 -04:00
Sorting fix: avoid NRE when sorting by user-dependent keys without a user 2026-07-22 07:43:58 +02:00
SyncPlay
TV Fix review comments 2026-07-02 08:49:11 +02:00
Updates Address PR comment 2026-06-06 21:55:30 -07:00
ApplicationHost.cs Fix local plugin registration 2026-06-26 11:42:28 +02:00
ConfigurationOptions.cs
Emby.Server.Implementations.csproj
IStartupOptions.cs
ServerApplicationPaths.cs
SystemManager.cs Run tree-wide dotnet format 2026-05-21 20:48:41 +02:00