Skip to content

Commit ab6594b

Browse files
committed
Fix close error handling on agent TTY
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 50a88b0 commit ab6594b

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

agent/agent.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
414414
return cmd, nil
415415
}
416416

417-
func (a *agent) handleSSHSession(session ssh.Session) error {
417+
func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
418418
cmd, err := a.createCommand(session.Context(), session.RawCommand(), session.Environ())
419419
if err != nil {
420420
return err
@@ -437,6 +437,16 @@ func (a *agent) handleSSHSession(session ssh.Session) error {
437437
if err != nil {
438438
return xerrors.Errorf("start command: %w", err)
439439
}
440+
defer func() {
441+
closeErr := ptty.Close()
442+
if closeErr != nil {
443+
a.logger.Warn(context.Background(), "failed to close tty",
444+
slog.Error(closeErr))
445+
if retErr == nil {
446+
retErr = closeErr
447+
}
448+
}
449+
}()
440450
err = ptty.Resize(uint16(sshPty.Window.Height), uint16(sshPty.Window.Width))
441451
if err != nil {
442452
return xerrors.Errorf("resize ptty: %w", err)
@@ -455,23 +465,15 @@ func (a *agent) handleSSHSession(session ssh.Session) error {
455465
go func() {
456466
_, _ = io.Copy(session, ptty.Output())
457467
}()
458-
waitErr := ptty.Wait()
468+
err = ptty.Wait()
459469
var exitErr *exec.ExitError
460470
// ExitErrors just mean the command we run returned a non-zero exit code, which is normal
461-
// and not something to be concerned about. But, if it's something else, we should log and
462-
// return it.
463-
if waitErr != nil && !xerrors.As(waitErr, &exitErr) {
471+
// and not something to be concerned about. But, if it's something else, we should log it.
472+
if err != nil && !xerrors.As(err, &exitErr) {
464473
a.logger.Warn(context.Background(), "wait error",
465474
slog.Error(err))
466-
return err
467475
}
468-
closeErr := ptty.Close()
469-
if closeErr != nil {
470-
a.logger.Warn(context.Background(), "failed to close tty",
471-
slog.Error(err))
472-
return err
473-
}
474-
return waitErr
476+
return err
475477
}
476478

477479
cmd.Stdout = session

pty/pty_other.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ type otherPty struct {
3131

3232
type otherPtyWithProcess struct {
3333
*otherPty
34-
cmd *exec.Cmd
34+
cmd *exec.Cmd
35+
36+
// cmdDone protects access to cmdErr: anything reading cmdErr should read from cmdDone first.
3537
cmdDone chan any
3638
cmdErr error
3739
}

pty/pty_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type ptyWindows struct {
6969

7070
type ptyWindowsWithProcess struct {
7171
*ptyWindows
72+
73+
// cmdDone protects access to cmdErr: anything reading cmdErr should read from cmdDone first.
7274
cmdDone chan any
7375
cmdErr error
7476
proc *os.Process

0 commit comments

Comments
 (0)