golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.42k stars 17.71k forks source link

x/crypto/ssh: cannot close ssh session #52290

Open XindaH opened 2 years ago

XindaH commented 2 years ago

What version of Go are you using (go version)?

$ go version
1.17

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jx/d_08_gm93psc4v9lm3zzjc300000gn/T/go-build1736071007=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

func test() {
    client, err := ssh.Dial("tcp", "xx.xx.xx.xx:22", &ssh.ClientConfig{
        User:            "root",
        Auth:            []ssh.AuthMethod{ssh.Password("123")},
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    })
    if err != nil {
        log.Panic(err)
    }
    session, err := client.NewSession()
    if err != nil {
        log.Panic(err)
    }
    defer session.Close()
    go func() {
        // err := session.Run("while true;do echo test;done")
        err := session.Run("sleep 6000")
        if err != nil {
            log.Println(err)
        }
    }()
    time.Sleep(time.Second * 5)

    err = session.Close()
    if err != nil {
        log.Println(err)
    }
    log.Println("finished")
}

What did you expect to see?

2022/04/12 13:35:17 finished
2022/04/12 13:35:17 Process exited with status 141 from signal PIPE

What did you see instead?

2022/04/12 13:35:17 finished

If I run while true;do echo test;done, the session can be closed gracefully. However sleep 6000 is still running after this process finished.

ianlancetaylor commented 2 years ago

This looks related to #23019.

XindaH commented 2 years ago

This looks related to #23019.

maybe different👀

dmitshur commented 2 years ago

CC @FiloSottile.

XindaH commented 2 years ago

ping

XindaH commented 2 years ago

I have to use client.Close() to kill this process with pty request. If I request pty, I cannot run cmd by dropbear server which exits with 129 code.