Skip to content

Executing stored procedures using TVP hard crashes python GC in python 3.11 #1450

@Sander-PGGM

Description

@Sander-PGGM

Hello,

Executing stored procedures using TVP hard crashes python GC in python 3.11. We think the issue has something to do with freeing memory inside cursor.execute(sql, *parameters). Which then gets double freed inside gc.collect().

Environment

  • Python: 3.11 (It seems to also occur in 3.13)
  • pyodbc: 5.3.0
  • DB: MS SQL Server 2022
  • Driver: Microsoft ODBC Driver 18 for SQL Server (18.5.1.1)

After writing using a stored procedure that uses a TVP to write data the python process crashes running gc.collect(). params contains regulair parameters and one set of TVP.

Example:

conn = pyodbc.connect(connection_string, attrs_before={1256: bytearray(token_struct)})
sql = f'EXEC [{schema}].[{procedure}]'

# Params contains the TPV values as `{data : (Tuple)}`
with self._connection.cursor() as cursor:
    if params:
        sql += ' ' + ', '.join(f'@{p}=?' for p in params.keys())
        asdf = tuple(params.values())
        cursor.execute(sql, asdf)
    else:
        cursor.execute(sql)
  gc.collect(0)
  gc.collect(1)
  gc.collect(2)

CPython crashes inside gc.collect(2) without any form of exception.

Result

Process finished with exit code -1073741819 (0xC0000005)

When GC is enabled, it crashes randomly. We think when garbage collection is triggered. With gc.disable() it doesn't crash.

After some debugging we found that it only happend with rows that contain None values where there should be a nullable float or decimal.

If you need any more information please let me know.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions