Skip to content

Add the installer's OOM settings to host daemon units written before them - #8298

Merged
ymichael merged 1 commit into
get-bb:mainfrom
lanjak:fix-systemd-unit-oom-upgrade
Dec 1, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
lanjak:fix-systemd-unit-oom-upgrade

Conversation

@lanjak

@lanjak lanjak commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

install-machine.sh writes OOMPolicy=continue (#7039) and ManagedOOMPreference=avoid (#7823) into the host daemon's systemd unit. But only a new install writes the unit. A unit from before 2026-11-04 keeps the systemd default OOMPolicy=stop, so one kernel OOM kill of an agent process stops the whole service. Every thread on the machine stops with it. No part of bb updates a unit that exists, and --auto-update changes only the bb-app package.

What changed

 host daemon startup (apps/host-daemon/src/app.ts)
+  serverMove.updateServiceDefinition()     # add missing [Service] settings, daemon-reload
   serverMove.resumeActivation()            # unchanged, now runs after the update
  • packages/host-daemon-contract/src/server-move.ts: one list, HOST_DAEMON_SYSTEMD_SERVICE_DIRECTIVES, holds the two settings. The daemon and the installer tests both read it.
  • apps/host-daemon/src/server-move/service-definition.ts: addMissingSystemdServiceDirectives(content) adds each missing setting at the end of [Service]. A setting that the unit sets already stays as it is, also OOMPolicy = stop with spaces. systemdDirective now accepts spaces around =, as systemd does.
  • apps/host-daemon/src/server-move/service.ts: updateServiceDefinition() finds the daemon's own unit with the same lookup a server move uses (BB_DATA_DIR must match this data dir), writes the file, and runs systemctl <scope> daemon-reload. It does not restart the service. The update uses the same queue as server-moved notices, so the two never write the unit at the same time. A launchd plist and BB_SERVER_MOVE_SERVICE_MANAGER=none are not changed.
  • apps/host-daemon/src/server-move/service-manager.ts: reloadSystemdService comes out of restartService, which now calls it.
  • apps/server/test/app/install-machine-script.test.ts: the user and system unit tests compare the [Service] settings of the generated unit with the shared list, in both directions. A new setting in the installer that is not in the list fails the test.
  • No wire change, so HOST_DAEMON_PROTOCOL_VERSION does not change.

Choice: the daemon updates the unit at startup. Precedent: the daemon already rewrites the same file on a server move (switchToMovedServer, writeServiceDefinition). The alternative is to tell users to rerun the installer, as #7823 does. That restarts the daemon and most users do not know they need it. A maintainer can ask for the other choice in a comment or push commits to this branch.

How you verified

Check Before (main b0ddaad) After
service-definition.test.ts: 6 cases for addMissingSystemdServiceDirectives (old unit, partial, other section, spaces, CRLF, no [Service]) fail: addMissingSystemdServiceDirectives is not a function pass
service-update.test.ts: old unit gets the settings and one daemon-reload, no restart, file mode kept; failed reload is logged; a server move during the update keeps both changes; unit settings are kept; none service manager is not touched fail pass
Installer test with MemoryHigh=90% added only to install-machine.sh - fails as expected

Live check on systemd 262 (Arch Linux), with a scratch user unit bb-host-daemon-oom-live-check.service (ExecStart=/usr/bin/sleep infinity, no OOM settings) and the real ServerMoveService:

before: MainPID=3079403 OOMPolicy=stop ManagedOOMPreference=none
info Added the current OOM settings to the bb service definition
after:  MainPID=3079403 OOMPolicy=continue ManagedOOMPreference=avoid

A second run changed nothing. The script removed the scratch unit.

On the machine that found this, an old unit restarted the daemon on each of four OOM kills of agent processes in nine minutes. With the same two settings in a drop-in, five more OOM kills did not restart it.

pnpm exec turbo run typecheck test lint lint:type-aware --continue --filter=@bb/host-daemon --filter=@bb/host-daemon-contract --filter=@bb/server: all typecheck and lint tasks pass with 0 warnings. @bb/server tests pass. Two tests fail locally and also fail on unchanged main on this machine: payload-size.test.ts (Node 26 gzip sizes) and protocol-self-update.test.ts (Arch keeps npm outside the Node directory). CI uses Node 22.

An Opus review found no blocking defect. Its points are fixed here: the update now runs before resumeActivation and on the server-moved queue, a failed reload is logged as such, settings with spaces are kept, and the drift test checks both directions.

Not covered by a test: the one app.ts line that starts the update. resumeActivation at the same place has no test either.

Fixes #8297

AGENT GENERATED

…them

install-machine.sh writes OOMPolicy=continue (get-bb#7039) and
ManagedOOMPreference=avoid (get-bb#7823) into the host daemon's systemd unit,
but only on a new install. A unit written before 2026-11-04 keeps
systemd's default OOMPolicy=stop, so one OOM kill of an agent process
stops the whole service and interrupts every thread on the machine.

At startup, before a server move resumes, the daemon now adds each
missing setting to the [Service] section of its own unit and runs
systemctl daemon-reload. The update runs on the same queue as
server-moved notices, so the two never write the unit at the same time.
Settings that the unit already sets are kept, also when written with
spaces around the equals sign. The installer tests and the daemon read
the same list from @bb/host-daemon-contract, and the installer test
fails if the unit gains a [Service] setting that is not in the list.
@ymichael
ymichael merged commit aec39ec into get-bb:main Dec 1, 2026
49 checks passed
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…them (get-bb#8298)

## Human comments

## What was wrong

`install-machine.sh` writes `OOMPolicy=continue` (get-bb#7039) and
`ManagedOOMPreference=avoid` (get-bb#7823) into the host daemon's systemd
unit. But only a new install writes the unit. A unit from before
2026-11-04 keeps the systemd default `OOMPolicy=stop`, so one kernel OOM
kill of an agent process stops the whole service. Every thread on the
machine stops with it. No part of bb updates a unit that exists, and
`--auto-update` changes only the bb-app package.

## What changed

```diff
 host daemon startup (apps/host-daemon/src/app.ts)
+  serverMove.updateServiceDefinition()     # add missing [Service] settings, daemon-reload
   serverMove.resumeActivation()            # unchanged, now runs after the update
```

- **`packages/host-daemon-contract/src/server-move.ts`**: one list,
`HOST_DAEMON_SYSTEMD_SERVICE_DIRECTIVES`, holds the two settings. The
daemon and the installer tests both read it.
- **`apps/host-daemon/src/server-move/service-definition.ts`**:
`addMissingSystemdServiceDirectives(content)` adds each missing setting
at the end of `[Service]`. A setting that the unit sets already stays as
it is, also `OOMPolicy = stop` with spaces. `systemdDirective` now
accepts spaces around `=`, as systemd does.
- **`apps/host-daemon/src/server-move/service.ts`**:
`updateServiceDefinition()` finds the daemon's own unit with the same
lookup a server move uses (`BB_DATA_DIR` must match this data dir),
writes the file, and runs `systemctl <scope> daemon-reload`. It does not
restart the service. The update uses the same queue as server-moved
notices, so the two never write the unit at the same time. A launchd
plist and `BB_SERVER_MOVE_SERVICE_MANAGER=none` are not changed.
- **`apps/host-daemon/src/server-move/service-manager.ts`**:
`reloadSystemdService` comes out of `restartService`, which now calls
it.
- **`apps/server/test/app/install-machine-script.test.ts`**: the user
and system unit tests compare the `[Service]` settings of the generated
unit with the shared list, in both directions. A new setting in the
installer that is not in the list fails the test.
- No wire change, so `HOST_DAEMON_PROTOCOL_VERSION` does not change.

Choice: the daemon updates the unit at startup. Precedent: the daemon
already rewrites the same file on a server move (`switchToMovedServer`,
`writeServiceDefinition`). The alternative is to tell users to rerun the
installer, as get-bb#7823 does. That restarts the daemon and most users do not
know they need it. A maintainer can ask for the other choice in a
comment or push commits to this branch.

## How you verified

| Check | Before (`main` a8c3228) | After |
| --- | --- | --- |
| `service-definition.test.ts`: 6 cases for
`addMissingSystemdServiceDirectives` (old unit, partial, other section,
spaces, CRLF, no `[Service]`) | fail:
`addMissingSystemdServiceDirectives is not a function` | pass |
| `service-update.test.ts`: old unit gets the settings and one
`daemon-reload`, no restart, file mode kept; failed reload is logged; a
server move during the update keeps both changes; unit settings are
kept; `none` service manager is not touched | fail | pass |
| Installer test with `MemoryHigh=90%` added only to
`install-machine.sh` | - | fails as expected |

Live check on systemd 262 (Arch Linux), with a scratch user unit
`bb-host-daemon-oom-live-check.service` (`ExecStart=/usr/bin/sleep
infinity`, no OOM settings) and the real `ServerMoveService`:

```text
before: MainPID=3079403 OOMPolicy=stop ManagedOOMPreference=none
info Added the current OOM settings to the bb service definition
after:  MainPID=3079403 OOMPolicy=continue ManagedOOMPreference=avoid
```

A second run changed nothing. The script removed the scratch unit.

On the machine that found this, an old unit restarted the daemon on each
of four OOM kills of agent processes in nine minutes. With the same two
settings in a drop-in, five more OOM kills did not restart it.

`pnpm exec turbo run typecheck test lint lint:type-aware --continue
--filter=@bb/host-daemon --filter=@bb/host-daemon-contract
--filter=@bb/server`: all typecheck and lint tasks pass with 0 warnings.
`@bb/server` tests pass. Two tests fail locally and also fail on
unchanged `main` on this machine: `payload-size.test.ts` (Node 26 gzip
sizes) and `protocol-self-update.test.ts` (Arch keeps npm outside the
Node directory). CI uses Node 22.

An Opus review found no blocking defect. Its points are fixed here: the
update now runs before `resumeActivation` and on the server-moved queue,
a failed reload is logged as such, settings with spaces are kept, and
the drift test checks both directions.

Not covered by a test: the one `app.ts` line that starts the update.
`resumeActivation` at the same place has no test either.

Fixes get-bb#8297

> AGENT GENERATED
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.

Host daemon units from before 2026-11-04 keep OOMPolicy=stop, so one OOM kill of an agent process interrupts every thread

2 participants