jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
309 stars 72 forks source link

Maximum recursion depth exceeded for large DFS server share #281

Open jackson-theisen opened 4 weeks ago

jackson-theisen commented 4 weeks ago

I am attempting to write files to a DFS server share that has hundreds of directories. It appears that regardless of the path passed in to smbclient.open_file(), it will attempt to read the entire file tree via get_smb_tree(). In my case, this results in [ERROR] RecursionError: maximum recursion depth exceeded while calling a Python object.

My hacky attempt at fixing the issue was to increase the recursion depth limit via sys.setrecursionlimit(), which ultimately failed and is not ideal regardless of whether it works.

Here is the relevant code snippet i'm using:

smbclient.ClientConfig(username=username, password=password, domain_controller=dc_hostname)
smbclient.register_session(server=dc_hostname, connection_timeout=5)

with smbclient.open_file(dest_file_path, mode="wb") as fd:
    s3_client.download_fileobj(SOURCE_BUCKET_NAME, k, fd)

and here is the full stack trace:

[ERROR] RecursionError: maximum recursion depth exceeded while calling a Python object
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 89, in lambda_handler
    with smbclient.open_file(dest_file_path, mode="wb") as fd:
  File "/var/task/smbclient/_os.py", line 381, in open_file
    raw_fd = file_class(
  File "/var/task/smbclient/_io.py", line 362, in __init__
    tree, fd_path = get_smb_tree(path, **kwargs)
  File "/var/task/smbclient/_pool.py", line 287, in get_smb_tree
    ipc_tree = get_smb_tree(rf"\\{client_config.domain_controller}\IPC$", **get_kwargs)[0]
  File "/var/task/smbclient/_pool.py", line 287, in get_smb_tree
    ipc_tree = get_smb_tree(rf"\\{client_config.domain_controller}\IPC$", **get_kwargs)[0]
  File "/var/task/smbclient/_pool.py", line 287, in get_smb_tree
    ipc_tree = get_smb_tree(rf"\\{client_config.domain_controller}\IPC$", **get_kwargs)[0]
  [Previous line repeated 979 more times]
  File "/var/task/smbclient/_pool.py", line 252, in get_smb_tree
    client_config = ClientConfig()
  File "/var/task/smbclient/_pool.py", line 56, in __call__
    config.set(**kwargs)
  File "/var/task/smbclient/_pool.py", line 170, in set
    for key, value in config.items():

This is not necessarily a bug, but rather a request for guidance on any potential misconfigurations or solutions to get around the issue. Thanks!

jborean93 commented 1 week ago

Thanks for the report, it sounds as if it's getting in some sort of recursive loop which doesn't sound right. Unfortunately DFS resolution is a very tricky thing to work with. The best thing you can do is try to share as much details about the environment that you can. Things like

I do understand that the trace log can expose environment details so it may not be possible to share but sharing as much information as you can is ideal for me to try and figure out what might be going wrong here.