Files
Hermenix 352cfdcb24 claim windows by pid, and find the compositor after a restart
Two things stood between "containers enabled" and an application actually
sitting on its own output, and neither was where I said it was.

ipc: discovery refused whenever two instance directories existed, which every
compositor restart leaves behind (a log file where a live compositor used to
be). After two rebuilds the broker could not reach any compositor at all, so no
container could be created and the pointer had nothing to aim at. It now asks
each candidate for its version and uses the one that answers, preferring the
newest only when two answer, and still refusing when none do.

container_host.claim: windows are assigned by the pid the compositor already
reports, not by the class stamp the per-session rule matches -- foot, GTK, Qt
and the chromium wrapper all ignore that stamp, so the desk was created and
left empty while the window sat in special:quarantine. The claim moves the
window to its desk's workspace, makes that workspace the one the desk is
showing, and reads back: the record says where the window is, never where it
was sent. Called from both places a window can appear -- the read-back at start
and container.reconcile -- and skipped in the steady state, because a move is a
real change on screen and should not be dispatched for its own sake.
2026-10-03 19:39:33 +00:00

208 lines
7.3 KiB
Python

"""Finding the instance: one answer beats a name that merely exists.
Two instance dirs and no signature used to be a hard refusal. It still is when
neither answers — a broker that guessed would aim its queries, and later its
pointer, at someone else's desktop. But a *dead* instance directory is left
behind by every compositor restart, so refusing on two names made the broker
blind after every rebuild. The compositor that answers a version request is the
one that exists; the corpse beside it is a log file.
"""
from __future__ import annotations
import os
import socket
import threading
import pytest
from hhstreamd.ipc import SIGNATURE_ENV, CompositorUnavailable, HyprlandIPC
BANNER = b"Hyprland 0.56.2 built from branch v0.56.2-b\nTag: v0.56.2\n"
def bind(path):
"""A bound, listening socket at ``path``, so discovery sees a real one."""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(str(path))
sock.listen(1)
return sock
def answering(path):
"""A socket with a compositor behind it: it replies to a version request."""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(str(path))
sock.listen(4)
def serve() -> None:
while True:
try:
conn, _ = sock.accept()
except OSError:
return
with conn:
# The client half-closes after writing, so the request ends at
# EOF -- the same shape the real compositor sees.
while conn.recv(4096):
pass
try:
conn.sendall(BANNER)
except OSError:
pass
threading.Thread(target=serve, daemon=True).start()
return sock
def test_single_instance_is_discovered(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"
assert ipc.event_socket == instance / ".socket2.sock"
def test_no_instance_directory_at_all(runtime_dir, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
assert str(runtime_dir / "hypr") in str(caught.value)
def test_zero_instances_under_hypr(runtime_dir, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
(runtime_dir / "hypr").mkdir()
with pytest.raises(CompositorUnavailable, match="no instance"):
HyprlandIPC.discover(runtime_dir)
def test_two_instances_and_one_answers_uses_the_one_that_answers(
runtime_dir, instance, monkeypatch
):
"""The corpse beside the live compositor must not make the broker blind."""
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
corpse = runtime_dir / "hypr" / "deadbee_1700000001"
corpse.mkdir()
(corpse / "hyprland.log").write_text("this compositor is gone; only its log is here\n")
sock = answering(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"
def test_two_instances_and_neither_answers_refuses(runtime_dir, instance, monkeypatch):
"""Nothing answering is still the dangerous case, and still refused."""
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
(other / "hyprland.log").write_text("dead\n")
(instance / ".socket.sock").write_text("not a socket")
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
message = str(caught.value)
assert "2 instances" in message
assert instance.name in message and other.name in message
assert "none of them answered" in message
assert SIGNATURE_ENV in message
def test_two_answering_instances_pick_the_most_recent(runtime_dir, instance, monkeypatch):
"""Two live compositors is a real ambiguity: take the newest, and say so."""
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
older = runtime_dir / "hypr" / "aaaaaaa_1700000001"
older.mkdir()
older_sock = answering(older / ".socket.sock")
newer = runtime_dir / "hypr" / "bbbbbbb_1700000002"
newer.mkdir()
newer_sock = answering(newer / ".socket.sock")
try:
os.utime(older / ".socket.sock", (1_700_000_001, 1_700_000_001))
os.utime(newer / ".socket.sock", (1_700_000_002, 1_700_000_002))
ipc = HyprlandIPC.discover(runtime_dir)
finally:
older_sock.close()
newer_sock.close()
assert ipc.request_socket == newer / ".socket.sock"
def test_two_instances_with_an_explicit_signature(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
sock = bind(other / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir, signature=other.name)
finally:
sock.close()
assert ipc.request_socket == other / ".socket.sock"
def test_signature_from_the_environment(runtime_dir, instance, monkeypatch):
other = runtime_dir / "hypr" / "deadbee_1700000001"
other.mkdir()
monkeypatch.setenv(SIGNATURE_ENV, other.name)
sock = bind(other / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == other / ".socket.sock"
def test_explicit_signature_beats_the_environment(runtime_dir, instance, monkeypatch):
monkeypatch.setenv(SIGNATURE_ENV, "not_this_one")
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir, signature=instance.name)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"
def test_signature_naming_a_missing_instance(runtime_dir, instance, monkeypatch):
monkeypatch.setenv(SIGNATURE_ENV, "gone_1700000002")
with pytest.raises(CompositorUnavailable, match="request socket"):
HyprlandIPC.discover(runtime_dir)
def test_instance_without_a_request_socket(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
with pytest.raises(CompositorUnavailable) as caught:
HyprlandIPC.discover(runtime_dir)
assert ".socket.sock" in str(caught.value)
def test_a_regular_file_is_not_a_socket(runtime_dir, instance, monkeypatch):
monkeypatch.delenv(SIGNATURE_ENV, raising=False)
(instance / ".socket.sock").write_text("not a socket")
with pytest.raises(CompositorUnavailable, match="is not a socket"):
HyprlandIPC.discover(runtime_dir)
def test_a_signature_cannot_be_a_path(runtime_dir, instance):
with pytest.raises(CompositorUnavailable, match="not a directory name"):
HyprlandIPC.discover(runtime_dir, signature="../../elsewhere")
def test_an_empty_environment_signature_falls_through(runtime_dir, instance, monkeypatch):
"""An exported-but-empty variable is no signature, not a signature of ""."""
monkeypatch.setenv(SIGNATURE_ENV, "")
sock = bind(instance / ".socket.sock")
try:
ipc = HyprlandIPC.discover(runtime_dir)
finally:
sock.close()
assert ipc.request_socket == instance / ".socket.sock"