Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
0.9.26: Fixed race condition on close
Browse files Browse the repository at this point in the history
This release fixes a potential race condition when closing connections.
  • Loading branch information
Janos Pasztor committed Apr 1, 2021
1 parent aafec3f commit 7394c70
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion serverImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *serverImpl) RunWithLifecycle(lifecycle service.Lifecycle) error {
// Assume listen socket closed
break
}
s.wg.Add(1)
go s.handleConnection(tcpConn)
}
lifecycle.Stopping()
Expand Down Expand Up @@ -403,6 +404,7 @@ func (s *serverImpl) handleConnection(conn net.Conn) {
if err != nil {
logger.Info(err)
_ = conn.Close()
s.wg.Done()
return
}
shutdownHandlerID := fmt.Sprintf("network-%s", connectionID)
Expand All @@ -425,6 +427,7 @@ func (s *serverImpl) handleConnection(conn net.Conn) {
logger.Debug(log.NewMessage(MDisconnected, "Client disconnected"))
handlerNetworkConnection.OnDisconnect()
_ = conn.Close()
s.wg.Done()
return
}
logger = logger.WithLabel("username", sshConn.User())
Expand All @@ -434,7 +437,6 @@ func (s *serverImpl) handleConnection(conn net.Conn) {
sshShutdownHandlerID := fmt.Sprintf("ssh-%s", connectionID)
s.lock.Unlock()

s.wg.Add(1)
go func() {
_ = sshConn.Wait()
logger.Debug(log.NewMessage(MDisconnected, "Client disconnected"))
Expand Down Expand Up @@ -484,8 +486,10 @@ func (s *serverImpl) handleChannels(
if !ok {
break
}
s.lock.Lock()
channelID := s.nextChannelID
s.nextChannelID++
s.lock.Unlock()
logger = logger.WithLabel("channelId", channelID)
if newChannel.ChannelType() != "session" {
logger.Debug(log.NewMessage(EUnsupportedChannelType, "Unsupported channel type requested").Label("type", newChannel.ChannelType()))
Expand Down

0 comments on commit 7394c70

Please sign in to comment.