Skip to content

Commit ca54029

Browse files
committed
Fix SSH PTY exit handling
1 parent f0dfa1f commit ca54029

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

guest/sshd/session.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,21 @@ func (s *Server) runWithPTY(ch ssh.Channel, requests <-chan *ssh.Request, cmd *e
273273
}()
274274

275275
// Bidirectional copy between the SSH channel and the PTY.
276-
var wg sync.WaitGroup
277-
wg.Add(2)
278-
276+
// Only wait for PTY->channel to drain; the input copy will exit once the
277+
// channel is closed after we send the exit status.
278+
outputDone := make(chan struct{})
279279
go func() {
280-
defer wg.Done()
281-
_, _ = io.Copy(ptmx, ch)
280+
defer close(outputDone)
281+
_, _ = io.Copy(ch, ptmx)
282282
}()
283283
go func() {
284-
defer wg.Done()
285-
_, _ = io.Copy(ch, ptmx)
284+
_, _ = io.Copy(ptmx, ch)
286285
}()
287286

288287
err = cmd.Wait()
289-
// Close the PTY to unblock the copy goroutines.
288+
// Close the PTY to unblock the output copy.
290289
_ = ptmx.Close()
291-
wg.Wait()
290+
<-outputDone
292291

293292
return exitCode(err)
294293
}

0 commit comments

Comments
 (0)