A task's optional Wayland desktop across its lifetime: launch, restart, verification.
The sandbox facade puts four questions to
DesktopAccess and keeps none of the
answers itself:
- at creation, what the selected mode adds to the container's launch
(
prepare);
- before a retained container is disturbed, whether its grant can still be
kept (
preflight);
- before each start, where the desktop is now
(
refresh);
- after a start, whether the endpoint answers from inside the container
(
verify).
A failure here costs GUI access and a warning, never the task. The one
exception is a grant that cannot be kept as it was created: that raises
WaylandRecreationRequired,
because starting anyway would hand the container a different desktop, or
none, behind the operator's back.
Everything a grant adds to the launch travels as Podman arguments and
environment on the prepared RunSpec; the
command assembly knows nothing about desktops.
DesktopAccess(cfg, runtime)
One sandbox's Wayland wiring, from a container's creation to each later start.
Source code in src/terok_sandbox/desktop.py
| def __init__(self, cfg: SandboxConfig, runtime: ContainerRuntime) -> None:
self._cfg = cfg
self._runtime = runtime
|
prepare(spec, *, hooks=None)
Return spec with its desktop wired in, and record what was granted.
Creation is the only moment mounts can be added, so what is recorded
here stands for the container's lifetime: a request that wires
nothing now — no reachable desktop for a direct bind, an isolation
mode without desktop support, a mount that could not be prepared —
stays without GUI on every later start.
Source code in src/terok_sandbox/desktop.py
| def prepare(self, spec: RunSpec, *, hooks: LifecycleHooks | None = None) -> RunSpec:
"""Return *spec* with its desktop wired in, and record what was granted.
Creation is the only moment mounts can be added, so what is recorded
here stands for the container's lifetime: a request that wires
nothing now — no reachable desktop for a direct bind, an isolation
mode without desktop support, a mount that could not be prepared —
stays without GUI on every later start.
"""
if spec.wayland == WaylandMode.NONE:
return spec
warn = _observer(hooks)
unwired = replace(spec, extra_args=(*spec.extra_args, *_UNSET_INHERITED))
session, endpoint = self._grant(spec)
try:
sidecar.record_wayland(self._cfg.sidecar_path(spec.container_name), session)
except (OSError, ValueError) as exc:
# A mount no restart could account for is not made.
wayland.warn_wayland(
spec.wayland, f"cannot record desktop wiring: {exc}", observer=warn
)
return unwired
if endpoint:
wayland.warn_desktop_access(spec.wayland, observer=warn)
if session.error:
wayland.warn_wayland(spec.wayland, session.error, observer=warn)
self._check_direct_domain(session, warn)
if not endpoint:
return unwired
inherited = {k: v for k, v in spec.env.items() if k not in wayland.WAYLAND_ENV_UNSET}
return replace(
unwired,
extra_args=(*unwired.extra_args, *endpoint, *_PRIVATE_RUNTIME),
env={
**inherited,
"WAYLAND_DISPLAY": CONTAINER_WAYLAND_SOCKET,
"XDG_RUNTIME_DIR": CONTAINER_USER_RUNTIME_DIR,
},
)
|
preflight(container_name)
Validate retained desktop access without stopping the task or rewriting state.
Orchestrators call this before disrupting a running task; the
start-time refresh
repeats it, because a desktop may change in between. A retained
direct bind needs readable metadata that matches it: missing optional
state cannot remove a mount Podman already recorded.
Raises:
Source code in src/terok_sandbox/desktop.py
| def preflight(self, container_name: str) -> None:
"""Validate retained desktop access without stopping the task or rewriting state.
Orchestrators call this before disrupting a running task; the
start-time [`refresh`][terok_sandbox.desktop.DesktopAccess.refresh]
repeats it, because a desktop may change in between. A retained
direct bind needs readable metadata that matches it: missing optional
state cannot remove a mount Podman already recorded.
Raises:
WaylandRecreationRequired: The retained desktop grant cannot be verified.
RuntimeError: The runtime cannot inspect the retained container reliably.
"""
saved = self._saved(container_name)
if saved is not None and saved.mode != WaylandMode.MITM:
current = saved.refreshed()
if current.direct_selinux:
# Only the refusal applies here; the warnings belong to a start.
self._check_direct_domain(current, None)
inspection = self._runtime.container(container_name).inspect()
bound = tuple(
Path(source)
for source, target in (inspection.mounts if inspection is not None else ())
if target == CONTAINER_WAYLAND_SOCKET
)
if bound and bound != (saved.bound_socket if saved is not None else None,):
raise WaylandRecreationRequired(
"wl-direct recreation required: cannot verify saved desktop access "
"against the retained compositor bind"
)
|
refresh(container_name, *, hooks=None)
Point the saved grant at the invoking desktop, as every start must.
A reboot or a new desktop session moves the compositor socket; the
container's mounts stay what creation made them.
Source code in src/terok_sandbox/desktop.py
| def refresh(self, container_name: str, *, hooks: LifecycleHooks | None = None) -> None:
"""Point the saved grant at the invoking desktop, as every start must.
A reboot or a new desktop session moves the compositor socket; the
container's mounts stay what creation made them.
"""
warn = _observer(hooks)
self.preflight(container_name)
path = self._cfg.sidecar_path(container_name)
if not path.exists():
return
try:
session = sidecar.update_wayland(path, WaylandSession.refreshed)
if session.mounted:
wayland.warn_desktop_access(session.mode, observer=warn)
self._check_direct_domain(session, warn)
if session.mode == WaylandMode.MITM and session.mounted:
self._proxy_directory(container_name)
if session.error:
wayland.warn_wayland(session.mode, session.error, observer=warn)
except (OSError, ValueError) as exc:
self.preflight(container_name)
# The saved mode is unknown here, so the warning claims none.
wayland.emit_warning(
f"WARNING: Wayland: could not refresh desktop session: {exc}. "
"Task continues; GUI access may be unavailable.",
observer=warn,
)
|
verify(container_name, *, hooks=None)
Probe the endpoint from the container's own security domain; failure stays optional.
A socket visible on the host proves nothing about what the container
may reach, so the question is asked from inside. Only a start that
was wired at all is probed.
Source code in src/terok_sandbox/desktop.py
| def verify(self, container_name: str, *, hooks: LifecycleHooks | None = None) -> None:
"""Probe the endpoint from the container's own security domain; failure stays optional.
A socket visible on the host proves nothing about what the container
may reach, so the question is asked from inside. Only a start that
was wired at all is probed.
"""
warn = _observer(hooks)
session = self._saved(container_name)
if session is None or not session.wired:
return
try:
handle = self._runtime.container(container_name)
result = self._runtime.exec(handle, wayland.WAYLAND_PROBE, timeout=_PROBE_TIMEOUT)
if result.ok:
return
detail = result.stderr.strip()
reason = detail.splitlines()[-1] if detail else "container cannot reach its endpoint"
_logger.debug(
"Wayland endpoint probe failed: %s", sanitize_log_text(detail, color=False)
)
if result.exit_code == 127:
wayland.emit_warning(
f"WARNING: unable to verify Wayland access (python3 unavailable): {reason}; task continues",
observer=warn,
)
else:
log = self._cfg.state_dir / "logs" / f"{handle.id}.log"
wayland.warn_wayland(session.mode, reason, log=log, observer=warn)
except Exception as exc: # noqa: BLE001 - optional diagnostics never fail container startup
detail = str(exc).strip()
reason = detail.splitlines()[-1] if detail else type(exc).__name__
if isinstance(exc, subprocess.TimeoutExpired):
reason = "timed out"
wayland.warn_wayland(
session.mode, f"container endpoint probe failed: {reason}", observer=warn
)
|