dongzhangnong2063 2017-03-03 23:47
浏览 391


TL;DR - What is the proper way to close a session freeing all resources?

My investigation thus far:

The *Session has a Close() function which calls the *Channel Close() function which sends a message (I'm guessing to the remote server) to close, but I don't see anything about closing other resources like the pipe returned from the *Session StdoutPipe() function.

Looking at the *Session Wait() code, I see that the *Session stdinPipeWriter is closed but nothing about the stdoutPipe.

This package feels a lot like the os/exec package which guarantees that using the os/exec Wait() function will clean up all the resources. Doing some light digging there shows some similarities in the Wait() functions. Both use the following construct to report errors on io.Copy calls to their stdout, stderr, stdin readers/writers (well if I'm reading this correctly actually only one error) - crypto package shown:

var copyError error
for _ = range s.copyFuncs {
    if err := <-s.errors; err != nil && copyError == nil {
        copyError = err

But the os/exec Wait() also calls this close descriptor method


which is just calling the close method on a slice of io.Closer interfaces:

func (c *Cmd) closeDescriptors(closers []io.Closer) {
    for _, fd := range closers {

when os/exec creates the pipe, it tracks what needs closing:

func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
if c.Stdout != nil {
    return nil, errors.New("exec: Stdout already set")
if c.Process != nil {
    return nil, errors.New("exec: StdoutPipe after process started")
pr, pw, err := os.Pipe()
if err != nil {
    return nil, err
c.Stdout = pw
c.closeAfterStart = append(c.closeAfterStart, pw)
c.closeAfterWait = append(c.closeAfterWait, pr)
return pr, nil

During this I noticed that x/cyrpto/ssh *Session StdoutPipe() returns an io.Reader and ox/exec returns an io.ReadCloser. And x/crypto/ssh does not track what to close. I can't find a call to os.Pipe() in the library so maybe the implementation is different and I'm missing something and confused by the Pipe name.

  • 写回答



      相关推荐 更多相似问题


      • ¥50 有没有大佬看一下关于思科的这道题怎么做
      • ¥30 求十字路口交通灯S7-200plc,今天早上八点前。
      • ¥20 如何用智能手表oppowatch3实现个人热点功能
      • ¥15 运营商打开wanpush会有怎么样的危害
      • ¥15 Java编程题,定义一个时钟
      • ¥15 Python面向对象编程——生成多项式的一阶导数
      • ¥15 在编写C++程序遇到了问题
      • ¥15 java程序查找字符串中所有的回文并输出
      • ¥15 怎么让只支持对.exe 和.dll文件进行加壳的加壳器,同时让它也支持php文件加壳呢
      • ¥15 这两个数学公式什么意思?