Skip to content

The agent that grows with you

fix(desktop): only Electron's singleton socket gets the short TMPDIR

teknium18860e1d78b2c
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 10c0f339e91..8ebdd506d40 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -939,6 +939,13 @@ if (!isPrimaryInstance) {
app.exit(0)
}
+// `hermes desktop` shortens TMPDIR only so the lock above can bind its socket (#124688). The
+// backend and every other child get the real one (the profile scratch dir) back.
+if (process.env.HERMES_DESKTOP_TMPDIR) {
+ process.env.TMPDIR = process.env.HERMES_DESKTOP_TMPDIR
+ delete process.env.HERMES_DESKTOP_TMPDIR
+}
+
const HERMES_HOME: string = resolveDesktopHermesHome({
home: app.getPath('home'),
directoryExists,
diff --git a/hermes_cli/main_desktop.py b/hermes_cli/main_desktop.py
index caf6cfeba99..0a76d222816 100644
--- a/hermes_cli/main_desktop.py
+++ b/hermes_cli/main_desktop.py
@@ -1422,16 +1422,23 @@ def _prefer_wsl_d3d12(env: dict) -> None:
env["GALLIUM_DRIVER"] = "d3d12"
+# Chromium's ProcessSingleton binds $TMPDIR/scoped_dirXXXXXX/SingletonSocket (33 bytes after
+# TMPDIR, measured on Electron 40); sun_path holds 107 bytes plus the NUL.
+_ELECTRON_TMPDIR_MAX_BYTES = 107 - len("/scoped_dirXXXXXX/SingletonSocket")
+_DESKTOP_TMPDIR_ENV = "HERMES_DESKTOP_TMPDIR"
+
+
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 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.
+ tmpdir = env.get("TMPDIR", "")
+ if sys.platform == "linux" and len(os.fsencode(tmpdir)) > _ELECTRON_TMPDIR_MAX_BYTES:
+ # A longer TMPDIR hangs requestSingleInstanceLock(). Only Chromium's socket dir moves:
+ # Electron main hands the real TMPDIR back to the backend and its other children.
+ env[_DESKTOP_TMPDIR_ENV] = tmpdir
env["TMPDIR"] = socket_safe_tmpdir()
_prefer_wsl_d3d12(env)
for attr, key in (
diff --git a/tests/hermes_cli/test_desktop_socket_tmpdir.py b/tests/hermes_cli/test_desktop_socket_tmpdir.py
index 272abe7423a..15249b6b758 100644
--- a/tests/hermes_cli/test_desktop_socket_tmpdir.py
+++ b/tests/hermes_cli/test_desktop_socket_tmpdir.py
@@ -1,6 +1,9 @@
"""The Electron child needs a socket-safe temp root, not a long CLI scratch path (#124688)."""
import argparse
+import os
+import secrets
+import shutil
import socket
import tempfile
from pathlib import Path
@@ -15,8 +18,19 @@ def _launch_env(tmp_path) -> dict:
return env
+def _bind_singleton_socket(tmpdir: str) -> None:
+ # Chromium's ProcessSingleton binds $TMPDIR/scoped_dirXXXXXX/SingletonSocket (6 random chars).
+ directory = Path(tmpdir) / f"scoped_dir{secrets.token_hex(3)}"
+ directory.mkdir()
+ try:
+ with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server:
+ server.bind(str(directory / "SingletonSocket"))
+ finally:
+ shutil.rmtree(directory)
+
+
@pytest.mark.platforms("linux")
-def test_long_scratch_tmpdir_still_hosts_electrons_singleton_socket(monkeypatch, tmp_path):
+def test_long_scratch_tmpdir_hosts_the_singleton_socket_and_is_kept_for_children(monkeypatch, tmp_path):
scratch = tmp_path / ("long-home-" * 8) / "cache" / "scratch"
scratch.mkdir(parents=True)
for key in ("TMPDIR", "TMP", "TEMP", "HERMES_SCRATCH_DIR"):
@@ -25,17 +39,25 @@ def test_long_scratch_tmpdir_still_hosts_electrons_singleton_socket(monkeypatch,
env = _launch_env(tmp_path)
- # Chromium's ProcessSingleton binds $TMPDIR/scoped_dirXXXXXX/SingletonSocket.
- 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"))
+ _bind_singleton_socket(env["TMPDIR"])
+ # Electron main restores this for the backend, so the agent keeps writing to scratch.
+ assert env["HERMES_DESKTOP_TMPDIR"] == str(scratch)
@pytest.mark.platforms("linux")
-def test_short_caller_tmpdir_is_passed_through(monkeypatch, tmp_path):
+def test_tmpdir_that_fits_the_socket_budget_is_passed_through(monkeypatch, tmp_path):
from hermes_constants import socket_safe_tmpdir
- with tempfile.TemporaryDirectory(prefix="dt-", dir=socket_safe_tmpdir()) as directory:
- monkeypatch.setenv("TMPDIR", directory)
+ with tempfile.TemporaryDirectory(prefix="dt-", dir=socket_safe_tmpdir()) as root:
+ # Longest TMPDIR whose scoped_dirXXXXXX/SingletonSocket still fits sun_path.
+ tmpdir = os.path.join(root, "x" * (74 - len(root) - 1))
+ os.mkdir(tmpdir)
+ assert len(os.fsencode(tmpdir)) == 74
+ _bind_singleton_socket(tmpdir)
+ monkeypatch.setenv("TMPDIR", tmpdir)
monkeypatch.setattr(tempfile, "tempdir", None)
- assert _launch_env(tmp_path)["TMPDIR"] == directory
+
+ env = _launch_env(tmp_path)
+
+ assert env["TMPDIR"] == tmpdir
+ assert "HERMES_DESKTOP_TMPDIR" not in env