denisenkom / pytds

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error: unpack_from requires a buffer of at least 8 bytes

infinitewarp opened this issue · comments

This appears to be a duplicate of #37, but since that issue is closed and doesn't appear to be getting any attention, here I am. If someone reopens #37 and provides a real fix, please close this. 🎉

Using a recent version of master from github source newer than the release in pypi is supposed to resolve this issue, but I intermittently get this error from pytds:

...
  File "create-budget.py", line 165, in execute
    return cur.fetchall()
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/__init__.py", line 790, in fetchall
    return list(row for row in self)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/__init__.py", line 790, in <genexpr>
    return list(row for row in self)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/__init__.py", line 793, in __next__
    row = self.fetchone()
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/__init__.py", line 766, in fetchone
    row = self._session.fetchone()
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 1586, in fetchone
    if not self.next_row():
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 1597, in next_row
    self.process_token(marker)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 1545, in process_token
    return handler(self)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 1664, in <lambda>
    tds_base.TDS_NBC_ROW_TOKEN: lambda self: self.process_nbcrow(),
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 734, in process_nbcrow
    value = curcol.serializer.read(r)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds_types.py", line 394, in read
    return self.subtypes[size].read(r)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds_types.py", line 1255, in read
    days, t = r.unpack(self._struct)
  File "/Users/bsmith/.virtualenvs/sqlserver/lib/python3.6/site-packages/pytds/tds.py", line 167, in unpack
    return struc.unpack_from(buf, offset)
struct.error: unpack_from requires a buffer of at least 8 bytes

Usually the first few operations (selects, inserts, deletes) to the database succeed, but sometimes unpredictably a random subsequent operation will fail with that error.

I've tried setting an arbitrarily large login_timeout value (currently at 1200) on the connect call (as a possible fix suggested in #37), but that doesn't seem to make any difference.

Also, it shouldn't make any difference, but I'm establishing the connection and cursors as context managers like this:

with pytds.connect(**db_args) as conn:
    # magic happens here

I don't know what has changed, but I'm not seeing this error any more. Maybe there was something else funky on my network or SQL Server instance that was somehow borking my connections in a weird way. ¯\_(ツ)_/¯