Skip to content

Commit 45772fa

Browse files
committed
add comments
1 parent 9fde237 commit 45772fa

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

session.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,24 @@ func DefaultSessionHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.Ne
127127
type session struct {
128128
sync.Mutex
129129
gossh.Channel
130-
conn *gossh.ServerConn
131-
handler Handler
132-
subsystemHandlers map[string]SubsystemHandler
133-
handled bool
134-
exited bool
135-
pty *Pty
136-
x11 *X11
137-
winch chan Window
138-
env []string
139-
ptyCb PtyCallback
140-
x11Cb X11Callback
141-
sessReqCb SessionRequestCallback
142-
rawCmd string
143-
subsystem string
144-
ctx Context
130+
conn *gossh.ServerConn
131+
handler Handler
132+
subsystemHandlers map[string]SubsystemHandler
133+
handled bool
134+
exited bool
135+
pty *Pty
136+
x11 *X11
137+
winch chan Window
138+
env []string
139+
ptyCb PtyCallback
140+
x11Cb X11Callback
141+
sessReqCb SessionRequestCallback
142+
rawCmd string
143+
subsystem string
144+
ctx Context
145+
// sigMu protects sigCh and sigBuf, it is made separate from the
146+
// session mutex to reduce the risk of deadlocks while we process
147+
// buffered signals.
145148
sigMu sync.Mutex
146149
sigCh chan<- Signal
147150
sigBuf []Signal
@@ -255,6 +258,12 @@ func (sess *session) Signals(c chan<- Signal) {
255258
return
256259
}
257260
go func() {
261+
// If we have buffered signals, we need to send them whilst
262+
// holding the signal mutex to avoid race conditions on sigCh
263+
// and sigBuf. We also guarantee that calling Signals(ch)
264+
// followed by Signals(nil) will have depleted the sigBuf when
265+
// the second call returns and that there will be no more
266+
// signals on ch.
258267
defer sess.sigMu.Unlock()
259268
for _, sig := range sess.sigBuf {
260269
sess.sigCh <- sig

0 commit comments

Comments
 (0)