claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Cross-session inbox goes permanently deaf when the /tmp/cc-socks-<uid> fallback directory is removed (systemd-tmpfiles boot sweep race)

Open philyoast opened this issue 2 months ago • 2 comments

Version: 2.1.283 (Linux, WSL2 with systemd; VS Code extension and CLI)

What happens. When $XDG_RUNTIME_DIR/cc-socks can't be used at startup, Claude Code falls back to /tmp/cc-socks-<uid>/<pid>.sock (added in 2.1.248). If that directory is later unlinked, the session keeps listening on a pathless socket. Its ~/.claude/sessions/<pid>.json keeps advertising the dead messagingSocketPath, so:

  • other sessions' ListAgents omit it, and SendMessage to it fails with "not reachable";
  • its own outbound messages carry from="uds:/tmp/cc-socks-<uid>/<pid>.sock", so replies fail;
  • nothing re-binds or warns, and the session is one-way until restarted.

How it happens in practice. On WSL2 with systemd, VS Code reconnects and the extension spawns sessions during boot. At that point /run/user/<uid> doesn't exist yet (user-runtime-dir@ starts late), so the fallback is taken. Meanwhile systemd-tmpfiles-setup.service is still running the D /tmp boot sweep, which deletes /tmp/cc-socks-<uid>. The journal shows the sweep running 08:24:29–08:29:40, the sessions starting at 08:25:12 and 08:26:57, and /run/user/1000 appearing at 08:29:41.

Repro (without a reboot).

  1. XDG_RUNTIME_DIR=/nonexistent claude (session A binds /tmp/cc-socks-$UID/…).
  2. rm -rf /tmp/cc-socks-$UID.
  3. From session B, ListAgents: A is missing, and SendMessage to A fails. A's /status still reports messaging as active.

Expected.

  • (a) Periodically verify that the bound socket path still refers to our listener (inode check). If it doesn't, re-bind, preferring the primary $XDG_RUNTIME_DIR directory once it exists, and update sessions/<pid>.json.
  • (b) Or retry the primary directory for a short window at startup before falling back.
  • (c) At minimum, surface a visible warning / /status degraded cause when the inbox path disappears.

Workaround. Restart the session after boot, or add x /tmp/cc-socks-* to /etc/tmpfiles.d/.

Related (different failure modes): #85497 (session never binds its socket), #91223 (socket directory squatting).

philyoast avatar Sep 26 '26 20:09 philyoast

Agree — once the fallback directory is unlinked the listener can stay open on a pathless inode while sessions/<pid>.json still advertises the dead path, so peer inbox looks permanently deaf until restart.

Validating "listener still open" is not enough. The check that matches the failure mode is:

  1. Path ↔ fd identity. lstat(advertisedPath) vs fstat(listener_fd) (dev/ino). Treat mismatch or ENOENT as dead advertisement.
  2. Atomic rebind before registry rewrite. Prefer $XDG_RUNTIME_DIR/cc-socks once it exists; otherwise recreate the /tmp/cc-socks-<uid> fallback. Only after the new bind succeeds, update messagingSocketPath in the session registry (and any outbound from= identity).
  3. Regression. With a live session: delete the bound directory, create $XDG_RUNTIME_DIR, then assert inbound ListAgents / SendMessage recover without restart and the registry points at the new socket. Optional: short startup retry of the primary runtime dir before falling back (OP's (b)) so boot races take the stable path more often.

That also covers the WSL2 systemd-tmpfiles D /tmp sweep race in the report. Happy to trim to the inode check + rebind if maintainers want a narrower first patch.

stonianua avatar Sep 26 '26 21:09 stonianua

Still reproducible on 2.1.322 (Linux, Debian in a Proxmox LXC; about 30 Claude Code sessions hosted by Claude Desktop's Code tab, all run by one uid). It affects the primary directory too, not only the /tmp fallback.

What happened. A buggy user script ran rm -rf $D with D empty-prefixed (D=$2/run, no $2), so it deleted /run, including /run/user/1000/cc-socks/. Linger=yes and the user manager kept running; no reboot happened.

Result. All ~30 sessions kept running but went one-way at once. Each still listened on a pathless socket while ~/.claude/sessions/<pid>.json advertised the dead messagingSocketPath.

  • SendMessage to them failed ("no sign of starting within 20s" / not reachable).
  • Their own outbound messages carried a from that nobody could answer.
  • Nothing in /status or the UI showed degraded messaging. We only noticed hours later, when peers' replies were missing.

Recovery (current workaround). Stop the idle session process; the next message wakes it, it binds a new socket and keeps its history. Side effect: stopping the process kills its background jobs (in our case, file-lock renewal loops), so every session needs a manual check afterwards.

Detection we added locally. A 5-minute timer compares each sessions/<pid>.json messagingSocketPath with the live listener (lstat of the path vs the socket inode in /proc/<pid>/fd), and flags the sessions whose path is gone.

+1 for proposal (a): periodic path↔fd identity check, re-bind on mismatch, and update sessions/<pid>.json. At minimum, proposal (c): a visible warning when the inbox path disappears. With many concurrent sessions, one directory deletion silently cuts all of them off.

MikeBrigante avatar Nov 15 '26 23:11 MikeBrigante