Uploaded image for project: 'Commons VFS'
  1. Commons VFS
  2. VFS-832

Sftp channel not put back in doGetInputStream

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.9.0
    • 2.10.0
    • None

    Description

      since VFS-210 added this in SftpFileObject.java:

       

      InputStream is;
      try
      {
          // VFS-210: sftp allows to gather an input stream even from a directory and will
          // fail on first read. So we need to check the type anyway
          if (!getType().hasContent())
          {
              throw new FileSystemException("vfs.provider/read-not-file.error", getName());
          }
      
          is = channel.get(relPath);
      }
      catch (SftpException e)
      {
          if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
          {
              throw new FileNotFoundException(getName());
          }
      
          throw new FileSystemException(e);
      }

      when throw an exception(such as file not exists or get input stream from a directory), the channel  is not called with putChannel() and not be closed. when this happen several times (normally 10 times due to sshd default MaxSession is 10), getChannel() will always throws exception(Channel is not opened), because server will not response the open request.

       

      It can be reproduced in this way:

       

      //Setup our SFTP configuration
      FileSystemOptions opts = new FileSystemOptions();
      SftpFileSystemConfigBuilder instance = SftpFileSystemConfigBuilder.getInstance();
      instance.setStrictHostKeyChecking(opts, "no");
      instance.setUserDirIsRoot(opts, true);
      instance.setConnectTimeout(opts, Duration.ofSeconds(30));
      instance.setSessionTimeout(opts, Duration.ofSeconds(30));
      instance.setDisableDetectExecChannel(opts, true);
      
      for (int i = 0; i < 15; i++) {
        try {
          try (FileObject fileObject = VFS.getManager().resolveFile("sftp://foo@example.com/path_not_exists.txt", opts)) {
            try (InputStream inputStream = fileObject.getContent().getInputStream()) {
              // do something
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      } 

      first 10 times will be "Could not read from "xxx" because it is not a file."

       

      then will be

       

      Caused by: com.jcraft.jsch.JSchException: channel is not opened.
          at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:768)
          at com.jcraft.jsch.Channel.connect(Channel.java:151)
          at org.apache.commons.vfs2.provider.sftp.SftpFileSystem.getChannel(SftpFileSystem.java:213)
          ... 6 more 

       

      I will commit a pr to fix this

       

       

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              wangerry wangerry
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 1h
                  1h
                  Remaining:
                  Remaining Estimate - 1h
                  1h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified