Skip to content

bulkcopy using service principal authentication freezes #662

Description

@nspcc-cm

Describe the bug

Now the service principal authentication is (meant to be) fixed when using bulk copy, I've attempted using it today.

However, I'm finding that bulkcopy freezes when using this authentication method.

Bulk copy does work when using the Default authentication method (and setting the appropriate environment variables which trigger it to use a service principal under the hood). But this explicit ServicePrincipal authentication route seems not to work.

To reproduce

Prerequisities: set the environment variables to the appropriate values.

import os
import mssql_python

cnxn_str = (
    f"Server={os.environ['DB_SERVER']};"
    f"Database={os.environ['DB_NAME']};"
    "Authentication=ActiveDirectoryServicePrincipal;"
    f"UID={os.environ['DB_SP_CLIENT_ID']};"
    f"PWD={os.environ['DB_SP_CLIENT_SECRET']};"
    "Encrypt=yes;"
)

with mssql_python.connect(cnxn_str, timeout=30) as cnxn:
    cursor = cnxn.cursor()
    
    cursor.execute("CREATE TABLE dbo.MyTable (Number INT, Letter NCHAR(1))")
    
    # Data can be inserted via execute() with INSERT
    cursor.execute("INSERT INTO dbo.MyTable (Number, Letter) VALUES (1, 'a');")
    print(cursor.execute("SELECT * FROM dbo.MyTable;").fetchone()) # prints (1, 'a')

    # ...but bulkcopy() will freeze when trying to insert data.
    data = [(2, 'b'), (3, 'c')] 
    cursor.bulkcopy("dbo.MyTable", data, timeout=30)
    print("This never prints. bulkcopy() will freeze and the timeout won't trigger.")

    cnxn.rollback()

The same happens for existing tables (ones I haven't created in the same session).

Expected behavior

Bulkcopy should not freeze.

Further technical details

Python version: 3.12.10
SQL Server version: Azure SQL managed instance
Operating system: Windows 11

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

area: bulk-copyIssues in cursor.bulkcopy()bugSomething isn't workingduplicateThis issue or pull request already existstriage doneIssues that are triaged by dev team and are in investigation.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions