@@ -414,7 +414,7 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
414
414
return cmd , nil
415
415
}
416
416
417
- func (a * agent ) handleSSHSession (session ssh.Session ) error {
417
+ func (a * agent ) handleSSHSession (session ssh.Session ) ( retErr error ) {
418
418
cmd , err := a .createCommand (session .Context (), session .RawCommand (), session .Environ ())
419
419
if err != nil {
420
420
return err
@@ -437,6 +437,16 @@ func (a *agent) handleSSHSession(session ssh.Session) error {
437
437
if err != nil {
438
438
return xerrors .Errorf ("start command: %w" , err )
439
439
}
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
+ }()
440
450
err = ptty .Resize (uint16 (sshPty .Window .Height ), uint16 (sshPty .Window .Width ))
441
451
if err != nil {
442
452
return xerrors .Errorf ("resize ptty: %w" , err )
@@ -455,23 +465,15 @@ func (a *agent) handleSSHSession(session ssh.Session) error {
455
465
go func () {
456
466
_ , _ = io .Copy (session , ptty .Output ())
457
467
}()
458
- waitErr : = ptty .Wait ()
468
+ err = ptty .Wait ()
459
469
var exitErr * exec.ExitError
460
470
// 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 ) {
464
473
a .logger .Warn (context .Background (), "wait error" ,
465
474
slog .Error (err ))
466
- return err
467
475
}
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
475
477
}
476
478
477
479
cmd .Stdout = session
0 commit comments