Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak When SftpFileSystemProvider newInputStream get a Exception #500

Closed
detective0922 opened this issue May 11, 2024 · 0 comments · Fixed by #501
Closed

Memory Leak When SftpFileSystemProvider newInputStream get a Exception #500

detective0922 opened this issue May 11, 2024 · 0 comments · Fixed by #501
Assignees
Labels
bug An issue describing a bug in the code
Milestone

Comments

@detective0922
Copy link

detective0922 commented May 11, 2024

Version

V2.8.0

Bug description

I checked the #294 and merge the fix code to V2.8.0
That fixed the old issue, but there is still a issue here.
If a exception happen in FilterInputStream init, for example, a "No such file" exception happen in client.read(p.toString(), modes), then lead to constructor init failure of FilterInputStream
return new FilterInputStream(client.read(p.toString(), modes))
If that happen, the code seems will not call the "client.close()", so SftpClient will not close and keep increasing, finally lead to a OOM

2024-05-11 12:00:02,311 INFO [LoggerStream] - SFTP error (SSH_FX_NO_SUCH_FILE): No such file
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.AbstractSftpClient.throwStatusException(AbstractSftpClient.java:217)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.AbstractSftpClient.checkAttributesResponse(AbstractSftpClient.java:306)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.AbstractSftpClient.checkAttributes(AbstractSftpClient.java:284)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.AbstractSftpClient.stat(AbstractSftpClient.java:1001)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.SftpInputStreamAsync.(SftpInputStreamAsync.java:69)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.impl.AbstractSftpClient.read(AbstractSftpClient.java:1189)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.fs.SftpFileSystem$Wrapper.read(SftpFileSystem.java:491)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.fs.SftpFileSystem$Wrapper.read(SftpFileSystem.java:483)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at org.apache.sshd.sftp.client.fs.SftpFileSystemProvider.newInputStream(SftpFileSystemProvider.java:525)
2024-05-11 12:00:02,311 INFO [LoggerStream] - at java.nio.file.Files.newInputStream(Files.java:152)

    @Override
    public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
        Collection<OpenMode> modes = OpenMode.fromOpenOptions(Arrays.asList(options));
        if (modes.isEmpty()) {
            modes = EnumSet.of(OpenMode.Read);
        }
        SftpPath p = toSftpPath(path);
        //return p.getFileSystem().getClient().read(p.toString(), modes);
        SftpClient client = p.getFileSystem().getClient();
        return new FilterInputStream(client.read(p.toString(), modes)) { //line 525
            @Override
            public void close() throws IOException {
                try {
                    super.close();
                } finally {
                    client.close();
                }
            }
        };
    }

Actual behavior

Memory leak happen

Expected behavior

No Memory Leak

Relevant log output

No response

Other information

No response

@tomaswolf tomaswolf added the bug An issue describing a bug in the code label May 11, 2024
@tomaswolf tomaswolf self-assigned this May 11, 2024
tomaswolf added a commit to tomaswolf/mina-sshd that referenced this issue May 11, 2024
If client.read() or client.write() throw an exception, the client must
be closed.

Bug: apache#500
@tomaswolf tomaswolf added this to the 2.13.0 milestone May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue describing a bug in the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants