---
# System prepended metadata

title: Lacros Keap Alive
tags: [elkurin-daily-note, Chromium-Lacros]

---

# Lacros Keep Alive

Keep alive is a feature to ensure Lacros to be running.  
On shutting down Lacros, if KeepAlive is enabled, [BrowserManager](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h) will trigger Lacros launch again which means it ensures Lacros to stay running in the background.

## Overview
Keep alive is handled as std::set of [Feature](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h;l=537;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a).  
Feature specifies ash features that are allowed to request keep alive. [`keep_alive_features_`](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h;l=793;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) is the container.

Keep Alive is expected to be enabled all the time when Lacros is enabled.

[KeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1035;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) instance is constructed from for example [PersistentForcedExtensionKeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/persistent_forced_extension_keep_alive.cc;l=83-84;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a), [AppServiceProxyAsh](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/apps/app_service/app_service_proxy_ash.cc;l=185-186;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) and so on.

If some of the instance holds this keep alive instance, it ensures Lacros to be launched.

## ScopedKeepAlive
[KeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1035;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) creates ScopedKeepAlive unique pointer.  

[ScopedKeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h;l=551;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) is a class used to create KeepAlive object that ensures lacros to be launched.

ScopedKeepAlive ctor calls [StartKeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1895;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) with the feature you woul like to add.  
Then it triggers [StartIfNeeded](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1676;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) of the given `browser`. StartIfNeeded starts Lacros when `state_` is State::STOPPED and the shudown has not yet requested.

On destruction, [StopKeepAlive](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1030;drc=6ca5cf6c747a131e785b0c3bdc2313d1815d7fd4) is called and changes the keep alive status by [UpdateKeepAliveInBrowserIfNecessary](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.cc;l=1928;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a).  

## For testing
For testing purpose, we sometimes want to test the behavior without keep alive feature. In such case, it's tough to remove all KeepAlive instance, so BrowserManager holds a separate testing API for it.

[ScopedUnsetAllKeepAliveForTesting](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h;l=565;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) class ignores all existing KeepAlive featurs.

On construction, it stores `keep_alive_features_` to [`previous_keep_alive_features_`](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/crosapi/browser_manager.h;l=572;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) and remove it from BrowserManager, and update keep alive to false.  
This kept previous features will be restored on the destructor of ScopedUnsetAllKeepAliveForTesting.