Client cannot reach the tmux server socket: stale socket versus exit-empty policy
0 reputation · 29 May 2024, 02:00 UTC
0 reputation · 29 May 2024, 02:00 UTC
tmux is a client/server program. Each client command connects to a long-lived per-user server over a local Unix domain socket, by default inside a tmux-<uid> directory under the temporary directory and overridable with TMUX_TMPDIR. No name resolution and no certificate validation occur on that path, so resolver and TLS diagnostics do not apply to a client that cannot reach the server.
The failure condition is a client that finds no usable server socket: none is running, or the socket is stale, inaccessible, or owned by another user. A different TMUX_TMPDIR between shells yields a separate server, which can resemble a vanished session. Exact message wording and exit status vary by release, so the installed manual page is the reference.
The unresolved decision is server lifetime. The exit-empty option governs whether the server exits once the last session ends, and that choice changes startup latency and how an absent-server condition surfaces in scripts.
exit-empty off?TMUX_TMPDIR values?30275 reputation · 29 May 2024, 11:21 UTC
When a tmux client says it can’t reach the server socket, the two scenarios are:
exit‑empty is on – the server exits as soon as the last session ends, so the socket file is removed. A new client will automatically start a fresh server, which is normal.Follow the steps below to determine which case applies and to recover.
Locate the socket path
```bash
# Default path if TMUX_TMPDIR is unset
SOCKET_DIR=${TMUX_TMPDIR:-$TMPDIR}/tmux-$(id -u)
SOCKET=$(ls -1 $SOCKET_DIR | head -n1) # usually a single file
SOCKET_PATH=$SOCKET_DIR/$SOCKET
```
Check if a tmux server is running
```bash
pgrep -u $(id -u) -x tmux
```
If this returns nothing, no server process is alive.
Verify socket liveness
```bash
# Is the file a socket?
if [ -S "$SOCKET_PATH" ]; then
echo "Socket file exists"
else
echo "No socket file"
fi
# Is any process listening on it?
ss -lx | grep $(basename "$SOCKET_PATH")
```
If ss shows no listener, the socket is stale.
Check the exit‑empty setting
```bash
tmux show-options -gs exit-empty
```
Output will be on or off. If it is on and the socket file is missing, the absence is expected.
Clean up a stale socket
Only do this after confirming there is no running tmux process:
```bash
rm -f "$SOCKET_PATH"
```
Then run a tmux command such as tmux ls – a new server will start automatically.
Verify behavior after detaching the last session
Start a session, detach it, and run tmux ls again. If the server persists, exit-empty is off; if the session list disappears, it is on.
Consider TMUX_TMPDIR differences
Running under a different TMUX_TMPDIR or a different UID creates a separate socket namespace. Ensure all related processes share the same TMUX_TMPDIR value and UID.
exit-empty is on, scripts that expect a persistent server will see a brief latency spike each time a new server is spawned.TMUX_TMPDIR values will never see each other’s sessions unless the same namespace is used.To confirm the tmux version you’re using, run tmux -V. The exact wording of the “cannot connect” message and the default value of exit-empty can vary slightly between releases.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.