Skip to content

fix(persistence): force-save cookie state on graceful shutdown#610

Merged
shaun0927 merged 1 commit intodevelopfrom
fix/cookie-snapshot-on-shutdown
Apr 9, 2026
Merged

fix(persistence): force-save cookie state on graceful shutdown#610
shaun0927 merged 1 commit intodevelopfrom
fix/cookie-snapshot-on-shutdown

Conversation

@shaun0927
Copy link
Copy Markdown
Owner

Summary

  • Add saveAllStorageState() method to SessionManager that iterates all active sessions and saves their storage state via CDP Network.getAllCookies
  • Wire it into the enhancedShutdown handler in src/index.ts with a 5-second timeout to prevent hanging shutdown
  • Ensures cookies are persisted before process exit on SIGTERM/SIGINT

Problem

The StorageStateManager watchdog timer uses .unref(), meaning it does not prevent Node.js process exit. If the MCP server is killed (SIGTERM/SIGINT), the final periodic save may not have completed, causing cookie/session loss across restarts.

This is the compounding factor from #606 — even when the persistent profile correctly stores cookies, the storage state backup (which serves as a safety net for headless-shell and cross-session restoration) was not guaranteed to be written on exit.

Changes

File Change
src/session-manager.ts Added saveAllStorageState() — iterates all sessions with active StorageStateManager, finds a page per session, calls manager.save()
src/index.ts Call saveAllStorageState() in enhancedShutdown before stopping stateManager, with 5s timeout

Test plan

  • npm run build — clean (zero TypeScript errors)
  • npm test — 170 suites passed, 3191 tests pass
  • Manual: launch with --auto-launch, authenticate to a site, send SIGTERM, verify storage state JSON exists in ~/.openchrome/storage-state/
  • Manual: restart after SIGTERM, verify cookies are restored on first target creation

Closes #606 (compounding factor)

🤖 Generated with Claude Code

… cookies

The StorageStateManager watchdog timer uses .unref(), so it does not
prevent process exit. If the MCP server exits via SIGTERM/SIGINT, the
final cookie save may never execute. Add saveAllStorageState() to
SessionManager and call it during graceful shutdown with a 5-second
timeout to ensure cookies are persisted before exit.

Closes #606 (compounding factor)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@shaun0927 shaun0927 merged commit 6e1a57c into develop Apr 9, 2026
9 checks passed
@shaun0927 shaun0927 deleted the fix/cookie-snapshot-on-shutdown branch April 9, 2026 01:52
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.

1 participant