Skip to content

fix: a book removed from the library frees its request - #471

Merged
kikootwo merged 4 commits into
mainfrom
fix/214-removed-from-library
Oct 27, 2026
Merged

kikootwo merged 4 commits into
mainfrom
fix/214-removed-from-library

Conversation

@kikootwo

@kikootwo kikootwo commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #214

What changed

  • New src/lib/processors/library-removal.ts resetRequestsAfterLibraryRemoval(), called from both places in the library scan (scan-plex.processor.ts) that unlink a book whose library item is gone: the stale library record cleanup and the orphaned audiobook cleanup. Those were the only two sites that moved available back to downloaded. The book's available requests now become:
    • failed with errorMessage "Removed from library. Request it again to download it." (REMOVED_FROM_LIBRARY_MESSAGE, src/lib/constants/request-statuses.ts) when no audio file is left in the organized folder (Audiobook.filePath), the folder no longer exists, or no folder is on record. Only cover.jpg, ebooks or other non-audio files left counts as no audio. The check reads the book's own folder and one level of child folders (disc folders), no deeper, using the audio extension list from src/lib/utils/files-hash.ts (now exported as isAudioFilename()). The constant's doc comment says not to reword it: rows are matched by equality.
    • downloaded, as before, while audio is still there. A folder that can't be read or listed (permission or I/O error, not "not found") counts as still there.
  • Automatic requests: createRequestForUser with automatic: true (shelf sync, watched authors and series) now returns removed_from_library and creates nothing while any user's request for the book is failed with that message (removedFromLibrarySkip() in src/lib/services/auto-rerequest.ts). This runs before the Shelf sync re-creates failed requests every 6 hours when nothing was blocked #409 backoff, so neither "a release was blocked" nor a due retry brings the book back.
  • Docs: documentation/backend/services/scheduler.md (plex_library_scan, Fixed Issues line), documentation/admin-features/release-blocklist.md (automatic re-request table row), documentation/TABLEOFCONTENTS.md.

Why

When a delivered book was deleted from Audiobookshelf or Plex, the next full scan set its request back to downloaded ("files may still be there") and nothing ever moved it on. The book showed Processing forever, and downloaded blocks every new request for it, from its owner and from anyone else. The only way out was deleting the request as an admin.

With the audio gone there is nothing left for a scan to match, so the request fails with a reason the user can read, and it can be requested again. When the audio is still there, the next scan can match it once the library picks them up again, so that case keeps today's behavior.

A user who deletes a book usually meant to. Shelf sync and watched lists would otherwise pick a failed request up again (the owner's after the backoff, another user's straight away, since nothing blocks them anymore), so they skip the book until someone requests it by hand.

Other automatic paths checked, none of which can search or download for this request: retry_missing_torrents and monitor_rss_feeds only take awaiting_search/awaiting_release; retry_failed_imports only takes awaiting_import; find_missing_ebooks only takes downloaded/available parents, so it also stops fetching ebooks for the removed book. The scan's own matching step still takes failed requests, so a book that comes back to the library becomes available again with no download.

Evidence

  • New tests/processors/scan-plex.library-removal.test.ts runs the real scan with real temp folders: folder gone at the stale record site and at the orphan site becomes failed with the exact message; no folder on record becomes failed; a folder left with only cover.jpg, only an ebook and its cover, or audio two levels down becomes failed; an m4b in the folder at either site stays downloaded with no error; audio in disc folders stays downloaded; fs.stat or fs.readdir rejecting with EACCES stays downloaded; a book still in the library is untouched; only available requests move; nothing is searched, downloaded or notified.
  • New tests/services/request-creator-removed-from-library.test.ts runs the real createRequestForUser against a stand-in requests table: the owner's manual request replaces the failed request and queues a search; another user's manual request is created and searched; an automatic caller for the owner or another user returns removed_from_library with nothing created, searched or notified, also when a release was blocked during that attempt.
  • tests/processors/scan-plex.processor.test.ts: the existing stale and orphan reset test now gives its books a temp folder holding an m4b, so it still checks the downloaded path.
  • With main's src (08dc816) and this branch's 3 test files: 12 failed, 14 passed, all on an assertion (the scan writes downloaded instead of failed; the automatic caller gets success: true; readdir is never called). On this branch: 26/26 pass.
  • With the previous PR head's src (513a971, folder existence only): 11 failed, 15 passed, all on an assertion. Folders left with only cover.jpg, only an ebook, or audio too deep stay downloaded instead of failed; the old message text no longer matches; readdir is never called. The m4b and disc folder cases pass there too, as they should.

Checks

  • Unit tests for the changed code (vitest related on the 7 changed src files plus the 3 test files): PASS, 97 files, 1234/1234
  • Type check and lint: PASS (tsc --noEmit clean; ESLint reports nothing new in the 10 touched code files compared with main)
  • No image build or E2E run: the change is in src/lib only
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Newly reachable: failed for a request whose book left the library while its folder holds no audio, is missing, or is not on record. Audio kept more than one folder level below the book folder is not seen, so such a book fails; RMAB itself writes audio straight into the book folder. A request that was matched to a book the user added to the library outside RMAB has no folder on record, so it now fails instead of sitting in downloaded.
  • A media mount that is down during a scan while the library also reports the items gone would fail those requests. The next scan that sees the books again matches the failed requests and makes them available, with no download.
  • Requests already stuck in downloaded before this release are not repaired. Their library item is already gone, so neither site sees them again. To clear one: delete the request in the admin UI, then request the book again.
  • Automatic callers skip any book with a "Removed from library" request, for every user, until a manual request replaces or outlives it. Manual requests, interactive search and admin retries are unchanged.
  • Rollback: revert this PR. Requests already failed stay failed and can still be requested by hand; shelf sync and watched lists would then pick them up again on the Shelf sync re-creates failed requests every 6 hours when nothing was blocked #409 backoff.

Release note

  • A book deleted from Audiobookshelf or Plex no longer leaves its request stuck in Processing: once no audio is left in its folder it shows as failed ("Removed from library") and anyone can request it again. Shelf sync and watched lists don't bring it back on their own. Requests already stuck before this release are not repaired: delete them in the admin UI and request the book again (Feature Request: Gracefully handle audiobooks that have been manually deleted #214).

🤖 Generated with Claude Code

kikootwo and others added 4 commits October 27, 2026 03:09
…ck in Processing (#214)

The library scan moved an available request back to downloaded whenever the
book's library item disappeared, and nothing moved it on. Both reset sites
(stale library record, orphaned audiobook link) now make it failed with
"Removed from library" when the organized folder is gone too, or none is on
record, so it can be requested again; it stays downloaded while the folder is
still there. Shelf sync and watched lists never request such a book again,
for any user; a manual request does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…library

# Conflicts:
#	src/lib/constants/request-statuses.ts
The library scan now fails a removed book's request when its organized
folder holds no audio (only cover.jpg, ebooks or other files), not only
when the folder itself is gone. Audio directly in the folder or in a
disc folder one level down keeps it downloaded; an unreadable folder
still counts as present. The message no longer claims RMAB had files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review October 27, 2026 07:48
@kikootwo
kikootwo merged commit ea0b4cd into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/214-removed-from-library branch October 27, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Gracefully handle audiobooks that have been manually deleted

1 participant