title: Getting Connection Reset error from sftp server frequently with parallel threads trying to put file on sftp
body: <p>I have a piece of multithreaded code which has 22 threads running in parallel and trying to put files on sftp server.</p>
  
  <p>But I keep getting Connection Reset error intermittently in my logs and few of the records fail because of that.</p>
  
  <p>On initial analysis, I found out that the size of the sftp server was t2.small and CPU utilization was going to 92%. </p>
  
  <p>Considering this as I reason I changed the server to c5n.xlarge, now the error is coming less frequently but still, I am getting it at times even when the maximum CPU utilization goes to 63%.</p>
  
  <p>I am not able to find anything different in sftp server logs at /var/log/secure.</p>
  
  <p>Below is the piece of code being used to put file, every thread creates a new session and closes it.</p>
  
  <pre><code>JSch ssh = new JSch();
              // ssh.setKnownHosts("/path/of/known_hosts/file");
              java.util.Properties config = new java.util.Properties();
              config.put("StrictHostKeyChecking", "no");
              // Use key authentication if it is set, else use password auth
              if (mpServerDetails.get(SftpFile.SFTP_USERKEY) != null
                      &amp;&amp; mpServerDetails.get(SftpFile.SFTP_USERKEY) != "") {
                      File userKeyFile = new File(mpServerDetails.get(SftpFile.SFTP_USERKEY).toString());
                  if (userKeyFile == null || !userKeyFile.exists()) {
                      throw new NonRetriableException(
                              "Key file " + mpServerDetails.get(SftpFile.SFTP_USERKEY).toString() + "not found.");
                  }
                  ssh.addIdentity(userKeyFile.getAbsolutePath());
                  session = ssh.getSession(mpServerDetails.get(SftpFile.SFTP_USERNAME).toString(),
                          mpServerDetails.get(SftpFile.SFTP_HOSTNAME).toString());
              } else if (mpServerDetails.get(SftpFile.SFTP_PASSWORD) != null) {
                  session = ssh.getSession(mpServerDetails.get(SftpFile.SFTP_USERNAME).toString(),
                          mpServerDetails.get(SftpFile.SFTP_HOSTNAME).toString());
                  session.setPassword(mpServerDetails.get(SftpFile.SFTP_PASSWORD).toString());
              }
              session.setConfig(config);
              session.connect();
              if (session != null &amp;&amp; !session.isConnected()) {
                  logger.warn("**session is not connected going to connect the sftp session ** {} ", session.getHost());
                  session.connect();
              }
              channel = (ChannelSftp) session.openChannel("sftp");
              if (channel != null &amp;&amp; !channel.isConnected()) {
                  logger.warn("**channel is not connected going to connect the sftp channel ** {} ",
                          channel.getSession().isConnected());
                  channel.connect();
              }
              channel.put(file.getAbsolutePath(), dest.getConfig().get(TransporterFileConstants.SFTP_DIRECTORY).toString()
                      + File.separatorChar + dest.getFileName(), new SystemOutProgressMonitor());
  
          }
          catch (NonRetriableException e) {
              throw new NonRetriableException(e);
          }
          catch (Exception e) {
              logger.error(
                      "Error occured while uploading file having name " + dest.getFileName() + " from remote directory:"
                              + dest.getConfig().get(TransporterFileConstants.SFTP_DIRECTORY).toString(),
                      e);
              logger.error("SFTP Exception : ", e);
              throw new RetriableException(e);
          }
          finally {
              if (null != channel &amp;&amp; channel.isConnected()) {
                  try {
                      channel.disconnect();
                  }
                  catch (Throwable e) {
                      logger.error("Error while disconnecting channel : ", e);
                  }
              }
              if (null != session) {
                  try {
                      session.disconnect();
                  }
                  catch (Throwable e) {
                      logger.error("Error while returning object to sftp pool : ", e);
                  }
              }
          }
  </code></pre>
  
  <p>Can someone help me understand why I might be getting this exception?</p>
  
  <p>SFTP server configurations are </p>
  
  <pre><code>MaxSessions 50
  Capacity - 25 GB
  4 core server with 10 GB Ram
  </code></pre>
  
  <p>A snippet of error message</p>
  
  <pre><code>com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
      at com.jcraft.jsch.Session.connect(Session.java:558) ~[honeybee-engine.jar:na]
  </code></pre>
  
  <p>If this would keep coming, my data processing would not be consistent.</p>