Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 8b75dc8

Browse files
authored
libsql-bindings: auto-finalize dangling statements (#523)
Right before closing a db connection, we go over active prepared statements, if any, and finalize them forcefully to free their memory. Normally we don't leave any prepared statements dangling, but extensions can do so.
1 parent be4e5e9 commit 8b75dc8

File tree

1 file changed

+19
-0
lines changed
  • sqld-libsql-bindings/src

1 file changed

+19
-0
lines changed

sqld-libsql-bindings/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ impl Deref for Connection<'_> {
3535
}
3636
}
3737

38+
impl Drop for Connection<'_> {
39+
fn drop(&mut self) {
40+
unsafe {
41+
let db = self.conn.handle();
42+
if db.is_null() {
43+
return;
44+
}
45+
let mut stmt = ffi::sqlite3_next_stmt(db, std::ptr::null_mut());
46+
while !stmt.is_null() {
47+
let rc = ffi::sqlite3_finalize(stmt);
48+
if rc != ffi::SQLITE_OK {
49+
tracing::error!("Failed to finalize a dangling statement: {rc}")
50+
}
51+
stmt = ffi::sqlite3_next_stmt(db, stmt);
52+
}
53+
}
54+
}
55+
}
56+
3857
impl<'a> Connection<'a> {
3958
/// returns a dummy, in-memory connection. For testing purposes only
4059
pub fn test(_: &mut ()) -> Self {

0 commit comments

Comments
 (0)