Open Bug 2094017 Opened 13 days ago Updated 9 hours ago

Session sometimes isn't restored after a Windows restart when Firefox is also set to launch on login

Categories

(Firefox :: Shell Integration, defect, P2)

Unspecified
Windows
defect

Tracking

()

People

(Reporter: drubino, Unassigned)

References

(Blocks 1 open bug)

Details

When a user has Firefox launch on login turned on, Windows can start two copies of Firefox when they sign back in after a restart. That happens after any restart if the Windows setting "Automatically save my restartable apps and restart them when I sign back in" is also on, and after a restart to install a Windows update even if it's off (in a manual test with the setting off, Windows still relaunched Firefox after an update restart). Which one gets there first is a race, and if the wrong one wins, the user doesn't get their session back.

How it happens:

  • Launch on login is a value under HKCU\Software\Microsoft\Windows\CurrentVersion\Run that starts Firefox with -os-autostart.
  • Firefox also calls RegisterApplicationRestart at startup (toolkit.winRegisterApplicationRestart). At shutdown, Windows saves that command line (with -os-restarted in front) as an Application Restart #N value under HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce, and replays it at the next sign-in.
  • Explorer starts Run and RunOnce entries asynchronously, and Microsoft documents the order as indeterminate (https://devblogs.microsoft.com/oldnewthing/20250515-00/?p=111181). So the two launches race, and the second one is forwarded to the first through the remote service.

The behavior we want is the OS restart with session restore, and that already works when the OS restart wins the race. Firefox starts with -os-restarted, Services.appinfo.restartedByOS is true, and the session is resumed (resume_reason "os_restart" in session_restore.startup_session_decision). The launch on login then gets forwarded to the running instance, where BrowserContentHandler deliberately ignores a non-initial command line that has -os-autostart.

When the launch on login wins, restartedByOS is false, so SessionStartup withdraws the resume_session_once that was armed for the OS restart, and the user gets their normal startup page instead of their session (unless they always restore their session anyway). The replayed restart then arrives as a forwarded command line, but -os-restarted is stripped in XRE_mainInit before the remote client sends it on, so the running instance can't tell it apart from a normal second launch. If the original command line didn't include -os-autostart, it presumably opens an extra window too. The session is still available from "Restore previous session", but most users won't think to look there.

Based on telemetry, for users with both settings enabled, session restore after a Windows restart is missed about 61% of the time, compared with about 33% for users who only have the restartable apps setting on.

Steps to reproduce:

  1. In Firefox settings, turn on "Open Firefox automatically when your computer starts up", and leave the startup page set to the home page.
  2. In Windows Settings > Accounts > Sign-in options, turn on "Automatically save my restartable apps and restart them when I sign back in".
  3. Open a few tabs.
  4. Restart Windows and sign back in. Repeat a few times, since it's a race.

Expected: the previous session is restored every time, in a single window.
Actual: sometimes the home page opens instead (and possibly a second window).

Some possible fixes, which I'll leave to engineering to weigh:

  • Course correct after the fact. Keep a marker for -os-restarted on the forwarded command line, and when the running instance was itself a launch on login, call SessionStore.restoreLastSession() (which reuses the current window when it isn't part of the old session) instead of treating it as a new launch. This probably needs a short time limit, or a check that the user hasn't started browsing yet.
  • Decide up front. When Firefox starts with -os-autostart and browser.sessionstore.resuming_after_os_restart is set, resume the session instead of withdrawing it, since a replay is presumably on its way. The hard part is knowing whether Windows will actually replay; the RestartApps value under HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon reflects the Windows setting, but it isn't documented.
  • Avoid the race. Don't register for application restart while launch on login is enabled, and let the launch on login do the session restore. That changes behavior for users who turned the Windows setting off, so I think it's as much a product call as an engineering one.

Whichever fix we pick also has to deal with the launch on login infobar, which targets isLaunchOnLogin. When the OS restart wins, BrowserInitState.isLaunchOnLogin stays false because the forwarded -os-autostart is ignored, so that sign-in doesn't count as a launch on login for messaging or for the Autostart launch method. Recording the forwarded flag instead isn't a clean answer either... it can arrive well after the first window opened and messaging was evaluated, so the session would flip to being a launch on login partway through. macOS has the same gap for a different reason: when "Reopen windows when logging back in" brings Firefox back, macOS sends a restore event instead of the login item one, so bug 2065018 "On macOS, there is no signal that the current session was started via launch-on-login" can't see it either. We need to decide whether a sign-in where both mechanisms fired should count as a launch on login, and if so, how messaging and telemetry should handle a signal that shows up late.

Triage: S3 / P2.

S3: no data loss, since "Restore previous session" still works. It only affects Windows users with both launch on login and the Windows "restart apps" setting on, and it depends on a race.

P2: it blocks bug 2082561 and the reported miss rate for affected users is high -> ~61%.

As far as I have checked, this is not a regression. -os-autostart has been around since bug 1843202 (Oct 2023), so the race is a uncontrolled gap in how the two mechanisms interact, not a recent change.

Severity: -- → S3
Priority: -- → P2
You need to log in before you can comment on or make changes to this bug.