Skip to content
Python

Machine service: start after the OpenShell gateway; honest start-up messages

Rohit C Prasad01ba1c46f295
diff --git a/coworker/remote/joiner.py b/coworker/remote/joiner.py
index e68cfe4f..3bec75c6 100644
--- a/coworker/remote/joiner.py
+++ b/coworker/remote/joiner.py
@@ -830,9 +830,16 @@ def sandbox_status() -> dict:
def _announce_sandbox() -> None:
+ """Say at start how sessions will run. A refusal here is not final: each session checks
+ again when it starts, so a gateway that is still coming up after a reboot (the unit
+ starts seconds before it) only delays sessions, it does not lose them."""
info = sandbox_status()
if info.get("refused"):
- print(f"\n[openworker] SESSIONS WILL BE REFUSED: {info['refused']}\n", file=sys.stderr)
+ print(
+ f"\n[openworker] sessions cannot start yet: {info['refused']}\n"
+ "[openworker] Each session checks again when it starts.\n",
+ file=sys.stderr,
+ )
elif info.get("warning"):
bar = "!" * 78
print(f"\n{bar}\n[openworker] {info['warning']}\n{bar}\n", file=sys.stderr)
@@ -881,7 +888,9 @@ def _systemd_unit(state: Path, exe: str, name: str = "") -> str:
return (
"[Unit]\n"
f"Description=OpenWorker headless (joined machine{label})\n"
- "After=network-online.target\n"
+ # After the OpenShell gateway too, when it is installed: started before it, the
+ # first sessions after a reboot wait for it (seen 2026-09-29, a 7 s gap).
+ "After=network-online.target openshell-gateway.service\n"
"Wants=network-online.target\n"
"\n"
"[Service]\n"
@@ -895,6 +904,22 @@ def _systemd_unit(state: Path, exe: str, name: str = "") -> str:
)
+def _linger_on() -> bool:
+ """Whether this user's services keep running without a login session (`loginctl
+ enable-linger`). Unknown counts as off, so the reminder is printed."""
+ import getpass
+ import subprocess
+
+ try:
+ done = subprocess.run(
+ ["loginctl", "show-user", getpass.getuser(), "-p", "Linger"],
+ capture_output=True, text=True, timeout=10,
+ )
+ except (OSError, subprocess.TimeoutExpired):
+ return False
+ return done.returncode == 0 and done.stdout.strip() == "Linger=yes"
+
+
def installed_units(unit_dir: Path) -> list[dict]:
"""Every openworker*.service user unit on this box with the state dir it
pins — what `service install` reports so a stale twin is visible, not a
@@ -999,10 +1024,11 @@ def _cmd_service(state: Path, args, platform: Optional[str] = None, home: Option
print(f"wrote {unit_path}")
if _systemctl_user("daemon-reload") and _systemctl_user("enable", "--now", derived):
print(f"service enabled and started (systemctl --user status {derived}).")
- print(
- "To keep it running after logout/reboot without a login session:\n"
- f" sudo loginctl enable-linger {__import__('getpass').getuser()}"
- )
+ if not _linger_on():
+ print(
+ "To keep it running after logout/reboot without a login session:\n"
+ f" sudo loginctl enable-linger {__import__('getpass').getuser()}"
+ )
else:
print(
"systemd user session not reachable from here — enable it manually:\n"
diff --git a/tests/test_remote_home.py b/tests/test_remote_home.py
index 29c87ce6..c302ca39 100644
--- a/tests/test_remote_home.py
+++ b/tests/test_remote_home.py
@@ -1653,6 +1653,18 @@ def test_systemd_unit_content(tmp_path):
assert f"Environment=COWORKER_STATE_DIR={tmp_path / 'state'}" in unit
assert "Restart=always" in unit
assert "WantedBy=default.target" in unit
+ # Ordered after the OpenShell gateway: started before it, sessions wait after a reboot.
+ assert "After=network-online.target openshell-gateway.service" in unit
+
+
+def test_sandbox_announcement_says_sessions_check_again(capsys, monkeypatch):
+ from coworker.remote import joiner
+
+ monkeypatch.setattr(joiner, "sandbox_status", lambda: {"provider": None, "refused": "The OpenShell gateway is not running"})
+ joiner._announce_sandbox()
+ err = capsys.readouterr().err
+ assert "sessions cannot start yet: The OpenShell gateway is not running" in err
+ assert "checks again when it starts" in err and "WILL BE REFUSED" not in err
def test_service_install_refuses_non_linux_and_unjoined(tmp_path, capsys):