C2FO / vfs

Pluggable, extensible virtual file system for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sftp backend only auto-disconnects sftp subsystem, not ssh connection

funkyshu opened this issue · comments

sftp backend automatically disconnects when no command has been issued in the last 10 seconds (default). However, it appears to only close the SFTP subsystem session, not the underlying SSH connection. This is not a problem when the application closes but is definitely a problem on long-running apps. Memory may eventually run out or ssh service max connections may be reached.

If you run the following test app, the server side (at least on OpenSSH server), every connection will show the main connection sshd: <username> [priv], and two forked processes sshd: <username>@notty and sshd: <username>@internal-sftp. 10 second after each connection was mad the subsytem @internal-sftp process goes away but leaves the other two.

package main

import (
	"fmt"

	"github.com/c2fo/vfs/v6/backend/sftp"
)

func main() {
        // 10 separate connections are made
	for i := 1; i < 11; i++ {
		err := list()
		if err != nil {
			panic(err)
		}
	}
	fmt.Println("DONE")

        // blocking -- ctrl-c when done
	select {}
}

func list() error {

	fs := &sftp.FileSystem{}
	fs = fs.WithOptions(sftp.Options{Password: "mySecretPassword"})
	loc, err := fs.NewLocation("myusername@sftp.myserver.com", "/home/myusername/")
	if err != nil {
		return err
	}
	fmt.Printf("checking %s\n", loc.URI())

        // connection is made here
	files, err := loc.List()
	if err != nil {
		return err
	}
	fmt.Printf("%v", files)
	return nil
}

Need to close the underlying ssh conn.