jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
318 stars 73 forks source link

Memory Leak in listdir( ) #127

Closed MichaelKorn closed 3 years ago

MichaelKorn commented 3 years ago

I stumbled upon a memory leak and located it. Since listdir( ) is used very frequently in my application (and Python's memory wastage is extreme) the application uses a few GB of RAM every day.

The problem is in: https://github.com/jborean93/smbprotocol/blob/e4c8b6af0bbca70cc7a27ccac6a95ef1a97b6c80/smbprotocol/connection.py#L926-L937 The request of listdir( ) is terminated here with status=NtStatus.STATUS_NO_MORE_FILES which raises a NoMoreFiles exception in L928. This is expected in https://github.com/jborean93/smbprotocol/blob/v1.5.1/smbclient/_io.py#L626-L629 and seems to be wanted by design. Unfortunately, the removal of the request from the Dict (necessary for the release of memory) is in L935 and this line is not reached anymore due to the exception. This problem is likely to arise whenever an exception occurs, this case is particularly problematic because it is a regular case.

jborean93 commented 3 years ago

Thanks for the pickup on this one. I believe https://github.com/jborean93/smbprotocol/pull/129 should solve this problem as it will remove the outstanding request on a failure like it should have done beforehand.