wake-alarm/wake_alarm/_ring_main.py
Krzysztof kuhy Rudnicki 899a2c5581
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
release-apk / release (APK) (push) Has been cancelled
Fix RTC +1 s mismatch and busy-ring fixture
rtcwake -t shifts the alarm by the RTC/system clock offset (vmbox: +1 s),
so the exact epoch compare in arm()/disarm_ours() forgot our record and
left the alarm armed. Match within 60 s instead (a local-time RTC is off
by hours and still never matches).

_ring_main now emits ring_backend FAILED when pw-play dies mid-ring, not
only when it never starts.

vmbox: the busy-card holder ran as the ssh user (no ACL on a fresh card)
and was aplay, which handles TERM; it now holds the node as root from a
TERM-ignoring sleep and fails the case as a fixture when nothing holds.
Guest repo path derived from the basename, so worktree runs work.
Records the S5 vmbox result; host S5 check dropped by kuhy.

vmbox harness: 16/16 PASS, ring-alsa-busy-fallback 10/10.

Todo: 364208fc
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:07:41 +01:00

67 lines
2.1 KiB
Python

"""The user-side ring the root ALSA script falls back to.
``scripts/pc/ring_alsa.sh`` runs ``python -m wake_alarm._ring_main <rise>
<loop>`` as the user (``runuser``) the first time the speakers' card is busy:
PipeWire holds it, so the ring goes *through* PipeWire instead of fighting
it. Plays until SIGTERM (exit 0); exits 1 with ``ring_backend ... FAILED``
when PipeWire cannot play, which tells the script to try killing the holder.
"""
from __future__ import annotations
from pathlib import Path
import signal
import sys
import time
from wake_alarm._events import emit
from wake_alarm._logging_setup import configure_logging
from wake_alarm._ring_player import PipeWireBackend
_PUMP_SECONDS = 1.0
_ARGS = 2 # rise.wav, loop.wav
def main(argv: list[str] | None = None) -> int:
"""Ring through PipeWire until SIGTERM; 1 when it cannot, 2 on bad usage."""
args = sys.argv[1:] if argv is None else argv
if len(args) != _ARGS:
sys.stderr.write("Usage: _ring_main <rise.wav> <loop.wav>\n")
return 2
configure_logging(component="ring")
backend = PipeWireBackend(Path(args[0]), Path(args[1]))
started, detail = backend.start()
emit(
"ring_backend",
f"pipewire for root unit ({'started' if started else 'FAILED'})",
backend=backend.name,
why="root unit found the card busy",
started=started,
detail=detail,
)
if not started:
return 1
stopping: list[bool] = []
signal.signal(signal.SIGTERM, lambda *_: stopping.append(True))
try:
while not stopping:
if not backend.pump():
# Dying mid-ring must reach the log as loudly as never starting.
emit(
"ring_backend",
"pipewire for root unit (FAILED)",
backend=backend.name,
why="pw-play stopped mid-ring",
started=False,
detail="pump failed",
)
return 1
time.sleep(_PUMP_SECONDS)
finally:
backend.stop()
return 0
if __name__ == "__main__":
sys.exit(main())