The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): serialise the XAUTHORITY swap around thumbnail grabs
teknium1c1d996a84d88
diff --git a/tests/tools/test_bot_desktop_thumbnail.py b/tests/tools/test_bot_desktop_thumbnail.pynew file mode 100644index 00000000000..73f742c69af--- /dev/null+++ b/tests/tools/test_bot_desktop_thumbnail.py@@ -0,0 +1,41 @@+"""``thumbnail_data_url`` swaps the process-wide XAUTHORITY around the grab; two profiles grabbed on+worker threads at once must each see their own cookie file, not the other's."""++from __future__ import annotations++import os+import threading++from PIL import Image, ImageGrab++from tools.bot_desktop import runtime, thumbnail+++def test_concurrent_grabs_each_see_their_own_xauthority(monkeypatch):+ envs = {"a": {"DISPLAY": ":91", "XAUTHORITY": "/tmp/xauth-a"},+ "b": {"DISPLAY": ":92", "XAUTHORITY": "/tmp/xauth-b"}}+ local = threading.local()+ monkeypatch.setattr(runtime, "published_env", lambda: envs[local.profile])+ monkeypatch.setattr(runtime, "_launcher_pid", lambda: 4242)+ monkeypatch.delenv("XAUTHORITY", raising=False)+ seen = {}+ start = threading.Barrier(2)++ def fake_grab(xdisplay=None):+ seen[xdisplay] = os.environ.get("XAUTHORITY")+ threading.Event().wait(0.05) # hold the env long enough for the other thread to collide+ return Image.new("RGB", (8, 8))+ monkeypatch.setattr(ImageGrab, "grab", fake_grab)++ def worker(profile):+ local.profile = profile+ start.wait()+ thumbnail.thumbnail_data_url()++ threads = [threading.Thread(target=worker, args=(p,)) for p in envs]+ for t in threads:+ t.start()+ for t in threads:+ t.join(5)+ assert seen == {":91": "/tmp/xauth-a", ":92": "/tmp/xauth-b"}+ assert "XAUTHORITY" not in os.environdiff --git a/tools/bot_desktop/thumbnail.py b/tools/bot_desktop/thumbnail.pyindex ee41e6ffbc9..eeb61b57892 100644--- a/tools/bot_desktop/thumbnail.py+++ b/tools/bot_desktop/thumbnail.py@@ -9,11 +9,13 @@ from __future__ import annotationsimport base64import ioimport os+import threadingfrom typing import Optionalfrom tools.bot_desktop import runtimeTHUMB_MAX = (960, 600)+_grab_lock = threading.Lock()def thumbnail_data_url(max_size: tuple[int, int] = THUMB_MAX, quality: int = 72) -> Optional[str]:@@ -25,16 +27,19 @@ def thumbnail_data_url(max_size: tuple[int, int] = THUMB_MAX, quality: int = 72)from PIL import ImageGrab # Pillow is a hard dependency; import lazily to keep status calls cheap# Xlib reads XAUTHORITY from the process env; the launcher publishes a per-profile cookie file.- previous = os.environ.get("XAUTHORITY")- if env.get("XAUTHORITY"):- os.environ["XAUTHORITY"] = env["XAUTHORITY"]- try:- image = ImageGrab.grab(xdisplay=display)- finally:- if previous is None:- os.environ.pop("XAUTHORITY", None)- else:- os.environ["XAUTHORITY"] = previous+ # The swap is process-wide, so two profiles grabbed on worker threads at once serialise here or+ # one would grab with the other's cookie and restore the wrong value.+ with _grab_lock:+ previous = os.environ.get("XAUTHORITY")+ if env.get("XAUTHORITY"):+ os.environ["XAUTHORITY"] = env["XAUTHORITY"]+ try:+ image = ImageGrab.grab(xdisplay=display)+ finally:+ if previous is None:+ os.environ.pop("XAUTHORITY", None)+ else:+ os.environ["XAUTHORITY"] = previousimage.thumbnail(max_size)buf = io.BytesIO()image.convert("RGB").save(buf, "JPEG", quality=quality, optimize=True)