ssh: defer channel window adjustment

Sending a window adjustment after every read is unnecessarily chatty,
especially with a series of small reads like with TTY interactions.

Copy OpenSSH's logic for deferring these, which seemingly hasn't changed
since 2007. Note that since channelWindowSize and c.maxIncomingPayload
are currently constants here, the two checks could be combined into a
single check for c.myWindow < 2 MiB - 96 KiB (with the current values
of the constants).

Fixes golang/go#57424.

Change-Id: Ifcef5be76fcc3f0b1a6dc396096bed9c50d64f21
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/459915
Reviewed-by: Nicola Murino <nicola.murino@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Nicola Murino <nicola.murino@gmail.com>
Auto-Submit: Nicola Murino <nicola.murino@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Commit-Queue: Nicola Murino <nicola.murino@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Will Mortensen
2023-11-27 17:27:49 +00:00
committed by Gopher Robot
parent b8ffc16e10
commit fdfe1f8531
3 changed files with 108 additions and 11 deletions
+71
View File
@@ -182,6 +182,40 @@ func TestMuxChannelOverflow(t *testing.T) {
}
}
func TestMuxChannelReadUnblock(t *testing.T) {
reader, writer, mux := channelPair(t)
defer reader.Close()
defer writer.Close()
defer mux.Close()
var wg sync.WaitGroup
t.Cleanup(wg.Wait)
wg.Add(1)
go func() {
defer wg.Done()
if _, err := writer.Write(make([]byte, channelWindowSize)); err != nil {
t.Errorf("could not fill window: %v", err)
}
if _, err := writer.Write(make([]byte, 1)); err != nil {
t.Errorf("Write: %v", err)
}
writer.Close()
}()
writer.remoteWin.waitWriterBlocked()
buf := make([]byte, 32768)
for {
_, err := reader.Read(buf)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Read: %v", err)
}
}
}
func TestMuxChannelCloseWriteUnblock(t *testing.T) {
reader, writer, mux := channelPair(t)
defer reader.Close()
@@ -754,6 +788,43 @@ func TestMuxMaxPacketSize(t *testing.T) {
}
}
func TestMuxChannelWindowDeferredUpdates(t *testing.T) {
s, c, mux := channelPair(t)
cTransport := mux.conn.(*memTransport)
defer s.Close()
defer c.Close()
defer mux.Close()
var wg sync.WaitGroup
t.Cleanup(wg.Wait)
data := make([]byte, 1024)
wg.Add(1)
go func() {
defer wg.Done()
_, err := s.Write(data)
if err != nil {
t.Errorf("Write: %v", err)
return
}
}()
cWritesInit := cTransport.getWriteCount()
buf := make([]byte, 1)
for i := 0; i < len(data); i++ {
n, err := c.Read(buf)
if n != len(buf) || err != nil {
t.Fatalf("Read: %v, %v", n, err)
}
}
cWrites := cTransport.getWriteCount() - cWritesInit
// reading 1 KiB should not cause any window updates to be sent, but allow
// for some unexpected writes
if cWrites > 30 {
t.Fatalf("reading 1 KiB from channel caused %v writes", cWrites)
}
}
// Don't ship code with debug=true.
func TestDebug(t *testing.T) {
if debugMux {