·6 min read
Why my browser signed everyone out: the host exited first
Sometime before 02:34 on Sep 20, 2026, I had a bug that is easy to describe and annoying to see: you close the browser, you open it again, and you are signed out of everything. The cause was not in the cookie code at all. It was in the order in which two processes exited, and my process was the impatient one.
- Project
- Light Browser
- Date
- Sep 2026
- Stack
- C++, Win32, WebView2
- Commit
- 8b4cee4
What broke
Light Browser is a Win32 window that hosts WebView2, which means the actual browser, with its networking, its cookie jar and its renderers, is a separate set of Chromium processes. The shell does not own the cookies. It asks the engine to run.
The symptom was the one in the opening: after a normal close and reopen, sessions were gone. The commit message states the cause in two sentences: "Chromium writes its cookie store during engine shutdown; the host used to exit first." The comment I wrote above the new function goes further and describes what that costs. Leaving before the shutdown finishes, or starting a second engine on the same profile folder while the old one is still writing, "loses or corrupts those files - which signs the user out of every site at once".
Those files are the cookie database, the site storage, and the os_crypt key. The key is on that list because stored values are encrypted with it. My reading is that a key that is lost or half-written makes the rest unreadable too, though I did not test that case.
I cannot give you a count of users who hit it, and I did not record a reproduction rate. What the history gives me is the mechanism and the fix.
What the code showed
The old close path was short. WM_CLOSE destroyed the window, and WM_DESTROY saved the visit history and session, released the download hosts, closed each tab's controller, cleared the tab list and posted WM_QUIT:
case WM_DESTROY: FlushVisits(); SaveSession();
...
for (auto& t : tabs_) { t->closed = true; if (t->controller) t->controller->Close(); }
for (auto& d : downloads_) ReleaseDownloadHost(d);
tabs_.clear();
PostQuitMessage(0);
return 0;controller->Close() tells WebView2 to close that view. It does not wait for the browser process behind it to finish writing to disk. So the shell asked the engine to close, and then, a few lines later, exited, with the engine process possibly still mid-write. On a normal machine the engine often finished anyway. Sometimes it did not.
I had also written something that was nearly the right answer without knowing it. A feature called deep sleep tears the engine down on purpose after the window has been minimized for a while, to drop memory from hundreds of megabytes to a roughly 35 MB shell. Its teardown code, inline inside DeepSleep, closed the controllers, released the environment and stopped. That was fine for a sleeping browser that would later wake, but it was the same race: the engine process could still be writing when a new one started on the same profile.
There were two more paths in the same shape. Windows shutdown and sign-out end a process with WM_ENDSESSION, not WM_DESTROY. The updater's Restart Manager pass does the same. In both, my shell was gone before the profile was settled. And when the engine itself crashed, EngineCrashed rebuilt it, but a dead browser process had to be fully gone before a new one could safely open the same profile folder.
All four were one bug: I knew when I was done, and I never asked whether the engine was.
The fix
8b4cee4 (Sep 20, 2026) pulled the teardown into one function, TeardownEngine(DWORD waitMs). It closes every WebView, resets the environment so every Chromium process exits, then opens the engine's browser process by its id and waits for it:
void TeardownEngine(DWORD waitMs) {
DWORD pid = browserPid_; browserPid_ = 0;
for (auto& tab : tabs_) { ... controller->Close(); ... }
net_.SetEnvironment(nullptr);
env_.Reset();
if (!pid || !waitMs) return;
if (HANDLE h = OpenProcess(SYNCHRONIZE, FALSE, pid)) {
WaitForSingleObject(h, waitMs); CloseHandle(h);
}
}The process id comes from get_BrowserProcessId, which ConfigureWebView now stores as browserPid_ when a view is created. Waiting on a process handle with SYNCHRONIZE access is a plain Win32 way to ask "has that process actually exited yet", and the wait is bounded, so a stuck engine cannot hang the shell forever.
Then every path that ends an engine goes through it, with a timeout that fits:
- 01
Window destroyed
WM_DESTROY waits up to 4000 ms
- 02
Windows sign-out or updater restart
WM_ENDSESSION waits up to 2500 ms
- 03
Deep sleep
DeepSleep waits up to 4000 ms
- 04
Engine crash
EngineCrashed waits up to 3000 ms
WM_ENDSESSION also got its own handler, which saves visits and session, releases the downloads and tears the engine down, because that message is how the process actually ends during a shutdown or an updater restart.
Two smaller changes rode along in the same commit. Clearing all site data now asks for confirmation first, with a warning that you will be signed out of every site and that it cannot be undone, a small fix in the same spirit. And the in-page media ad script now respects the per-site ad switch, because it used to read it only once: the commit message says the switch "never reached the media ad script".
What I would do differently
I would have treated the shell's exit as a distributed shutdown from the start. When your app embeds another process, "I am done" is not the same as "my data is safe", and the order of exits is part of the design. I wrote four paths that ended an engine and they each did it slightly differently, which is how the bug could hide behind a working main path.
I would also have written a test for it, and I did not. The commit adds no test, and I cannot reproduce a race on demand, so what I have is a fix based on the documented behavior of the engine plus a bounded wait, not a proof. If I did it again, I would add a smoke test that opens a site, logs in, closes the browser, and checks that the cookie is still there on the next start.
The timeouts, 2500 to 4000 ms, are guesses I picked to be longer than a normal shutdown and shorter than a user's patience. I have not measured a typical shutdown time.
Where this fits
This is the kind of bug a thin shell on a system engine gives you: the engine does the hard work, and you inherit its rules about when it is finished. The rest of that trade, including filters, memory and updates, is in the full Light Browser case study.
The full story shows what building on WebView2 costs and buys, with more bugs found by real use.
Read the full case study#cpp#webview2#windows#debugging#shutdown
Keep reading
Have a project in mind?