Merge pull request #14 from github/ssh-close-workaround

add workaround for ssh.channel.Close EOF
This commit is contained in:
Alan Donovan 2021-09-03 15:13:20 -04:00 committed by GitHub
commit 5c26d1488f
2 changed files with 10 additions and 9 deletions

View file

@ -26,13 +26,6 @@ func NewPortForwarder(session *Session, name string, remotePort int) *PortForwar
}
}
// ListenTCP calls listen on the chosen local TCP port. Zero picks an
// arbitrary port. It is provided for the convenience of callers of
// ForwardToListener.
func ListenTCP(port int) (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf(":%d", port))
}
// ForwardToListener forwards traffic between the container's remote
// port and a local port, which must already be listening for
// connections. (Accepting a listener rather than a port number avoids
@ -121,7 +114,15 @@ func (fwd *PortForwarder) handleConnection(ctx context.Context, id channelID, co
if err != nil {
return fmt.Errorf("error opening streaming channel for new connection: %v", err)
}
defer safeClose(channel, &err)
// Ideally we would call safeClose again, but (*ssh.channel).Close
// appears to have a bug that causes it return io.EOF spuriously
// if its peer closed first; see github.com/golang/go/issues/38115.
defer func() {
closeErr := channel.Close()
if err == nil && closeErr != io.EOF {
err = closeErr
}
}()
errs := make(chan error, 2)
copyConn := func(w io.Writer, r io.Reader) {

View file

@ -46,7 +46,7 @@ func TestPortForwarderStart(t *testing.T) {
}
defer testServer.Close()
listen, err := ListenTCP(8000) // local port
listen, err := net.Listen("tcp", ":8000")
if err != nil {
t.Fatal(err)
}