Skip to content

fix: resolve Jellyfin collection creation failure#2511

Merged
enoch85 merged 1 commit into
mainfrom
fix/jellyfin-collection-creation
Mar 24, 2026
Merged

fix: resolve Jellyfin collection creation failure#2511
enoch85 merged 1 commit into
mainfrom
fix/jellyfin-collection-creation

Conversation

@enoch85

@enoch85 enoch85 commented Mar 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2493 — Jellyfin collections fail to create with Failed to fetch created collection error.

Root cause

How Plex does it: Plex's POST /library/collections returns the full collection object directly in response.MediaContainer.Metadata[0]. No re-fetch needed.

How Jellyfin does it: Jellyfin's POST /Collections creates the collection synchronously (persists to DB via parentFolder.AddChild(collection) before returning), but only returns { Id: string }. The collection exists immediately in the database when the response comes back — confirmed by the Jellyfin server source code in CollectionManager.CreateCollectionAsync.

Why the re-fetch failed: The getCollection method used getItemsApi().getItems() — a query/search endpoint that goes through Jellyfin's item query pipeline with user-level filtering and caching. There was a known cache invalidation bug in Jellyfin 10.11.0-10.11.2 (jellyfin/jellyfin#15423), and even on later versions the query pipeline can fail to surface a just-created item.

Why the re-fetch was unnecessary: Both callers of createCollection only use mediaCollection.id. The Id from the creation response is guaranteed valid by Jellyfin's synchronous creation.

Changes

  • createCollection: Remove the unnecessary re-fetch. Construct MediaCollection directly from the creation response Id and input params.
  • getCollection: Switch from getItemsApi().getItems() (query pipeline) to getUserLibraryApi().getItem() (direct GetItemById — hits LRU cache first, then single DB lookup, no query pipeline). This is the correct API for fetching a single item by known ID.

Test plan

  • Configure Maintainerr with Jellyfin and create a rule
  • Verify collection is created successfully in Jellyfin
  • Verify collection shows media count in Maintainerr (no longer 0)
  • Verify existing collection operations (get, delete, update) still work

🤖 Generated with Claude Code

@enoch85 enoch85 requested a review from ydkmlt84 as a code owner March 21, 2026 00:44
enoch85 added a commit that referenced this pull request Mar 21, 2026
PR #2511 - fix: resolve Jellyfin collection creation failure
- Remove unnecessary re-fetch after creating a Jellyfin collection
- Switch getCollection to use direct getUserLibraryApi().getItem() lookup instead of query-based getItemsApi().getItems() pipeline
- Construct MediaCollection from known creation response data instead of re-querying
@enoch85 enoch85 self-assigned this Mar 21, 2026
@enoch85

enoch85 commented Mar 21, 2026

Copy link
Copy Markdown
Collaborator Author

Last commit keeps the default stale-link healing in place, but avoids re-checking a collection we already reconciled earlier in the same rule run. That’s the piece that was causing Jellyfin to trip over a freshly created collection ID and restart the recreate/resync loop in #2509

enoch85 added a commit that referenced this pull request Mar 21, 2026
PR #2511 - fix: resolve Jellyfin collection creation failure
- avoid revalidating freshly reconciled collection links in the same rule run
- preserve stale-link healing for normal collection add/remove paths
- add focused rule executor coverage for Jellyfin resync and removal flows
Remove unnecessary re-fetch after creating a Jellyfin collection.
Switch getCollection to use direct getUserLibraryApi().getItem() lookup
instead of the query-based getItemsApi().getItems() pipeline.
Avoid revalidating reconciled collection links within the same rule run.
@enoch85 enoch85 force-pushed the fix/jellyfin-collection-creation branch from f5ad63a to 6ca0b76 Compare March 24, 2026 21:39
@enoch85 enoch85 merged commit 7d5db09 into main Mar 24, 2026
12 checks passed
@enoch85 enoch85 deleted the fix/jellyfin-collection-creation branch March 24, 2026 21:47
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.2.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

enoch85 added a commit that referenced this pull request Mar 28, 2026
…ellyfin

Fixes a regression introduced in v3.2.0 where rule execution would crash
with "Cannot read properties of undefined (reading 'id')" for Jellyfin users.

The COLLECTION_CREATION_WITH_ITEMS optimisation (PR #2494, fixing #2489) attempted
to create a Jellyfin collection with all existing item IDs in a single atomic API
call. If Jellyfin rejected any ID (e.g. stale/unavailable media), the entire
createCollection call threw, addToCollectionInternal returned undefined, and the
caller in rule-executor then accessed collection.id on that undefined — crashing
the rule run for every affected collection.

The original lag problem that motivated PR #2494 was independently solved by
PR #2511 (issue #2493), so the optimisation no longer provides any benefit.

Fix:
- Remove itemIds from all createCollection call sites; collections are always
  created empty and items are added via the existing addBatchToCollection path
- Remove the COLLECTION_CREATION_WITH_ITEMS feature flag (enum, constants,
  CreateCollectionParams.itemIds) as it is now unused
- Add null guards in addToCollectionWithResolvedLink and
  removeFromCollectionWithResolvedLink to prevent crashes if collection creation
  fails for any other reason
- Add collection && guard in rule-executor before the remove block for the same
  defensive reason
- Update tests to reflect the new invariant: createCollection never receives
  itemIds regardless of media server type

Removed implementation introduced by: PR #2494
PR #2494 fixed: issue #2489
Related later Jellyfin collection creation fix: PR #2511 for issue #2493
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maintainerr can't create jellyfin collections

1 participant