[GH-ISSUE #5612] Boot-time gallery recovery loads every chat with images into memory #3110

Closed
opened 2026-10-21 05:59:41 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Aug 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5612

Originally assigned to: @kolacheee on GitHub.

What happens

recoverGalleryImages (packages/server/src/services/image/gallery-recovery.ts:19-60, called at boot from app.ts:23) walks every chat directory under data/gallery/ and runs a chat_images query scoped to that chat. Each query is perfectly scoped — but loading any row for a chat pulls that chat's entire storage unit into memory (all 17 lazy tables' shards for it). So on an install where most chats have generated images, boot loads nearly every chat unit back into memory, quietly reproducing the eager-boot behavior #5592 removed. Eviction then has to claw it all back down afterward (and can't, for anything pinned or above the cap during the scan).

This never shows up in a query audit because no individual query is wrong — the problem is that the scan visits every chat.

Try it yourself

On an install with images in many chats, set MARINARA_MAX_RESIDENT_CHATS=8 and watch memory during startup: it climbs to roughly the old eager-boot level while the gallery scan runs, before eviction trims it back.

Possible directions (whichever fits best)

  • Defer the recovery per chat: run it the first time a chat's unit loads, instead of for every chat at boot.
  • Drive it off the filesystem only: the scan needs the set of known filePaths per directory, which could come from a small on-disk index instead of loading chat units.
  • Batch it through the existing eviction machinery: scan one chat, evict, next — so boot memory stays bounded even while the scan is table-complete.

Found during the residual-lease audit for #5611 / #5592.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5612 Originally assigned to: @kolacheee on GitHub. **What happens** `recoverGalleryImages` (`packages/server/src/services/image/gallery-recovery.ts:19-60`, called at boot from `app.ts:23`) walks every chat directory under `data/gallery/` and runs a `chat_images` query scoped to that chat. Each query is perfectly scoped — but loading any row for a chat pulls that chat's **entire storage unit** into memory (all 17 lazy tables' shards for it). So on an install where most chats have generated images, boot loads nearly every chat unit back into memory, quietly reproducing the eager-boot behavior #5592 removed. Eviction then has to claw it all back down afterward (and can't, for anything pinned or above the cap during the scan). This never shows up in a query audit because no individual query is wrong — the problem is that the scan visits every chat. **Try it yourself** On an install with images in many chats, set `MARINARA_MAX_RESIDENT_CHATS=8` and watch memory during startup: it climbs to roughly the old eager-boot level while the gallery scan runs, before eviction trims it back. **Possible directions** (whichever fits best) - Defer the recovery per chat: run it the first time a chat's unit loads, instead of for every chat at boot. - Drive it off the filesystem only: the scan needs the set of known `filePath`s per directory, which could come from a small on-disk index instead of loading chat units. - Batch it through the existing eviction machinery: scan one chat, evict, next — so boot memory stays bounded even while the scan is table-complete. Found during the residual-lease audit for #5611 / #5592.
bsakaguchi 2026-10-21 05:59:41 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 29, 2026):

Taking this one (kolacheee, agent-assisted) — PR incoming.

<!-- gh-comment-id:5459126635 --> @kolacheee commented on GitHub (Aug 29, 2026): Taking this one (kolacheee, agent-assisted) — PR incoming.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3110
No description provided.