Skip to content

Commit 29d44b6

Browse files
authored
fix: Guard pty window resize after close (#3270)
Could help alleviate #3236.
1 parent 43b8cf0 commit 29d44b6

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

agent/agent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
457457
for win := range windowSize {
458458
resizeErr := ptty.Resize(uint16(win.Height), uint16(win.Width))
459459
if resizeErr != nil {
460-
a.logger.Warn(context.Background(), "failed to resize tty", slog.Error(err))
460+
a.logger.Warn(context.Background(), "failed to resize tty", slog.Error(resizeErr))
461461
}
462462
}
463463
}()

pty/pty_other.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
"github.com/creack/pty"
13+
"golang.org/x/xerrors"
1314
)
1415

1516
func newPty() (PTY, error) {
@@ -26,6 +27,8 @@ func newPty() (PTY, error) {
2627

2728
type otherPty struct {
2829
mutex sync.Mutex
30+
closed bool
31+
err error
2932
pty, tty *os.File
3033
}
3134

@@ -55,6 +58,9 @@ func (p *otherPty) Output() ReadWriter {
5558
func (p *otherPty) Resize(height uint16, width uint16) error {
5659
p.mutex.Lock()
5760
defer p.mutex.Unlock()
61+
if p.closed {
62+
return p.err
63+
}
5864
return pty.Setsize(p.pty, &pty.Winsize{
5965
Rows: height,
6066
Cols: width,
@@ -65,17 +71,24 @@ func (p *otherPty) Close() error {
6571
p.mutex.Lock()
6672
defer p.mutex.Unlock()
6773

74+
if p.closed {
75+
return p.err
76+
}
77+
p.closed = true
78+
6879
err := p.pty.Close()
80+
err2 := p.tty.Close()
6981
if err != nil {
70-
_ = p.tty.Close()
71-
return err
82+
err = err2
7283
}
7384

74-
err = p.tty.Close()
7585
if err != nil {
76-
return err
86+
p.err = err
87+
} else {
88+
p.err = xerrors.New("pty: closed")
7789
}
78-
return nil
90+
91+
return err
7992
}
8093

8194
func (p *otherProcess) Wait() error {

0 commit comments

Comments
 (0)