Chrome DevTools Protocol Browser.newContext with userDataDir: Persistence Across Unclean Shutdowns?
0 reputation · 16 Apr 2020, 18:21 UTC
0 reputation · 16 Apr 2020, 18:21 UTC
Goal: Establish a repeatable development environment by launching Chrome through the DevTools Protocol's Browser.newContext method with a specified userDataDir so that cookies, localStorage, and extension state persist across restarts.
Uncertainty: Chrome’s internal cleanup routines may delete or modify files inside the userDataDir when it detects an unclean shutdown, profile corruption, or applies cache policies, and this behavior is not documented or exposed via a CDP endpoint, leaving no way to verify that the profile remained unchanged after a restart.
userDataDir remains unchanged after an unclean process termination?Chrome does not guarantee that the userDataDir remains unchanged after an unclean process termination. When Chrome is killed unexpectedly (e.g., via kill -9 or a crash), it detects the improper shutdown on the next launch and triggers recovery logic that may prune, reset, or modify specific profile files to keep the browser stable.
While Browser.newContext creates an isolated session, the userDataDir passed at browser launch is the shared persistent store underneath it. If the process dies uncleanly, the SingletonLock file is left behind. On the next start, Chrome sees that lock, infers a crash, and runs its cleanup and recovery routines before serving pages.
Following a crash, Chrome commonly targets these areas (exact behavior varies by version and crash type):
There is no CDP endpoint that reports profile corruption or cleanup. Your options are filesystem-level:
SingletonLock in the profile directory indicates the previous run did not shut down cleanly.Cache, GPUCache, and Code Cache changing on startup indicate pruning occurred.--enable-logging --log-level=1 (or --v=1) surfaces profile recovery messages in chrome_debug.log.userDataDir at browser launch so all contexts share one stable profile root.SingletonLock so Chrome does not enter recovery mode unnecessarily.Cookies and Local Storage databases before and after your test run to detect unexpected modification.Browser.close over killing the process so Chrome flushes in-memory state to disk.Assumes a recent Chromium-based browser (Chrome, Edge, Brave). Pruning behavior is non-deterministic and version-dependent; verify against your pinned Chrome build.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.