← Back to team overview

syncany-team team mailing list archive

[Bug 991771] [NEW] SFTP connection bug

 

Public bug reported:

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.

** Affects: syncany
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Syncany
Team, which is subscribed to Syncany.
https://bugs.launchpad.net/bugs/991771

Title:
  SFTP connection bug

Status in Syncany:
  New

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 manage notifications about this bug go to:
https://bugs.launchpad.net/syncany/+bug/991771/+subscriptions


Follow ups

References