SFTP connection bug

Bug #991771 reported by Guillermo Guerrero
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Syncany
New
Undecided
Unassigned

Bug Description

Hi,

I found a bug with SFTP connection, the bug is the conection with the storage server never closes, and creates more connections every time.
For example when upload the chunks the conection is already stablished and for each chunk open a conection because in the SftpTransferManager in funtion upload conects with the storage and the application not checks the connection.

@Override
    public void upload(File localFile, RemoteFile remoteFile) throws StorageException {
        connect();

And the function connects every time creates a new session and not closes the last session:

                        this.session = jsch.getSession(getConnection().getUsername(), getConnection().getHost(), getConnection().getPort());
                        Hashtable cf = new Hashtable();
                        cf.put("StrictHostKeyChecking", "no");
                        session.setConfig(cf);
                        session.connect();

I solved checking the connection when try to connect with the storage.

    @Override
    public void connect() throws StorageConnectException {
        boolean isConnected = false;

        if(session != null){
            if(session.isConnected()){
                if(sftp != null){
                    if (sftp.isConnected()){
                        isConnected = true;
                    }else{
                        session.disconnect();
                    }
                } else{
                    session.disconnect();
                }
            }
        }

        if(!isConnected){
            for (int i=0; i<CONNECT_RETRY_COUNT; i++) {
                try {

                    if (logger.isLoggable(Level.INFO)) {
                        logger.log(Level.INFO, "SFTP client connecting to {0}:{1} ...", new Object[]{getConnection().getHost(), getConnection().getPort()});
                    }

                    if (getConnection().isKeyAuth()) {
                        jsch.addIdentity(getConnection().getKeyPath(), getConnection().getPassphrase());
                        this.session = jsch.getSession(getConnection().getUsername(), getConnection().getHost(), getConnection().getPort());
                        Hashtable cf = new Hashtable();
                        cf.put("StrictHostKeyChecking", "no");
                        session.setConfig(cf);
                        session.connect();
                        if(!session.isConnected())
                            logger.log(Level.WARNING, "SFTP client: unable to connect (user/password) to {0}:{1} ...", new Object[]{getConnection().getHost(), getConnection().getPort()});

                    } else {
                        this.session = jsch.getSession(getConnection().getUsername(), getConnection().getHost(), getConnection().getPort());

                        Hashtable cf = new Hashtable();
                        cf.put("StrictHostKeyChecking", "no");
                        session.setConfig(cf);
                        session.setPassword(getConnection().getPassword());
                        session.connect();
                        if(!session.isConnected())
                            logger.log(Level.WARNING, "SFTP client: unable to connect (user/password) to {0}:{1} ...", new Object[]{getConnection().getHost(), getConnection().getPort()});
                    }

                    this.sftp = (ChannelSftp) session.openChannel("sftp");
                    this.sftp.connect();
                    if(!sftp.isConnected())
                            logger.log(Level.WARNING, "SFTP client: unable to connect sftp Channel ( {0}:{1} ) ...", new Object[]{getConnection().getHost(), getConnection().getPort()});

                    return;
                }
                catch (Exception ex) {
                    if (logger.isLoggable(Level.WARNING)) {
                        logger.log(Level.WARNING, "SFTP client connection failed.", ex);
                    }

                    throw new StorageConnectException(ex);
                }
            }

            if (logger.isLoggable(Level.SEVERE)) {
                logger.log(Level.SEVERE, "RETRYING FAILED: SFTP client connection failed.");
            }
        }
    }

In function disconnect sometimes appears the null pointer exception because sftp or session is null.
I solved with this:

    @Override
    public void disconnect() {
        if(sftp != null){
            this.sftp.quit();
            this.sftp.disconnect();
        }
        if(session != null){
            this.session.disconnect();
        }
    }

Regards.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.