-
Notifications
You must be signed in to change notification settings - Fork 367
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 in SftpFileSystemProvider #294
Comments
Meet the same question when using SftpFileSystem, seems to be caused by SftpFileSystem using a ThreadLocal to cache a sftp client but unable to clean it |
SftpFileSystem.getClient() returns wrapper instances that need to be closed to avoid a memory leak via ThreadLocals. Make sure that the streams returned by SftpFileSystemProvider.newInputStream() and newOutputStream() do close the client used by ensuring that the streams returned by SftpFileSystem.read() and write() do so. Also fix SftpFileSystemProvider.copy() to close the SftpClient it uses. SftpFileSystemProvider.newDirectoryStream() and newFileChannel() already do close the SftpClient used. Fix the SftpFileSystemTest to properly close SftpClients and DirectoryStreams. Bug: apache#294
SftpFileSystem.getClient() returns reference-counted wrapper instances that need to be closed to avoid a memory leak via ThreadLocals. Make sure that the streams returned by SftpFileSystemProvider.newInputStream() and newOutputStream() do close the client used by ensuring that the streams returned by SftpFileSystem.read() and write() do so. Also fix SftpFileSystemProvider.copy() to close the SftpClient it uses. SftpFileSystemProvider.newDirectoryStream() and newFileChannel() already do close the SftpClient used. Fix the SftpFileSystemTest to properly close SftpClients and DirectoryStreams. Bug: apache#294
Thanks for pointing out this bug. It should be fixed by the referenced pull request. But personally I'm not convinced using a ThreadLocal to store these wrappers is a good idea. It appears the idea was to use different SftpClients and thus SSH channels for different threads. I cannot be sure though since there is no design documentation and this code was written long before I became involved here. I'm not entirely sure that using ThreadLocals for this works as intended in all cases. |
SftpFileSystem.getClient() returns reference-counted wrapper instances that need to be closed to avoid a memory leak via ThreadLocals. Make sure that the streams returned by SftpFileSystemProvider.newInputStream() and newOutputStream() do close the client. Also fix SftpFileSystemProvider.copy() to close the SftpClient it uses. SftpFileSystemProvider.newDirectoryStream() and newFileChannel() already do close the SftpClient used. Fix the SftpFileSystemTest to properly close SftpClients and DirectoryStreams. Bug: apache#294
SftpFileSystem.getClient() returns reference-counted wrapper instances that need to be closed to avoid a memory leak via ThreadLocals. Make sure that the streams returned by SftpFileSystemProvider.newInputStream() and newOutputStream() do close the client. Also fix SftpFileSystemProvider.copy() to close the SftpClient it uses. SftpFileSystemProvider.newDirectoryStream() and newFileChannel() already do close the SftpClient used. Fix the SftpFileSystemTest to properly close SftpClients and DirectoryStreams. Bug: apache#294
hello, how can i get the code without memory leak? it seems that the problem is not fixed in version 2.9.2 |
@tomaswolf |
I cannot reproduce this. Can you show fully working test code using a thread pool (I presume from an ExecutorService) that downloads a number of files and where at the end, when all downloads are done, there are extra ThreadLocals in the pool threads? |
Version
master
Bug description
The newInputStream and newOutputStream methods of SftpFileSystemProvider call the SftpFileSystem#getClient() method without call the close() method:

Actual behavior
I have a thread that uploads a file to the server every 10 seconds, as time goes by, the heap memory will get bigger and bigger, and eventually the heap memory will occur out of memory:

When I exported the heap memory, I found that there are a lot of objects of org.apache.sshd.sftp.client.fs.SftpFileSystem$Wrapper

Expected behavior
When the program is running normally, the heap memory will not overflow
Relevant log output
No response
Other information
No response
The text was updated successfully, but these errors were encountered: