Skip to content

fix(memory): bound pet marketplace asset cache - #1189

Open
kogekiplay wants to merge 2 commits into
spacering-net:mainfrom
kogekiplay:fix/bounded-pet-marketplace-assets
Open

kogekiplay wants to merge 2 commits into
spacering-net:mainfrom
kogekiplay:fix/bounded-pet-marketplace-assets

Conversation

@kogekiplay

Copy link
Copy Markdown
Contributor

Browsing pet marketplace pages kept every successful poster and preview's Base64 payload in a page-wide map. Closing a card or the dialog revoked its blob URL, but the fetched bytes remained reachable for the lifetime of the WebView.

Keep up to 64 recently used assets and 16 MiB of estimated string storage. Pending requests still share one fetch per URL; oversized previews still display for current consumers and are not cached after the fetch settles. Each consumer keeps ownership of its own blob URL.

The three new regressions fail on the previous implementation and pass with the fix. The pet suites, full frontend suite on Node 24 (13,125 tests), lint, and static export passed.

@kogekiplay

Copy link
Copy Markdown
Contributor Author

Could a maintainer rerun the unsuccessful jobs in this CI run? The Ubuntu desktop runner received a shutdown signal during cargo test; the macOS CLI job was cancelled before any steps ran. The other eight jobs passed, including macOS desktop. GitHub requires repository admin rights to rerun these jobs, so I could not start the rerun from the fork.

The bounded cache lengthens loadAsset's promise chain, so two microtask
yields no longer reach the hook's late callback and the test passed even
without the cancellation guard. Resolve inside an awaited act instead.
@xintaofei

Copy link
Copy Markdown
Collaborator

Verdict: the approach holds, and with one test fix on top this is ready.

The problem is real. src/lib/pet/use-proxied-marketplace-asset.ts kept every fetched poster/preview Base64 payload in a module-level Map for the life of the WebView. Replacing it with a bounded LRU (64 entries, i.e. two pages of PAGE_SIZE = 30 plus a preview, and a 16 MiB estimated budget) is the right size of fix. Keeping in-flight deduplication in a separate pendingAssets map is also right. The pages still get the reason the cache exists in the first place, fast paging back and reopening against a slow CDN, and memory stays bounded.

Review findings:

  • Checked and correct: the LRU bookkeeping (touch on hit, single byte charge per insert, eviction until both limits are met), the identity guards on the pending request, retry after rejection (including after unmount), and StrictMode double effects sharing one fetch while each consumer mints and revokes its own blob URL.
  • Byte accounting charges 2 bytes per code unit. That overestimates Base64 strings, which engines store one byte per character, so the effective budget is conservative. Not blocking.
  • Test coverage regression, fixed: the longer loadAsset chain (.then + .finally) meant the existing test "does not mint a blob when unmounted before the fetch settles" (src/lib/pet/use-proxied-marketplace-asset.test.ts:181) no longer reached the hook's late callback with its two await Promise.resolve() yields. It passed even with the if (cancelled) return guard removed. I changed it to resolve inside await act(...). With the guard deleted, the test now fails.

Verification:

  • The three new regression tests fail on the previous implementation and pass on this branch.
  • Full frontend suite: 846 files, 13,125 tests passing.
  • pnpm lint . and pnpm build (static export) pass.
  • An independent second review approved both the original change and the test fix.

The changes were pushed to this pull request (1 file, +2/-3).


🤖 Generated with Codeg

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.

2 participants