Skip to content

bridgev2/mxmain: add systemd Type=notify support#500

Open
Deln0r wants to merge 1 commit into
mautrix:mainfrom
Deln0r:feat/sd-notify-megabridge
Open

bridgev2/mxmain: add systemd Type=notify support#500
Deln0r wants to merge 1 commit into
mautrix:mainfrom
Deln0r:feat/sd-notify-megabridge

Conversation

@Deln0r
Copy link
Copy Markdown

@Deln0r Deln0r commented May 12, 2026

Closes #437.

Summary

Megabridge bridges started under a systemd unit with Type=notify currently never report readiness or accept watchdog supervision. Operators have to fall back to Type=simple and lose the ability to distinguish startup failures from runtime failures.

This PR adds three small helpers in bridgev2/matrix/mxmain that wrap the already-vendored coreos/go-systemd/v22/daemon package:

Helper When Effect
sdNotifyReady after BridgeMain.Start() READY=1
sdNotifyStopping before BridgeMain.Stop() STOPPING=1
startSdWatchdog after sdNotifyReady spawns a goroutine that pings WATCHDOG=1 at half the configured interval, returns a stop function

All three calls are no-ops when the process is not running under a systemd unit, so non-systemd deployments are unaffected.

Dependency

github.com/coreos/go-systemd/v22 is already in go.sum as a transitive dependency. go.mod is unchanged.

Test plan

A new TestSdNotifyHelpersAreNoOpWithoutSystemd in bridgev2/matrix/mxmain/systemd_test.go verifies that the helpers do not panic, do not block, and return a usable stop function when NOTIFY_SOCKET / WATCHDOG_USEC are unset.

$ go test -tags=goolm -run TestSdNotifyHelpersAreNoOpWithoutSystemd -v ./bridgev2/matrix/mxmain
=== RUN   TestSdNotifyHelpersAreNoOpWithoutSystemd
--- PASS: TestSdNotifyHelpersAreNoOpWithoutSystemd (0.00s)
PASS

Scope

This PR only touches bridgev2/matrix/mxmain.BridgeMain.Run. Bridges that drive their own main loop directly (without BridgeMain.Run) can call the helpers from their own equivalent of Run if they want the same behavior.

Megabridge bridges started under a systemd unit with `Type=notify`
currently never report readiness or accept watchdog supervision,
which forces operators to fall back to `Type=simple` and lose the
ability to distinguish startup failures from runtime failures.

Add three small helpers in bridgev2/matrix/mxmain that wrap the
already-available coreos/go-systemd v22 daemon package:

  sdNotifyReady     - sends READY=1 once the bridge has finished
                      starting up.
  sdNotifyStopping  - sends STOPPING=1 just before the graceful
                      shutdown begins.
  startSdWatchdog   - pings WATCHDOG=1 at half the configured
                      interval, returning a stop function that
                      blocks until the pinger has exited.

All three calls are no-ops when the process is not running under
a systemd unit (the daemon package returns false without error in
that case), so non-systemd deployments are unaffected.

BridgeMain.Run is updated to call the helpers around the
WaitForInterrupt block. coreos/go-systemd is already a transitive
dependency, so go.mod is unchanged.

Closes mautrix#437.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Systemd sd_notify support (Megabridge)

1 participant