Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible issue in class DirectTCPSessionMessage #157

Open
pasiorovuo opened this issue May 20, 2020 · 3 comments
Open

Possible issue in class DirectTCPSessionMessage #157

pasiorovuo opened this issue May 20, 2020 · 3 comments

Comments

@pasiorovuo
Copy link

pasiorovuo commented May 20, 2020

We have been experiencing an odd issue where pysmb fails after around 15 minutes, with the exception below (date + time added by execution script):

Wed May 20 10:10:45 UTC 2020
Traceback (most recent call last):
  File "transfer.py", line 116, in <module>
    main()
  File "transfer.py", line 110, in main
    timeout=CIFS_TIMEOUT
  File "/usr/local/lib/python3.7/site-packages/smb/SMBConnection.py", line 365, in storeFile
    return self.storeFileFromOffset(service_name, path, file_obj, 0, True, timeout)
  File "/usr/local/lib/python3.7/site-packages/smb/SMBConnection.py", line 396, in storeFileFromOffset
    self._pollForNetBIOSPacket(timeout)
  File "/usr/local/lib/python3.7/site-packages/smb/SMBConnection.py", line 634, in _pollForNetBIOSPacket
    self.feedData(data)
  File "/usr/local/lib/python3.7/site-packages/nmb/base.py", line 49, in feedData
    length = self.data_nmb.decode(self.data_buf, offset)
  File "/usr/local/lib/python3.7/site-packages/nmb/nmb_structs.py", line 60, in decode
    raise NMBError("Invalid protocol header for Direct TCP session message")
nmb.nmb_structs.NMBError: Invalid protocol header for Direct TCP session message
Wed May 20 10:15:45 UTC 2020

We initiated the connection with following command:

    connection = smb.SMBConnection.SMBConnection(
        username=parsed.username or '',
        password=parsed.password or '',
        my_name='',
        remote_name='',
        use_ntlm_v2=True,
        is_direct_tcp=True
    )

is_direct_tcp being the thing to note here.

I took a look at the source code of version 1.2.1 and traced back to where the exception originated, and this stands out in method decode on line 57 of nmb_structs.py:

length = struct.unpack(self.HEADER_STRUCT_FORMAT, data[offset:offset+self.HEADER_STRUCT_SIZE])[0]

On line 33 in same file (decode() of NMBSessionMessage) we have:

self.type, self.flags, length = struct.unpack(self.HEADER_STRUCT_FORMAT, data[offset:offset+self.HEADER_STRUCT_SIZE])

Difference being that the length is the 3rd element of the tuple returned by decode() of NMBSessionMessage and 1st in DirectTCPSessionMessage's decode(). Could this be the issue here?

@miketeo
Copy link
Owner

miketeo commented May 21, 2020

@pasiorovuo : May I know if there is prolonged time lapse where the SMBConnection instance was left idling in your application? You have mentioned that pysmb fails after around 15 minutes,

SMBConnection was not designed to be left idle by the application. When SMBConnection is left idle, it is unable to keep the SMB connection "alive", so the server might just assume the connection is dead and close the connection.

@pasiorovuo
Copy link
Author

We thought about that too. However, in our case we move (read, copy, delete from SMB location) small files, ranging from few kb to 100 kb from an on-premise location to AWS S3. The process was moving around 10 to 15 files per second without idle periods.

@pasiorovuo
Copy link
Author

The issue can be worked around with using is_direct_tcp=False during creation of the connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants