Skip to content

Commit

Permalink
Merge pull request #461 from Darkren/fix/data_race_therealssh
Browse files Browse the repository at this point in the history
Fix data race in `therealssh`
  • Loading branch information
ayuryshev authored Jul 8, 2019
2 parents eccff5d + 907d170 commit e6a3cb1
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions internal/therealssh/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type SSHChannel struct {
conn net.Conn
msgCh chan []byte

session *Session
listener *net.UnixListener
session *Session
listenerMx sync.Mutex
listener *net.UnixListener

dataChMx sync.Mutex
dataCh chan []byte
Expand Down Expand Up @@ -155,7 +156,9 @@ func (sshCh *SSHChannel) ServeSocket() error {
return fmt.Errorf("failed to open unix socket: %s", err)
}

sshCh.listenerMx.Lock()
sshCh.listener = l
sshCh.listenerMx.Unlock()
conn, err := l.AcceptUnix()
if err != nil {
return fmt.Errorf("failed to accept connection: %s", err)
Expand All @@ -164,8 +167,7 @@ func (sshCh *SSHChannel) ServeSocket() error {
debug("got new socket connection")
defer func() {
conn.Close()
sshCh.listener.Close()
sshCh.listener = nil
sshCh.closeListener() //nolint:errcheck
os.Remove(sshCh.SocketPath())
}()

Expand Down Expand Up @@ -270,9 +272,7 @@ func (sshCh *SSHChannel) close() (closed bool, err error) {
sErr = sshCh.session.Close()
}

if sshCh.listener != nil {
lErr = sshCh.listener.Close()
}
lErr = sshCh.closeListener()

if sErr != nil {
err = sErr
Expand Down Expand Up @@ -314,6 +314,13 @@ func (sshCh *SSHChannel) IsClosed() bool {
}
}

func (sshCh *SSHChannel) closeListener() error {
sshCh.listenerMx.Lock()
defer sshCh.listenerMx.Unlock()

return sshCh.listener.Close()
}

func debug(format string, v ...interface{}) {
if !Debug {
return
Expand Down

0 comments on commit e6a3cb1

Please sign in to comment.