mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
ssh/tailssh: retain the latest session window size
Drain any stale buffered size before sending the latest window update. The adapter is the only sender to a one-slot channel, so the final resize is retained even if the consumer races with the drain.
Cover late and slow readers over a real SSH session with deterministic synctest synchronization. Keep the final two resizes unread to exercise replacement of the buffered size.
Updates tailscale/corp#50380
Signed-off-by: James Tucker <[email protected]>
Change-Id: I8c077114a6d4765c4208d42a22f384c82f73f75c
(cherry picked from commit 123232260e)
This commit is contained in:
1 parent
19523b1c2b
commit
028f06144b
2 files changed
+99
-4
No files matched your search
@@ -469,6 +469,94 @@ func TestListenSSHWindowChangesWithoutPty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestListenSSHWindowChangesLatest tests that a slow or late reader sees
|
||||
// the last window size, even after earlier changes filled the buffer.
|
||||
func TestListenSSHWindowChangesLatest(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
slowReader bool
|
||||
}{
|
||||
{"late-reader", false},
|
||||
{"slow-reader", true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
sl, rawLn := startTestSSHListener(t, nil)
|
||||
defer sl.Close()
|
||||
sc, client := startTestSSHClient(t, rawLn)
|
||||
defer sc.Close()
|
||||
defer client.Close()
|
||||
|
||||
sess, err := client.NewSession()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sess.Close()
|
||||
if err := sess.RequestPty("xterm-256color", 24, 80, nil); err != nil {
|
||||
t.Fatalf("RequestPty: %v", err)
|
||||
}
|
||||
if err := sess.Shell(); err != nil {
|
||||
t.Fatalf("Shell: %v", err)
|
||||
}
|
||||
conn, err := sl.Accept()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := conn.(*Session)
|
||||
defer s.Close()
|
||||
pty, winCh, ok := s.Pty()
|
||||
if !ok || pty.Window.Width != 80 || pty.Window.Height != 24 {
|
||||
t.Fatalf("Pty = %+v, ok = %v; want 80x24", pty, ok)
|
||||
}
|
||||
|
||||
// Let the pump buffer the initial size without a reader.
|
||||
synctest.Wait()
|
||||
if got := len(winCh); got != 1 {
|
||||
t.Fatalf("initial buffered window count = %d; want 1", got)
|
||||
}
|
||||
if tt.slowReader {
|
||||
if got := <-winCh; got.Width != 80 || got.Height != 24 {
|
||||
t.Fatalf("initial window = %+v; want 80x24", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A reply confirms gliderssh processed each resize; Wait
|
||||
// confirms the adapter drained its upstream notification.
|
||||
resize := func(width, height uint32) {
|
||||
msg := struct{ Columns, Rows, Width, Height uint32 }{
|
||||
width, height, width * 8, height * 8,
|
||||
}
|
||||
ok, err := sess.SendRequest("window-change", true, testssh.Marshal(&msg))
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("window-change %dx%d: ok=%v, err=%v", width, height, ok, err)
|
||||
}
|
||||
synctest.Wait()
|
||||
_, upstream, _ := s.sess.Pty()
|
||||
if got := len(upstream); got != 0 {
|
||||
t.Fatalf("upstream buffered windows = %d; want 0", got)
|
||||
}
|
||||
}
|
||||
if tt.slowReader {
|
||||
resize(100, 30)
|
||||
if got := <-winCh; got.Width != 100 || got.Height != 30 {
|
||||
t.Fatalf("first resize = %+v; want 100x30", got)
|
||||
}
|
||||
// Do not receive from winCh between these final two
|
||||
// resizes: 120x40 must replace the buffered 110x35.
|
||||
resize(110, 35)
|
||||
}
|
||||
resize(120, 40)
|
||||
|
||||
// Start reading only after the final change has been drained.
|
||||
// No later resize can mask a stale value in the buffer.
|
||||
if got := <-winCh; got.Width != 120 || got.Height != 40 {
|
||||
t.Fatalf("last window = %+v; want 120x40", got)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListenSSHListenerCloseClosesSessions tests that closing the
|
||||
// listener ends the sessions it accepted, so they do not outlive the
|
||||
// listener.
|
||||
|
||||
+11
-4
@@ -83,8 +83,8 @@ func newSession(sess gliderssh.Session, peer peerIdentity, done chan struct{}) *
|
||||
// Start draining window changes now, even when the application
|
||||
// never calls Pty. gliderssh writes window changes from the session
|
||||
// request loop, so an undrained channel blocks the session's
|
||||
// requests. Pty returns winCh. Window changes are dropped when the
|
||||
// application is not reading it.
|
||||
// requests. Pty returns winCh, which retains the latest size
|
||||
// when the application is not reading it.
|
||||
if gPty, gWinCh, ok := sess.Pty(); ok {
|
||||
s.pty = &Pty{
|
||||
Term: gPty.Term,
|
||||
@@ -109,6 +109,13 @@ func newSession(sess gliderssh.Session, peer peerIdentity, done chan struct{}) *
|
||||
WidthPixels: gw.WidthPixels,
|
||||
HeightPixels: gw.HeightPixels,
|
||||
}
|
||||
// Replace any stale size. This is the only sender to the
|
||||
// one-slot channel, so draining leaves room for the latest
|
||||
// size even if the consumer races with the receive.
|
||||
select {
|
||||
case <-s.winCh:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case s.winCh <- w:
|
||||
default:
|
||||
@@ -190,8 +197,8 @@ func (s *Session) Subsystem() string {
|
||||
}
|
||||
|
||||
// Pty returns PTY information, a channel of window size changes, and
|
||||
// whether a PTY was requested. Window changes are dropped when the
|
||||
// returned channel is not being read.
|
||||
// whether a PTY was requested. When the returned channel is not being
|
||||
// read, it retains the latest window size.
|
||||
func (s *Session) Pty() (_ Pty, _ <-chan Window, ok bool) {
|
||||
if s.pty == nil {
|
||||
return Pty{}, nil, false
|
||||
|
||||
Reference in new issue
Block a user