Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
2.9.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
- links to