Skip to content

fix: Guard against conn leak after server close #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Server struct {
conns map[*gossh.ServerConn]struct{}
connWg sync.WaitGroup
doneChan chan struct{}
closedAt time.Time
}

func (srv *Server) ensureHostSigner() error {
Expand Down Expand Up @@ -180,6 +181,8 @@ func (srv *Server) Close() error {
srv.mu.Lock()
defer srv.mu.Unlock()

srv.closedAt = time.Now()

srv.closeDoneChanLocked()
err := srv.closeListenersLocked()
for c := range srv.conns {
Expand Down Expand Up @@ -260,6 +263,17 @@ func (srv *Server) Serve(l net.Listener) error {
}

func (srv *Server) HandleConn(newConn net.Conn) {
// Best effort, track start time in case the server is closed
// between here and trackConn. If trackConn is called after the
// server is closed, this connection would leak.
//
// A better approach would be to have a clearer signal between the
// server being "started" and "closed", but that would require a
// larger refactor or change in logic.
srv.mu.RLock()
start := time.Now()
srv.mu.RUnlock()

ctx, cancel := newContext(srv)
if srv.ConnCallback != nil {
cbConn := srv.ConnCallback(ctx, newConn)
Expand Down Expand Up @@ -289,6 +303,14 @@ func (srv *Server) HandleConn(newConn net.Conn) {
srv.trackConn(sshConn, true)
defer srv.trackConn(sshConn, false)

srv.mu.RLock()
if srv.closedAt.After(start) {
srv.mu.RUnlock()
sshConn.Close()
return
}
srv.mu.RUnlock()

ctx.SetValue(ContextKeyConn, sshConn)
applyConnMetadata(ctx, sshConn)
//go gossh.DiscardRequests(reqs)
Expand Down
103 changes: 103 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"testing"
"time"

gossh "golang.org/x/crypto/ssh"
)

func TestAddHostKey(t *testing.T) {
Expand Down Expand Up @@ -124,3 +126,104 @@ func TestServerClose(t *testing.T) {
return
}
}

func TestServerClose_ConnectionLeak(t *testing.T) {
Copy link
Member Author

@mafredri mafredri Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is a bit finicky, but without the fix it will usually time out (as a proof of the issue being fixed).

l := newLocalListener()
s := &Server{
Handler: func(s Session) {
time.Sleep(5 * time.Second)
},
}
go func() {
err := s.Serve(l)
if err != nil && err != ErrServerClosed {
t.Error(err)
}
}()

clientDoneChan := make(chan struct{})
closeDoneChan := make(chan struct{})

num := 3
ch := make(chan struct{}, num)
go func() {
for i := 0; i < num; i++ {
<-ch
}
close(clientDoneChan)
}()
prepare := make(chan struct{}, num)
go func() {
for i := 0; i < num; i++ {
go func() {
defer func() {
ch <- struct{}{}
}()
sess, _, cleanup, err := newClientSession2(t, l.Addr().String(), nil)
prepare <- struct{}{}
if err != nil {
t.Log(err)
return
}
defer cleanup()
if err := sess.Run(""); err != nil && err != io.EOF {
t.Log(err)
}
}()
}
}()

go func() {
for i := 0; i < num-1; i++ {
<-prepare
}
err := s.Close()
if err != nil {
t.Error(err)
}
close(closeDoneChan)
}()

timeout := time.After(1000 * time.Millisecond)
select {
case <-timeout:
t.Error("timeout")
return
case <-closeDoneChan:
}
select {
case <-timeout:
t.Error("timeout")
return
case <-clientDoneChan:
}
}

func newClientSession2(t *testing.T, addr string, config *gossh.ClientConfig) (*gossh.Session, *gossh.Client, func(), error) {
t.Helper()

if config == nil {
config = &gossh.ClientConfig{
User: "testuser",
Auth: []gossh.AuthMethod{
gossh.Password("testpass"),
},
}
}
if config.HostKeyCallback == nil {
config.HostKeyCallback = gossh.InsecureIgnoreHostKey()
}
client, err := gossh.Dial("tcp", addr, config)
if err != nil {
return nil, nil, nil, err
}
session, err := client.NewSession()
if err != nil {
client.Close()
return nil, nil, nil, err
}
return session, client, func() {
session.Close()
client.Close()
}, nil
}
6 changes: 3 additions & 3 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ func TestPty(t *testing.T) {

func TestPtyResize(t *testing.T) {
t.Parallel()
winch0 := Window{40, 80}
winch1 := Window{80, 160}
winch2 := Window{20, 40}
winch0 := Window{Width: 40, Height: 80}
winch1 := Window{Width: 80, Height: 160}
winch2 := Window{Width: 20, Height: 40}
winches := make(chan Window)
done := make(chan bool)
session, _, cleanup := newTestSession(t, &Server{
Expand Down