browser-search icon indicating copy to clipboard operation
browser-search copied to clipboard

Camofox: heap OOM when the session is restored (unbounded storage state) and tabs leaked when a step times out

Open Johell1NS opened this issue 1 month ago • 1 comments

Problem 1: server crash with JavaScript heap out of memory

The Camofox image starts Node with --max-old-space-size=${MAX_OLD_SPACE_SIZE:-128}. With no open tabs the heap is already at 70-95 MB.

The persistence plugin saves the whole browser storage (cookies + localStorage of every visited site) to storage-state.json and never prunes it. On a long-lived profile the file was 7 MB (3186 cookies, 257 origins). The session is closed ~30 s after the last tab, and the next tab recreates it and parses that file several times. The crash happened exactly there:

restoring persisted storage state ... FATAL ERROR: Reached heap limit Allocation failed

After the restart the first tab took ~20 s.

Problem 2: tabs left open when a step times out

smart-extract.mjs runs camofox.mjs through execFile with a timeout. On expiry the child receives SIGTERM and dies immediately, so the finally blocks that close its tab never run. The tab stays open until the server reaper closes it (5 min). If the kill lands while POST /tabs is in flight, the server still creates the tab and the client never learns its id.

Changes

  • docker/setup.md and the Docker section of data/advanced-browsing-reference.md: add -e MAX_OLD_SPACE_SIZE=512 with an explanation.
  • camofox.mjs: automatic storage reset (DELETE /sessions/:userId/storage_state) every 7 days, only when no session is active, best-effort (never blocks a command). Tunable with CAMOFOX_STORAGE_MAX_AGE_DAYS (0 disables) and CAMOFOX_STORAGE_MARKER. The marker defaults to ~/.cache/browser-search/camofox-storage-reset.
  • camofox-client.mjs: new resetStorageState().
  • camofox.mjs: tabs created by the process are tracked; on SIGTERM/SIGINT or on an internal deadline (CAMOFOX_DEADLINE_MS) it waits for an in-flight creation, closes every tracked tab and exits with a JSON error. The internal deadline also works on Windows, where signals cannot be handled gracefully.
  • smart-extract.mjs: gives each Camofox child a deadline 3 s shorter than its kill timeout.
  • check.sh: reports the server heap usage from /health.

Verification

  • Deadline, SIGTERM and in-flight creation: activeTabs returns to 0 in all cases.
  • smart-extract --expr on a 25 s page: readable timeout errors, no tab left.
  • Storage reset: runs with a missing or expired marker, skipped with a recent marker, deferred while a session is active, disabled with CAMOFOX_STORAGE_MAX_AGE_DAYS=0.
  • Container recreated with MAX_OLD_SPACE_SIZE=512 and the same volume; regression checks on example.com, Wikipedia, x.com, YouTube, Hacker News, a 404 page, --expr and pdf give the same results.

Johell1NS avatar Oct 21 '26 10:10 Johell1NS

Released in v3.2.0.

Johell1NS avatar Oct 21 '26 12:10 Johell1NS