Skip to content

Close connection after error on open#351

Merged
warmwaffles merged 1 commit into
elixir-sqlite:mainfrom
katafrakt:close-after-open-error
Jul 14, 2026
Merged

Close connection after error on open#351
warmwaffles merged 1 commit into
elixir-sqlite:mainfrom
katafrakt:close-after-open-error

Conversation

@katafrakt

Copy link
Copy Markdown
Contributor

As per documentation, even when the database opening failed, we should still run sqlite3_close() to free the resources:

Whether or not an error occurs when it is opened, resources associated with the database connection handle should be released by passing it to sqlite3_close() when it is no longer required.

I verified this against Python implementation, which does this too. Python also calls assert to make sure that closing was clean (it should be per a comment there), but I think it's not a good idea for a NIF to do such thing.

AI disclosure

The issue was flagged by an AI review while I was working on SQLite backend for Expert. The rest of the work was human.

This is a behaviour that docs describe as correct:

> Whether or not an error occurs when it is opened, resources associated
> with the database connection handle should be released by passing it
> to sqlite3_close() when it is no longer required.

https://www.sqlite.org/c3ref/open.html

@warmwaffles warmwaffles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't read the documentation for this:

Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer argument is a harmless no-op.

Neat.

I appreciate the disclosure.

@warmwaffles warmwaffles merged commit e94f2d6 into elixir-sqlite:main Jul 14, 2026
9 checks passed
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.

2 participants