diff --git a/internal/rex/rextest/server.go b/internal/rex/rextest/server.go index 3305b5b..d417dfe 100644 --- a/internal/rex/rextest/server.go +++ b/internal/rex/rextest/server.go @@ -21,6 +21,18 @@ type Server struct { Endpoint rex.Endpoint Dir string log *bytes.Buffer + cmd *exec.Cmd +} + +// Pause stops the server's process, as a machine that has gone away +// would: connections stay open, and nothing answers. It carries on when +// the test ends. +func (s *Server) Pause(t testing.TB) { + t.Helper() + if err := s.cmd.Process.Signal(syscall.SIGSTOP); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.cmd.Process.Signal(syscall.SIGCONT) }) } // Start runs a new Rex server with no saved sessions and an empty @@ -64,6 +76,7 @@ func StartArgs(t testing.TB, config string, args ...string) *Server { // The server's terminals must not find the user's server instead. cmd.Env = append(os.Environ(), rex.ServerEnv+"=") cmd.Stdout, cmd.Stderr = s.log, s.log + s.cmd = cmd if err := cmd.Start(); err != nil { t.Fatal(err) } diff --git a/internal/rexterm/pane.go b/internal/rexterm/pane.go index 32b95c1..8e2c8ee 100644 --- a/internal/rexterm/pane.go +++ b/internal/rexterm/pane.go @@ -50,6 +50,7 @@ type State struct { // however fast a program prints, a frame never has to parse it: a frame // holds the terminal, from Sync to Release, only to draw it. type Pane struct { + closeOnce sync.Once conn *rex.Conn ep rex.Endpoint sessionID string @@ -294,14 +295,47 @@ func (p *Pane) HandleEvent(ev rex.Event) { } } -// Close stops showing the block, ending its stream, and frees the terminal. +// closing counts panes whose streams are still ending. +var closing sync.WaitGroup + +// Close stops showing the block. It returns at once: its stream ends, with +// a close handshake that waits for the server, and its terminal is freed, +// in the background, since Close is called while drawing, and a server +// that has gone away answers only when the handshake times out. The pane +// mustn't be used after, but Close may be called again, and does nothing. func (p *Pane) Close() { + p.closeOnce.Do(p.close) +} + +func (p *Pane) close() { p.Release() // the stream may be waiting to apply output p.cancel() - p.wg.Wait() - if p.term != nil { - p.term.Close() - p.term = nil + closing.Add(1) + go func() { + defer closing.Done() + p.wg.Wait() + // The stream is over, so nothing else has the terminal. + if p.term != nil { + p.term.Close() + p.term = nil + } + }() +} + +// WaitClosed waits for the streams of closed panes to end, so that the +// server sees them close rather than drop, or until timeout passes. It +// reports whether they ended. +func WaitClosed(timeout time.Duration) bool { + done := make(chan struct{}) + go func() { + closing.Wait() + close(done) + }() + select { + case <-done: + return true + case <-time.After(timeout): + return false } } diff --git a/internal/rexterm/pane_test.go b/internal/rexterm/pane_test.go index b8174a0..31f14b1 100644 --- a/internal/rexterm/pane_test.go +++ b/internal/rexterm/pane_test.go @@ -176,3 +176,23 @@ func TestOwnership(t *testing.T) { return st.Owned && cols == 80 && rows == 20 && strings.Contains(screen(t, term), "x") }) } + +// TestCloseDoesntWait closes a pane whose server has stopped answering, as +// over a connection that has died. Close is called while drawing, so it +// mustn't wait for the stream's close handshake. +func TestCloseDoesntWait(t *testing.T) { + srv := rextest.Start(t) + c := srv.Dial(t, "100gex-test") + sid, bid := rextest.NewSession(t, c, rextest.RawCat...) + h := open(t, srv, c, sid, bid) + h.until("the snapshot", func(term *libghostty.Terminal, _ rexterm.State) bool { + return term != nil && strings.Contains(screen(t, term), "ready") + }) + + srv.Pause(t) + start := time.Now() + h.pane.Close() + if d := time.Since(start); d > 100*time.Millisecond { + t.Errorf("Close took %v with the server not answering", d) + } +} diff --git a/main.go b/main.go index 6910c30..60cd0a0 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ import ( "100gex/internal/hosts" "100gex/internal/primary" "100gex/internal/rex" + "100gex/internal/rexterm" "100gex/internal/theme" "100gex/internal/ui" "100gex/internal/workspace" @@ -103,6 +104,9 @@ func run(w *app.Window, server, sessionQuery string, opts options) error { // From here the UI owns the server: it follows its events, reconnects // when the connection drops, switches to others, and closes it. u := ui.New(ws, conn, theme.NewShaper(), w.Invalidate) + // Panes' streams end in the background; give them a moment to close + // properly, so the server doesn't keep them for a resume. + defer rexterm.WaitClosed(2 * time.Second) defer u.Close() u.TerminalTheme = terminalTheme u.ShowFPS = opts.showFPS