Skip to content

Conversation

@prmoore77
Copy link
Contributor

@prmoore77 prmoore77 commented Dec 15, 2025

Closes #3713

@prmoore77 prmoore77 requested a review from lidavidm as a code owner December 15, 2025 22:26
@github-actions github-actions bot added this to the ADBC Libraries 22 milestone Dec 15, 2025
@prmoore77 prmoore77 force-pushed the fix-auto-close-dbapi-cursors branch from be5af22 to 93790ff Compare December 15, 2025 22:38
@lidavidm lidavidm changed the title fix(python/adbc_driver_manager): auto-close open cursors when closing… fix(python/adbc_driver_manager): close Cursors when closing Connection Dec 15, 2025
Copy link
Member

@amoeba amoeba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me. I also manually tested flightsql, mssql, and postgresql and the tests you added here run correctly for those drivers as well.

One comment about a docstring.

Co-authored-by: Bryce Mecum <[email protected]>
@prmoore77
Copy link
Contributor Author

Thanks, @amoeba !

@amoeba
Copy link
Member

amoeba commented Dec 16, 2025

PS I'll defer to @lidavidm's review before merging.

Copy link
Member

@lidavidm lidavidm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should file a separate issue for this - it's fine to have Python guard this but I think the current issue is more about having drivers themselves handle this.

@prmoore77
Copy link
Contributor Author

I think we should file a separate issue for this - it's fine to have Python guard this but I think the current issue is more about having drivers themselves handle this.

Is it ok to merge this in the meantime - for Python users to benefit?

@lidavidm
Copy link
Member

Please make a separate issue to attach to this PR so I can merge.

@lidavidm
Copy link
Member

lidavidm commented Dec 16, 2025

Ah, the issue sounds like Python should handle it...my original comment was about having the drivers handle it so that's not what I expected. Ok then. Let me just file the new issue for the drivers so that we don't lose the context.


# Close all open cursors first to avoid RuntimeError from
# AdbcConnection about open children
for cursor in list(self._cursors):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed now.

Comment on lines 381 to 384
# Close all open cursors first to avoid RuntimeError from
# AdbcConnection about open children
for cursor in list(self._cursors):
cursor.close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If close raises, the other references won't get closed. Can we handle that? (We should catch Exception and either prepare to rethrow it, or add it as a suppressed exception.)

For testing purposes, we could factor it out into a small internal helper that can then be directly tested with non-Cursor objects containing a close method.

Copy link
Contributor Author

@prmoore77 prmoore77 Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to do here - b/c if we fail to close one (or more) of the connection's cursors for some reason - won't we hit the original error?

RuntimeError: Cannot close AdbcConnection with open AdbcStatement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but we should at least try and clean up all of the ones we can, IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - I've pushed up a change suppressing cursor close errors. The original error should still bubble up in case we leave one opened.

@prmoore77 prmoore77 requested a review from lidavidm December 16, 2025 17:10
Comment on lines 386 to 387
except Exception:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to do this, IMO it should at least get emitted as a ResourceWarning or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified to emit a ResourceWarning.

@prmoore77 prmoore77 requested a review from lidavidm December 16, 2025 17:46
@lidavidm
Copy link
Member

The formatter is unhappy :)

@prmoore77
Copy link
Contributor Author

The formatter is unhappy :)

This should be fixed now. Thanks for your help/review.

@prmoore77
Copy link
Contributor Author

I've made another formatting fix

@lidavidm lidavidm merged commit 8286d17 into apache:main Dec 17, 2025
63 of 70 checks passed
@prmoore77 prmoore77 deleted the fix-auto-close-dbapi-cursors branch January 6, 2026 15:45
prmoore77 added a commit to gizmodata/dbt-gizmosql that referenced this pull request Jan 9, 2026
Updated ADBC Flight SQL Client driver version.
Fixed cursor leak bug.
Removed ADBC Connection proxy - b/c the cursor close bug has been fixed - apache/arrow-adbc#3810
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

Successfully merging this pull request may close these issues.

python/adbc_driver_manager: support closing connection with unclosed cursors

3 participants