jborean93 / smbprotocol

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

disconnect on exit very slow #297

Open didacarbones opened 3 weeks ago

didacarbones commented 3 weeks ago

I am experiencing severe delays on the main python process exiting. I can take about 5 minutes to exit. I have to force it to exit by repeatedly sending SIGINT. I can see that the exception stacktrace printed shows that it is waiting on the disconnect request response here:

            iter_timeout = int(max(timeout - (time.time() - start_time), 1)) if timeout is not None else None
            if not request.response_event.wait(timeout=iter_timeout):
                value = request.message["message_id"].get_value()
                raise SMBException(
                    f"Connection timeout of {timeout} seconds exceeded while waiting for a message id {value} "
                    "response from the server"
                )

This piece of code is reached atexit after calling smbclient.reset_connection_cache. I have tried calling it myself, before exiting, or keeping a smaller connection cache as suggested in #176. I have only had success by copying the contents of reset_connection_cache over, and passing a timeout to the connection.disconnect call. I can create a PR, but I wanted to make an issue first, so we can start a discussion on whether this is a sensible thing to do. My suggestion is to add a disconnect timeout parameter in ClientConfig, and using that in reset_connection_cache. I would imagine a sensible default would be 3-5 seconds.

jborean93 commented 3 weeks ago

I think adding an option to the ClientConfig makes sense to shorten the time It would be nice to find out why something is taking 5 minutes to disconnect though, maybe the default doesn't make too much sense and we should just leave the server to reap any outstanding connections and keep our tidy up to be around 1-5 seconds.

didacarbones commented 2 weeks ago

Sounds good. Shall I make a PR, or is is something you'd want to tackle?

jborean93 commented 2 weeks ago

If you feel up to doing it then that would be great. Otherwise I’ll get to it when I can find the time.