Skip to content

Storage state fails to persist on container shutdown (race with context close) #7162

Description

@joyjit

Summary

The persistence plugin's shutdown/SIGTERM handler tries to save
storageState for each session on container shutdown, but by the
time that handler runs the browser context has already been torn
down, so the save always fails with:

{"level":"warn","msg":"failed to persist storage state","userId":"joyjit","storageStatePath":"/data/profiles/<id>/storage-state.json","error":"browserContext.storageState: Target page, context or browser has been closed"}

Current Behavior

Storage state is only flushed on two triggers today:

  1. Idle-tab reap (30 min)
  2. Explicit session destroy

On container shutdown/restart there's also a final-save attempt,
but the browser context is closed before that handler runs, so the
save is a guaranteed no-op that just logs a warning.

Seen 3 times in docker logs camofox-browser:

  • 2026-06-30 17:38 (userId: deploy-e2e)
  • 2026-06-30 17:54 (userId: health-monitor)
  • 2026-07-02 16:34 (userId: joyjit, during a docker restart camofox-browser)

In each case the prior idle-flush happened to be recent enough that
on-disk state was still current, so no data was actually lost. But
that's incidental — if a restart happens shortly after real session
activity (login, cookie refresh) and before the next 30-minute idle
flush, that state is silently dropped. Restarts happen for upgrades,
crashes, or OOM, not just idle periods.

Expected Behavior

On shutdown, storage state should be persisted before the browser
context is closed, so the shutdown-time save actually captures
current state instead of racing the teardown.

Suggested Fix

In the persistence plugin's shutdown/SIGTERM handler, reorder so the
flush happens first:

// current (approximate): context is closed, then save is attempted
await browserContext.close();
await persistStorageState(browserContext); // always fails, context is gone

// suggested: flush before teardown
await persistStorageState(browserContext);
await browserContext.close();

Environment

  • Repo: jo-inc/camofox-browser
  • Deployment: Docker, camofox-browser container on parkcircus
  • Observed via: docker logs camofox-browser, cross-referenced
    against a persistence runbook re-run on 2026-07-02

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions