jborean93 / smbprotocol

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

Help Wanted: smbclient.register_session periodically taking > 1 min to create connection #176

Open MacHu-GWU opened 2 years ago

MacHu-GWU commented 2 years ago

Hi @jborean93, first I appreciate you created this awesome library!

I find out that after around 10 - 15 minutes every time I created a session with smbclient.register_session to the same server, the next session takes like 1 minutes to create. In average it only takes 1~2 seconds.

The server we are using is AWS FSx Windows Server, and I close the connection everytime I finish the job. I think I understand how you implement the cache. However I am not running a long running script. I am like run a python script then go, and run another one (they all have some SMBclient read / write logic). I don't think the cache are used between different python script.py. Also I don't think there would be lots of open connection on the server side since I close those connection after the work.

Any Idea how it happens? Thank you again for this awesome library.

ses = smbclient.register_session(...)
# do something
ses.connection.disconnect()
jborean93 commented 2 years ago

Sorry for the delay in the reply. I'm unsure as to why this is happening. It sounds like it might be trying to close down the existing connection that might be broken and causing a long time to disconnect. If you are calling ses.connection.disconnect() it will be manually disconnecting the connection but the pool doesn't have any idea that has happened so it fails to remove it from the pool. The next subsequent connection will attempt to revive the connection.

I'll have to find some time to try and play around with it some more and see if I can reproduce the problem. For now I would recommend avoiding manually disconnecting the connection through the session object. If you wish to do it then disconnect the entire pool through smbclient.reset_connection_cache().

import smbclient

smbclient.register_session(...)
...

smbclient.reset_connection_cache()

Or if you wish to keep control over what is being reset you can use a specific cache.

import smbclient

server_cache = {}
smbclient.register_session(..., connection_cache=server_cache)

smbclient.listdir(..., connection_cache=server_cache)
...

smbclient.reset_connection_cache(connection_cache=server_cache)

The downside of the latter approach is you need to specify the cache everytime you do an operation on the server requested.

MacHu-GWU commented 2 years ago

@jborean93 it does help! Thank you for your reply!