我已经编写了一个SSH客户端来连接网络设备,一旦运行命令超过25秒,我就通过"select"设置超时.我注意到一些设备,他们有另一个IOS,一旦超时被触发,它就不能通过Close()方法丢弃SSH会话,并导致goroutinge泄漏.我需要保持客户端并断开会话以准备下一个命令.看起来goroutine在那个时候永远不会终止!你们有什么想法吗?
go func() {
r <- s.Run(cmd)
}()
select {
case err := <-r:
return err
case <-time.After(time.Duration(timeout) * time.Second):
s.Close()
return fmt.Errorf("timeout after %d seconds", timeout)
}
Run Code Online (Sandbox Code Playgroud)
通过堆分析,我看到了以下内容:2.77GB 99.44%99.44%2.77GB 99.44%bytes.makeSlice
0 0% 99.44% 2.77GB 99.44% bytes.(*Buffer).ReadFrom
0 0% 99.44% 2.77GB 99.44% golang.org/x/crypto/ssh.(*Session).start.func1
0 0% 99.44% 2.77GB 99.44% golang.org/x/crypto/ssh.(*Session).stdout.func1
0 0% 99.44% 2.77GB 99.44% io.Copy
0 0% 99.44% 2.77GB 99.44% io.copyBuffer
0 0% 99.44% 2.78GB 99.93% runtime.goexit
Run Code Online (Sandbox Code Playgroud)
在/usr/local/go/src/runtime/asm_amd64.s中的例程======================== runtime.goexit
0 2.78GB (flat, cum) 99.93% of Total
. . 1993: RET
. . 1994:
. . 1995:// The top-most function running on a goroutine
. . 1996:// returns to goexit+PCQuantum.
. . 1997:TEXT runtime·goexit(SB),NOSPLIT,$0-0
. 2.78GB 1998: BYTE $0x90 // NOP
. . 1999: CALL runtime·goexit1(SB) // does not return
. . 2000: // traceback from goexit1 must hit code range of goexit
. . 2001: BYTE $0x90 // NOP
. . 2002:
. . 2003:TEXT runtime·prefetcht0(SB),NOSPLIT,$0-8
Run Code Online (Sandbox Code Playgroud)
Channelr阻止 Go 例程返回,因为它没有被清空。我已经编写了您的代码的改编版本并插入了一个等待组来演示该问题:
func main() {
var wg sync.WaitGroup // This is only added for demonstration purposes
s := new(clientSession)
r := make(chan error)
go func(s *clientSession) {
wg.Add(1)
r <- s.Run()
wg.Done() // Will only be called after s.Run() is able to return
}(s)
fmt.Println("Client has been opened")
select {
case err := <-r:
fmt.Println(err)
case <-time.After(1 * time.Second):
s.Close()
fmt.Println("Timed out, closing")
}
wg.Wait() // Waits until wg.Done() is called.
fmt.Println("Main finished successfully")
}
Run Code Online (Sandbox Code Playgroud)
Go Playground 似乎终止了程序,所以我创建了一个包含完整可运行代码的要点。当我们运行时incorrect.go:
$ go run incorrect.go
Client has been opened
Timed out, closing
fatal error: all goroutines are asleep - deadlock!
....
Run Code Online (Sandbox Code Playgroud)
那是因为我们的代码就线路死锁了wg.Wait()。这表明wg.Done()在 Go 例程中从未达到过。
正如评论所指出的,缓冲通道可以在这里提供帮助。但前提是你不再关心错误,在调用之后s.Close()
r := make(chan error, 1)
Run Code Online (Sandbox Code Playgroud)
buffered.go运行正确,但报错丢失:
$ go run buffered.go
Client has been opened
Timed out, closing
Main finished successfully
Run Code Online (Sandbox Code Playgroud)
另一种选择是将通道排空一次:
select {
case err := <-r:
fmt.Println(err)
case <-time.After(1 * time.Second):
s.Close()
fmt.Println("Timed out, closing")
fmt.Println(<-r)
}
Run Code Online (Sandbox Code Playgroud)
或者通过循环包装select(for没有缓冲通道):
X:
for {
select {
case err := <-r:
fmt.Println(err)
break X // because we are in main(). Normally `return err`
case <-time.After(1 * time.Second):
s.Close()
fmt.Println("Timed out, closing")
}
}
Run Code Online (Sandbox Code Playgroud)
当我们运行时,drain.go我们看到还打印了错误:
$ go run incorrect.go
Client has been opened
Timed out, closing
Run() closed
Main finished successfully
Run Code Online (Sandbox Code Playgroud)
在现实世界中,人们会运行多个 Go 例程。因此,您将需要在for循环中使用一些计数器或进一步利用等待组功能。