Skip to content

Commit e94f2d6

Browse files
authored
Close connection after error on open (#351)
As per [documentation](https://www.sqlite.org/c3ref/open.html), 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](https://www.sqlite.org/c3ref/sqlite3.html) handle > should be released by passing it to [sqlite3_close()](https://www.sqlite.org/c3ref/close.html) > when it is no longer required. I verified this against Python implementation, which [does this too](https://github.com/python/cpython/blob/357c6500589ca7e065a6c263accfa1307d93c990/Modules/_sqlite/connection.c#L334-L339). 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.
1 parent cbda961 commit e94f2d6

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

c_src/sqlite3_nif.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
461461

462462
rc = sqlite3_open_v2((char*)bin.data, &db, flags, NULL);
463463
if (rc != SQLITE_OK) {
464+
sqlite3_close_v2(db);
464465
return make_error_tuple(env, am_database_open_failed);
465466
}
466467

0 commit comments

Comments
 (0)