Skip to content

Not assigning cursor from DuckDBPyConnection.cursor() causes Connection Error when fetching data #315

Closed

Description

@provinzkraut

What happens?

When obtaining a cursor from DuckDBPyConnection.cursor(), and not assigning it to a variable, that cursor cannot be used to fetch any data, and will raise a Connection Error.

It seems that not assigning the cursor causes it to be garbage collected immediately, which should not be the case, there's still a reference being held to it.

To Reproduce

This raises _duckdb.ConnectionException: Connection Error: Connection has already been closed:

import duckdb

conn = duckdb.connect()
conn.cursor().sql("SELECT 1 as foo").fetchall()

while this works

import duckdb

conn = duckdb.connect()

cur = conn.cursor()
cur.sql("SELECT 1 as foo").fetchall()

OS:

macOS

DuckDB Package Version:

1.4.3

Python Version:

3.13.7

Full Name:

Janek Nouvertné

Affiliation:

nope

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have not tested with any build

Did you include all relevant data sets for reproducing the issue?

Yes

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant configuration to reproduce the issue?

  • Yes, I have

Activity

  1. evertlammerts commented on Feb 16, 2026

    @evertlammerts
    Member

    That's a bug indeed, in the relational API.

    To work around, use execute():

    import duckdb
    
    conn = duckdb.connect()
    conn.cursor().execute("SELECT 1 as foo").fetchall()
    
  2. provinzkraut commented on Feb 16, 2026

    @provinzkraut
    Author

    I'm using the .pl method is only available on the relational api, but using the cursor as a context manager also works:

    import duckdb
    
    conn = duckdb.connect()
    
    with conn.cursor() as cur:
        cur.sql("SELECT 1 as foo").fetchall()
  3. added a commit that references this issue on Feb 19, 2026
    2acbdba
  4. evertlammerts commented on Feb 19, 2026

    @evertlammerts
    Member

    Fixed in #329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions