Skip to content

The agent that grows with you

fix(desktop): give Linux Electron a socket-safe temp root

funky-xamarin4a4450c79c19
diff --git a/hermes_cli/main_desktop.py b/hermes_cli/main_desktop.py
index 79bc0b3e873..caf6cfeba99 100644
--- a/hermes_cli/main_desktop.py
+++ b/hermes_cli/main_desktop.py
@@ -1425,9 +1425,14 @@ def _prefer_wsl_d3d12(env: dict) -> None:
def _desktop_launch_env(args: argparse.Namespace) -> tuple[dict, list[str]]:
"""Electron child env + config-supplied extra flags. ``desktop.*`` config is bridged to env vars
Electron already reads; an explicit env var wins over config (and over keychain detection)."""
- from hermes_constants import with_hermes_node_path
+ from hermes_constants import socket_safe_tmpdir, with_hermes_node_path
# with_hermes_node_path() copies os.environ when called with no arg.
env = with_hermes_node_path()
+ if sys.platform == "linux":
+ # Chromium appends scoped_dir*/SingletonSocket to TMPDIR. A deep
+ # profile scratch root can exceed AF_UNIX's path budget and hang
+ # requestSingleInstanceLock(). Keep the exception child-local.
+ env["TMPDIR"] = socket_safe_tmpdir()
_prefer_wsl_d3d12(env)
for attr, key in (
("fake_boot", "HERMES_DESKTOP_BOOT_FAKE"), ("ignore_existing", "HERMES_DESKTOP_IGNORE_EXISTING")):
diff --git a/tests/hermes_cli/test_desktop_socket_tmpdir.py b/tests/hermes_cli/test_desktop_socket_tmpdir.py
new file mode 100644
index 00000000000..30a1511b484
--- /dev/null
+++ b/tests/hermes_cli/test_desktop_socket_tmpdir.py
@@ -0,0 +1,56 @@
+"""The Electron child needs a socket-safe temp root, not a long CLI scratch path."""
+
+import argparse
+import os
+import socket
+import tempfile
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+
+from hermes_cli import main_desktop
+
+
+def _launch(tmp_path):
+ with patch.object(main_desktop, "_desktop_launch_options", return_value=([], "auto", "basic", "auto")):
+ return main_desktop._desktop_launch_env(argparse.Namespace(cwd=str(tmp_path)))
+
+
+def test_long_scratch_root_can_host_electron_singleton_socket(monkeypatch, tmp_path):
+ scratch = tmp_path / ("long-home-" * 8) / "cache" / "scratch"
+ scratch.mkdir(parents=True)
+ for key in ("TMPDIR", "TMP", "TEMP"):
+ monkeypatch.setenv(key, str(scratch))
+ monkeypatch.setenv("HERMES_SCRATCH_DIR", str(scratch))
+ monkeypatch.setattr(tempfile, "tempdir", None)
+
+ env, _ = _launch(tmp_path)
+
+ with tempfile.TemporaryDirectory(prefix="scoped_dir", dir=env["TMPDIR"]) as directory:
+ with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server:
+ server.bind(str(Path(directory) / "SingletonSocket"))
+ assert os.environ["TMPDIR"] == str(scratch)
+ assert env["TMP"] == env["TEMP"] == str(scratch)
+ assert env["HERMES_SCRATCH_DIR"] == str(scratch)
+
+
+def test_short_caller_selected_temp_root_is_preserved(monkeypatch, tmp_path):
+ # Use the existing short-root resolver so this fixture also works under deep CI homes.
+ from hermes_constants import socket_safe_tmpdir
+
+ with tempfile.TemporaryDirectory(prefix="dt-", dir=socket_safe_tmpdir()) as directory:
+ monkeypatch.setenv("TMPDIR", directory)
+ monkeypatch.setattr(tempfile, "tempdir", None)
+ env, _ = _launch(tmp_path)
+ assert env["TMPDIR"] == directory
+
+
+def test_windows_temp_environment_is_not_retargeted(monkeypatch, tmp_path):
+ for key in ("TMPDIR", "TMP", "TEMP"):
+ monkeypatch.setenv(key, str(tmp_path))
+ env, _ = _launch(tmp_path)
+ assert all(env[key] == str(tmp_path) for key in ("TMPDIR", "TMP", "TEMP"))