Skip to content

The agent that grows with you

fix: derive the lost-bus check from the env the scoped worker was spawned with

teknium114ebd48c64cd
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 8088f1ca230..2acde950253 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3261,7 +3261,7 @@ def _launch_external_cron_worker(job: dict) -> bool:
with _running_lock:
_restart_safe_waiter_job_ids.discard(job_id)
payload_path.unlink(missing_ok=True)
- if dispatch.mode == "scoped" and scoped_spawn_lost_user_bus():
+ if dispatch.mode == "scoped" and scoped_spawn_lost_user_bus(worker_env):
# systemd-run itself failed (stderr is DEVNULL): name the cause, not the exit code.
raise RuntimeError(
"restart-safe systemd scope could not be created: the user D-Bus session at "
diff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.py
index d08b9210f96..444c5e9a48a 100644
--- a/tests/tools/test_process_registry.py
+++ b/tests/tools/test_process_registry.py
@@ -2634,6 +2634,44 @@ class TestSystemdCgroupIsolation:
assert "XDG_RUNTIME_DIR" not in os.environ
assert "DBUS_SESSION_BUS_ADDRESS" not in os.environ
+ @pytest.mark.linux_only
+ def test_scoped_spawn_lost_user_bus_honours_configured_runtime_dir(self, monkeypatch, request):
+ """The lost-bus check must derive from the env the worker was spawned with: when the bus
+ lives under a configured ``XDG_RUNTIME_DIR`` (not ``/run/user/<uid>``), an unrelated wrapper
+ exit is not a lost bus and must not flip the cached scope verdict to unscoped dispatch."""
+ import socket
+ import tempfile
+
+ import tools.process_registry as pr
+
+ runtime_dir = pr.Path(tempfile.mkdtemp(prefix="hbus-", dir="/tmp"))
+ runtime_dir.chmod(0o700)
+ bus_path = runtime_dir / "bus"
+ bus_socket = socket.socket(socket.AF_UNIX)
+ bus_socket.bind(str(bus_path))
+
+ def _cleanup():
+ bus_socket.close()
+ bus_path.unlink(missing_ok=True)
+ runtime_dir.rmdir()
+
+ request.addfinalizer(_cleanup)
+
+ monkeypatch.setattr(pr, "_default_user_runtime_dir", lambda: pr.Path("/nonexistent/run/user/0"))
+ monkeypatch.setattr(pr, "_SYSTEMD_SCOPE_AVAILABLE", True)
+ monkeypatch.setattr(pr, "_SYSTEMD_SCOPE_PROBED_AT", pr.time.monotonic())
+ spawn_env = pr.systemd_user_bus_env({"XDG_RUNTIME_DIR": str(runtime_dir)})
+ assert spawn_env["DBUS_SESSION_BUS_ADDRESS"] == f"unix:path={bus_path}"
+
+ assert pr.scoped_spawn_lost_user_bus(spawn_env) is False
+ assert pr._SYSTEMD_SCOPE_AVAILABLE is True
+
+ # Same spawn env, bus actually gone: now it is a lost bus and the verdict flips.
+ bus_socket.close()
+ bus_path.unlink()
+ assert pr.scoped_spawn_lost_user_bus(spawn_env) is True
+ assert pr._SYSTEMD_SCOPE_AVAILABLE is False
+
@pytest.mark.linux_only
def test_probe_succeeds_without_bin_true(self, monkeypatch):
"""An absent ``/bin/true`` must not make a usable scope fail its probe."""
diff --git a/tools/process_registry.py b/tools/process_registry.py
index f257c68cf63..ff8122a3ade 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -325,13 +325,20 @@ class GatewayChildDispatch(NamedTuple):
argv: List[str]
-def scoped_spawn_lost_user_bus() -> bool:
+def scoped_spawn_lost_user_bus(spawn_env: Dict[str, str]) -> bool:
"""After a ``systemd-run --user --scope`` wrapper exits before its child could start: True
when the user bus is gone (:func:`systemd_user_bus_env` derives nothing), in which case the
cached True verdict is replaced so the next dispatch re-probes and degrades instead of
- consuming another occurrence on the same dead wrapper (#110803)."""
+ consuming another occurrence on the same dead wrapper (#110803).
+
+ *spawn_env* is the environment the wrapper was launched with: re-deriving from it (minus the
+ bus address it carried) honours a configured ``XDG_RUNTIME_DIR`` exactly as the spawn did, so
+ an unrelated wrapper exit on a host whose bus lives outside ``/run/user/<uid>`` is not
+ misread as a lost bus."""
global _SYSTEMD_SCOPE_AVAILABLE, _SYSTEMD_SCOPE_PROBED_AT
- if systemd_user_bus_env({}):
+ base_env = dict(spawn_env)
+ base_env.pop("DBUS_SESSION_BUS_ADDRESS", None)
+ if "DBUS_SESSION_BUS_ADDRESS" in systemd_user_bus_env(base_env):
return False
with _SYSTEMD_SCOPE_PROBE_LOCK:
_SYSTEMD_SCOPE_AVAILABLE = False