Skip to content

Commit

Permalink
Added default timeout on connection.receive() (#257)
Browse files Browse the repository at this point in the history
* Added default timeout on receive() to prevent disconnect hanging against server that died after connecting.

* Added a default timeout when connecting

* Mistakenly forgot to change back the default of connection.receive()
  • Loading branch information
Robbert-Brand authored Jan 8, 2024
1 parent 37512ee commit 7d6abeb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/smbclient/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def dfs_request(tree: TreeConnect, path: str) -> DFSReferralResponse:
return dfs_response


def delete_session(server, port=445, connection_cache=None):
def delete_session(server, port=445, connection_cache=None, timeout=60):
"""
Deletes the connection in the connection pool for the server specified. This will also close all sessions
associated with the connection.
Expand All @@ -226,7 +226,7 @@ def delete_session(server, port=445, connection_cache=None):
connection = connection_cache.get(connection_key, None)
if connection:
del connection_cache[connection_key]
connection.disconnect(close=True)
connection.disconnect(close=True, timeout=timeout)


def get_smb_tree(
Expand Down
4 changes: 2 additions & 2 deletions src/smbprotocol/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ def connect(self, dialect=None, timeout=60, preferred_encryption_algos=None, pre
elif context_type == NegotiateContextType.SMB2_SIGNING_CAPABILITIES:
self.signing_algorithm_id = context["data"]["signing_algorithms"][0]

def disconnect(self, close=True):
def disconnect(self, close=True, timeout=None):
"""
Closes the connection as well as logs off any of the
Disconnects the TCP connection and shuts down the socket listener
Expand All @@ -943,7 +943,7 @@ def disconnect(self, close=True):
# We cannot close the session or tree if the socket has been closed.
if close and self.transport.connected:
for session in list(self.session_table.values()):
session.disconnect(True)
session.disconnect(True, timeout=timeout)

log.info("Disconnecting transport connection")
self.transport.close()
Expand Down
4 changes: 2 additions & 2 deletions src/smbprotocol/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def connect(self):
log.info("Verifying the SMB Setup Session signature as auth is successful")
self.connection.verify_signature(response, self.session_id, force=True)

def disconnect(self, close=True):
def disconnect(self, close=True, timeout=None):
"""
Logs off the session
Expand All @@ -418,7 +418,7 @@ def disconnect(self, close=True):
request = self.connection.send(logoff, sid=self.session_id)

log.info("Session: %s - Receiving Logoff response", self.username)
res = self.connection.receive(request)
res = self.connection.receive(request, timeout=timeout)
res_logoff = SMB2Logoff()
res_logoff.unpack(res["data"].get_value())
log.debug(res_logoff)
Expand Down

0 comments on commit 7d6abeb

Please sign in to comment.