tmux-plugins / tmux-resurrect

Persists tmux environment across system restarts.
MIT License
11.39k stars 423 forks source link

Bug: Re-saving session restored by tmux-resurrect corrupts resurrect session file, and makes programs not launch #392

Open poetaman opened 3 years ago

poetaman commented 3 years ago

This one was difficult to debug, but it's simple to reproduce.

Steps:

  1. Kill tmux-server >>tmux kill-server, and restart tmux >>tmux.
  2. Create a vertical split so there are two panes. At this point the focus will be in right pane.
  3. Move the focus to the left pane by clicking mouse on the left pane.
  4. Launch >>htop or other program (I also tried nvim) in left pane.
  5. Save resurrect session file by pressing <prefix>Ctrl-s.
  6. Kill tmux-server >>tmux kill-server, and restart tmux >>tmux.
  7. Restore the session saved in step-5 by pressing <prefix>Ctrl-r. The session will restore correctly this time, with htop launched in left pane.
  8. Again save resurrect session file by pressing <prefix>Ctrl-s.
  9. Kill tmux-server >>tmux kill-server, and restart tmux >>tmux.
  10. Restore the session saved in step-8 by pressing <prefix>Ctrl-r.

This time htop won't launch automatically in left pane!

I compared the files saved by tmux-resurrect in step 5, step 8. They are different, and the difference introduced is the cause of this bug. Here are the contents of the file ~/.tmux/resurrect/last after step-5, and step-8:

After step-5:

pane    0   1   :zsh... 1   :*  1   :/Users/reportaman/.tmux/plugins/tmux-resurrect 1   htop    :htop
-zsh
pane    0   1   :zsh... 1   :*  2   :/Users/reportaman/.tmux/plugins/tmux-resurrect 0   zsh :-zsh
window  0   1   1   :*  2280,288x76,0,0{144x76,0,0,0,143x76,145,0,1}
state   0   

After step-8:

pane    0   1   :zsh... 1   :*  1   :/Users/reportaman/.tmux/plugins/tmux-resurrect 1   htop    :-zsh
htop
pane    0   1   :zsh... 1   :*  2   :/Users/reportaman/.tmux/plugins/tmux-resurrect 0   zsh :-zsh
window  0   1   1   :*  a281,288x76,0,0{144x76,0,0,1,143x76,145,0,2}
state   0   

There seem to be two problems:

1) On first and second line, the programs htop & zsh got swapped.

2) On line four 2280 became a281, and 145,0,1} became 145,0,2}. Not sure what this is.

There seem to be old bugs that complain about programs not launching correctly, in this one I give simple steps to reproduce & source of the problem. Hopefully this will get resolved sooner than later :) it will make tmux-resurrect reliable!

bruno- commented 3 years ago

I followed the repro steps and I can't reproduce the bug. However, I do believe you're getting a bug, but it's probably due to something else in your (and other users') environment.

I propose you try to figure it out. PR with a fix would be welcome!

poetaman commented 3 years ago

@bruno- Are you setting any variable related to tmux-resurrect in tmux.conf file? I had just one, and commenting that also doesn't do anything different.

Perhaps you have more elaborate settings related to tmux-resurrect? Can you give it a try by disabling them if thats true? Btw, the bug is not reproducible if the process is started in right pane instead of left.

set -g @resurrect-processes ' \
    "~nvim->nvim" \
'
Some system information:
OS: macOS 11.4 BigSur
Architecture: ARM-64
Terminal: iTerm2 or Alacritty
Shell zsh 5.8
Bash version: GNU bash, version 5.1.8(1)-release (aarch64-apple-darwin20.4.0)

Also, can you post your entire tmux.conf file? It would also help to understand the format of resurrect file. Like what's the line with -zsh (on line 2) (I guess that is the parent process).

poetaman commented 3 years ago

@bruno- I debugged further, and this is what is happening:

Firstly, I am using the default save strategy, "ps". I am not sure what save strategy you are using?

The problem seems to stem from the following statement in file https://github.com/tmux-plugins/tmux-resurrect/blob/master/save_command_strategies/ps.sh

    ps -ao "ppid command" |
        sed "s/^ *//" |
        grep "^${PANE_PID}" |
        cut -d' ' -f2-

During first resurrect save it returns

htop
-zsh

After reloading resurrect session, and upon resurrect save again it returns:

-zsh
htop

The order is reversed, any thoughts of what this means?

poetaman commented 3 years ago

@bruno- I think I fixed it! Adding a final piped grep to the ps command grep -v -e "^-.*" resolves the problem. It removed lines for login shell -zsh.

    ps -ao "ppid command" |
        sed "s/^ *//" |
        grep "^${PANE_PID}" |
        cut -d' ' -f2- |
        grep -v -e "^-.*"

Now every time the resurrect session file is saved like this:

pane    0   1   :zsh... 1   :*  1   :/Users/amanmehra   1   htop    :htop
pane    0   1   :zsh... 1   :*  2   :/Users/amanmehra   0   zsh :
window  0   1   1   :*  c920,300x76,0,0{150x76,0,0,1,149x76,151,0,2}
state   0

Please review, and let me know what you think. Then I will submit a PR.

Updated the command above from grep -v -e "-." to grep -v -e "^-.". Can be further refined to just catch login processes like --zsh, -bash, etc.