Skip to content

fix: give SSH stdio sessions a chance to close before closing netstack #10815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
if err != nil {
return xerrors.Errorf("connect SSH: %w", err)
}
copier := &rawSSHCopier{conn: rawSSH, r: inv.Stdin, w: inv.Stdout}
copier := newRawSSHCopier(logger, rawSSH, inv.Stdin, inv.Stdout)
if err = stack.push("rawSSHCopier", copier); err != nil {
return err
}
Expand Down Expand Up @@ -853,9 +853,16 @@ type rawSSHCopier struct {
logger slog.Logger
r io.Reader
w io.Writer

done chan struct{}
}

func newRawSSHCopier(logger slog.Logger, conn *gonet.TCPConn, r io.Reader, w io.Writer) *rawSSHCopier {
return &rawSSHCopier{conn: conn, logger: logger, r: r, w: w, done: make(chan struct{})}
}

func (c *rawSSHCopier) copy(wg *sync.WaitGroup) {
defer close(c.done)
logCtx := context.Background()
wg.Add(1)
go func() {
Expand Down Expand Up @@ -890,5 +897,16 @@ func (c *rawSSHCopier) copy(wg *sync.WaitGroup) {
}

func (c *rawSSHCopier) Close() error {
return c.conn.CloseWrite()
err := c.conn.CloseWrite()

// give the copy() call a chance to return on a timeout, so that we don't
// continue tearing down and close the underlying netstack before the SSH
// session has a chance to gracefully shut down.
t := time.NewTimer(5 * time.Second)
defer t.Stop()
select {
case <-c.done:
case <-t.C:
}
return err
}