SSH handshake in goroutine and defer close (#1640)
Some checks failed
gofmt / Run gofmt (push) Failing after 2s
smoke-extra / Run extra smoke tests (push) Failing after 3s
smoke / Run multi node smoke test (push) Failing after 2s
Build and test / Build all and test on ubuntu-linux (push) Failing after 3s
Build and test / Build and test on linux with boringcrypto (push) Failing after 2s
Build and test / Build and test on linux with pkcs11 (push) Failing after 2s
Build and test / Build and test on macos-latest (push) Has been cancelled
Build and test / Build and test on windows-latest (push) Has been cancelled

* SSH handshake in goroutine and defer close
This commit is contained in:
brad-defined
2026-04-23 14:53:52 -04:00
committed by GitHub
parent db9218b0be
commit db85d61c23
2 changed files with 47 additions and 52 deletions

View File

@@ -17,15 +17,15 @@ type session struct {
c *ssh.ServerConn
term *term.Terminal
commands *radix.Tree
exitChan chan bool
cancel func()
}
func NewSession(commands *radix.Tree, conn *ssh.ServerConn, chans <-chan ssh.NewChannel, l *logrus.Entry) *session {
func NewSession(commands *radix.Tree, conn *ssh.ServerConn, chans <-chan ssh.NewChannel, cancel func(), l *logrus.Entry) *session {
s := &session{
commands: radix.NewFromMap(commands.ToMap()),
l: l,
c: conn,
exitChan: make(chan bool),
cancel: cancel,
}
s.commands.Insert("logout", &Command{
@@ -42,6 +42,7 @@ func NewSession(commands *radix.Tree, conn *ssh.ServerConn, chans <-chan ssh.New
}
func (s *session) handleChannels(chans <-chan ssh.NewChannel) {
defer s.Close()
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
s.l.WithField("sshChannelType", newChannel.ChannelType()).Error("unknown channel type")
@@ -100,7 +101,6 @@ func (s *session) handleRequests(in <-chan *ssh.Request, channel ssh.Channel) {
if err != nil {
s.l.WithError(err).Info("Error handling ssh session requests")
s.Close()
return
}
}
@@ -123,12 +123,11 @@ func (s *session) createTerm(channel ssh.Channel) *term.Terminal {
return "", 0, false
}
go s.handleInput(channel)
go s.handleInput()
return term
}
func (s *session) handleInput(channel ssh.Channel) {
defer s.Close()
func (s *session) handleInput() {
w := &stringWriter{w: s.term}
for {
line, err := s.term.ReadLine()
@@ -170,10 +169,9 @@ func (s *session) dispatchCommand(line string, w StringWriter) {
}
_ = execCommand(c, args[1:], w)
return
}
func (s *session) Close() {
s.c.Close()
s.exitChan <- true
s.cancel()
}