Skip to content

Proposal: restart-server, replacing the server process without losing panes #5693

Open

Description

@fiorix

I have a working restart-server command that moves a running server to a new tmux binary without losing any panes. Below is what it does, what happens when the new binary is wrong, and how I plan to send it. If the approach is wrong for tmux, I would prefer to learn about that now instead of during the PRs.

Problem: Upgrading tmux means killing the server, so the pane ptys close and the programs in them exit. There is no way to move a running server to a new binary.

What my patch does: restart-server makes the server execute its binary again, in place:

  • The server resolves its binary by path, so after an upgrade restart-server runs the new one.
  • The pid does not change, so the programs in the panes stay children of the server and can still be waited for.
  • The pane pty masters and the listening socket stay open across the exec. tmux already uses closefrom() in children, so clearing close-on-exec on those few descriptors does not leak them into panes.
  • The server's state is written to a versioned checkpoint, an unlinked temporary file beside the socket, which is passed across the exec. It covers sessions, windows, panes, layouts, options, environment and paste buffers, plus each pane's screen, scrollback, parser state and palette.
  • The new image rebuilds everything into a private graph and publishes it in one step.
  • Clients are detached and must attach again. pipe-pane stops and copy mode is left.

When the new binary is wrong: There is no way back once the exec has happened, so the server checks the new binary first:

  • Before committing, the server runs the new binary as a child with the checkpoint on its standard input, and the child decodes it. If that fails, restart-server fails with the child's reason and the server is unchanged. A tmux from before this change exits with an error when run this way, so a restart into an older binary is refused too.
  • If the check passes but the exec then fails, the server carries on with the same sessions, windows and panes. Its clients have been detached by then and attach again.
  • The check covers decoding but not the rebuild. If the rebuild fails after the exec, the panes are lost, as they would be with kill-server.

Regress covers the first two cases.

Tested: I tested on Linux only (Ubuntu 26.04), with and without --enable-systemd. Regress gives the same results as master c067a0b, where two copy-mode tests fail here as well, and the five new restart tests pass. By hand, I replaced the binary on disk with a different build and ran restart-server. The server came back on the new binary with the same pid, the pane program was still running with its scrollback, a user option kept its value, and a control client got %exit. With tmux 3.7b on disk instead, restart-server failed after 21 ms and the server carried on unchanged.

Cost:

  • About 16,200 lines in new files: the checkpoint format is 13,600, the rebuild 700, the transport 900 and the command 1,000. About 2,100 lines go into existing files, mostly entry points in window.c, session.c, layout.c and input.c, plus 670 lines of tests in five scripts.
  • Maintenance: the checkpoint format mirrors what it saves. A new window, pane, screen or option field that should survive a restart needs a matching change in the format, and a change to how windows or panes are constructed needs one in the rebuild. The recent damage list was one such case.

Choices I have made, which I will keep unless you want them different:

  • One design on every platform. The check needs only fork(), execv(), mkstemp() and pread(), so there are no compile-time variants and no new HAVE_ macros. I have only tested it on Linux.
  • The checkpoint is its own binary format, because nothing in tmux serializes screen, grid or parser state today. Each record is marked required or optional, and a reader skips optional records it does not know, so new fields can be added without changing the format's major version. A checkpoint the new binary cannot read is refused by the check.
  • I also have a backend that hands the descriptors to systemd's descriptor store, for a server run as a systemd service. It builds on this, so I will send it separately afterwards if you are interested in it for systemd users.
  • The patch goes in one PR of seven commits, in this order, and each one builds without warnings:
    1. A versioned format for terminal screen state.
    2. A versioned format for session, window and pane state.
    3. Rebuilding the live server from a decoded state.
    4. The transport that carries the descriptors and the checkpoint to the new image.
    5. The restart-server command.
    6. Tests.
    7. The manual page.

If none of that is a problem, I will open the PR as described.

Activity

  1. nicm commented on Oct 6, 2026

    @nicm
    Member

    My major problem with this is adding a state file for this single purpose.

    A requirement that comes up frequently is restoring sessions - both if tmux is started entirely new, for templating, or say I just want to clone session A as session B.

    The bulk of the state - names, sizes and positions, grid content and history, options, etc - will be the same whether I want to start a new server with my old sessions, or do an in place upgrade to a new server. The only differences are the file descriptors.

    So I don't think we should add a way to save and restore state for upgrades, unless it can also be used for other things where we might want to save and restore the state. I don't know what format you have used for your state but it should definitely be JSON.

    I would like this to be built out in steps starting with say saving and restoring or cloning a window or session, then the entire server state, and then in place upgrade could be built on top of that.

    Someone did start on this here #5585 and they seem to be active but I have not had much of a look at it yet.

  2. fiorix commented on Oct 6, 2026

    @fiorix
    Author

    Cool, let me get myself familiar with this other issue and how it's being handled before anything else. Cheers!

  3. nicm commented on Oct 6, 2026

    @nicm
    Member

    OK thanks, this will be a big chunk of work so I think it will need to be done in stages to make sure everything makes sense.

    I am OK with ending up with reexec but I think the save/restore parts should definitely be reusable.

    I useful first step would be able to do something like tmux save-state -w -|tmux load-state -w - and end up with a new window the same as the current one, except without the running programs.

    Then the same for sessions, and once we have a way to save and restore an entire server, it should not be a big step to do save/reexec/load/reassign file descriptors for upgrade.

  4. fiorix commented on Oct 7, 2026

    @fiorix
    Author

    The first step is up as #5698: save-state -w and load-state -w, in four commits (window structure, pane contents, options, and the rest of the terminal state). Sessions are next, on the same JSON.

    Agents found these other bugs along the way. The PR does not touch them, and each one is reproduced on master (052c54f):

    1. respawn-pane -k -E destroys the pane it has just respawned. The empty path in spawn_pane (spawn.c:459) skips the fork but leaves wp->pid as the pid of the killed process. When that process is reaped, server_child_exited (server.c:495) finds the new empty pane by that pid and treats it as exited. If it is the only pane in the only session, the server exits:

      tmux -Ltest new -d; tmux -Ltest respawn-pane -k -E; tmux -Ltest ls
      # no server running on .../test
    2. A pane with no process leaks its bufferevent when it is destroyed. window_pane_set_event (window.c:1689) creates one even when wp->fd is -1, but window_pane_destroy (window.c:1600) frees it only when the fd is open. Under valgrind, new-window '' followed by kill-window reports the bufferevent allocated in window_pane_set_event as definitely lost.

    3. load-buffer -w keeps a reference to the target client when the file cannot be read or is empty. cmd_load_buffer_done calls server_client_unref only inside the bsize != 0 branch (cmd-load-buffer.c:81). In a tmux -v server log, a control client that ran load-buffer -w /nonexistent or load-buffer -w /dev/null is lost but never freed, while one that loaded a real file is freed.

    I can send a small PR for each of these unless you prefer to fix them yourself.

  5. nicm commented on Oct 7, 2026

    @nicm
    Member

    You can make one PR for all 3 if you like or show a diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions