Skip to content

fix: Guard pty window resize after close #3270

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
Jul 28, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
for win := range windowSize {
resizeErr := ptty.Resize(uint16(win.Height), uint16(win.Width))
if resizeErr != nil {
a.logger.Warn(context.Background(), "failed to resize tty", slog.Error(err))
a.logger.Warn(context.Background(), "failed to resize tty", slog.Error(resizeErr))
}
}
}()
Expand Down
23 changes: 18 additions & 5 deletions pty/pty_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/creack/pty"
"golang.org/x/xerrors"
)

func newPty() (PTY, error) {
Expand All @@ -26,6 +27,8 @@ func newPty() (PTY, error) {

type otherPty struct {
mutex sync.Mutex
closed bool
err error
pty, tty *os.File
}

Expand Down Expand Up @@ -55,6 +58,9 @@ func (p *otherPty) Output() ReadWriter {
func (p *otherPty) Resize(height uint16, width uint16) error {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.closed {
return p.err
}
return pty.Setsize(p.pty, &pty.Winsize{
Rows: height,
Cols: width,
Expand All @@ -65,17 +71,24 @@ func (p *otherPty) Close() error {
p.mutex.Lock()
defer p.mutex.Unlock()

if p.closed {
return p.err
}
p.closed = true

err := p.pty.Close()
err2 := p.tty.Close()
if err != nil {
_ = p.tty.Close()
return err
err = err2
}

err = p.tty.Close()
if err != nil {
return err
p.err = err
} else {
p.err = xerrors.New("pty: closed")
}
return nil

return err
}

func (p *otherProcess) Wait() error {
Expand Down