greymd / tmux-xpanes

Awesome tmux-based terminal divider
MIT License
1.96k stars 61 forks source link

Temporal session file is deleted after detaching #135

Closed greymd closed 4 years ago

greymd commented 4 years ago

Reported in https://github.com/greymd/tmux-xpanes/issues/114#issuecomment-561247362

The session file is automatically deleted when tmux session is detached. I imagine it must be helpful for users if the session file is kept in that case. Because, in a common case, the user does not recognize the possibility that the session is better to be kept alive when opening the new windows.

How to reproduce

(1) Create session from outside of tmux session.

$ xpanes --ssh localhost

(2) Detach the session (ctrl+b and d by default)

(3) Session is alive but session file is deleted.

$ ps alx | grep tmux
1  1000 20005     1  20   0  27648  4032 poll_s Ss   ?          0:00 tmux -S /home/ubuntu/.cache/xpanes/socket.19931 new-session -s xpanes-19931 -n tmp-19931 -d /usr/bin/xpanes '--ssh' '-c' 'ssh -o StrictHostKeyChecking=no {} '  '--' 'localhost'

$ ls /home/ubuntu/.cache/xpanes/socket.19931
ls: cannot access '/home/ubuntu/.cache/xpanes/socket.19931': No such file or directory

Technical reason

This is caused by this trap. https://github.com/greymd/tmux-xpanes/blob/dda18b9283d49d757eb525161fe866fc61ef6e94/bin/xpanes#L11

$ bash -x xpanes -c 'ssh {}' localhost localhost
+ readonly 'XP_SHELL=/usr/bin/env bash'
...
+ tmux -S /home/ubuntu/.cache/xpanes/socket.9635 attach-session -t xpanes-9635
[detached (from session xpanes-9635)]
+ exit 0
+ rm -f '/home/ubuntu/.cache/xpanes/__xpns_*9635'
+ rm -f /home/ubuntu/.cache/xpanes/socket.9635

The temporal session file ~/.cache/xpanes/socket.<PID> is supposed to be used to recover the session. So, the file may be kept in case of network disconnection or any other troubles. On the other hand, the file is going to be deleted if the tmux session is explicitly exited because any recovery won't be required.

However, tmux also makes its own process exit when detaching (ctrl+b d). As far as investigated, there is no way for xpanes to confirm whether the session is "explicitly exited" or "detached". This is the reason why xpanes deletes the session file.

greymd commented 4 years ago

Fixed in v4.1.2