From c942c8262dabc47e403d23e8ca22f7cc0a1007f5 Mon Sep 17 00:00:00 2001 From: Wasim Date: Sun, 26 Jul 2026 10:09:40 +0530 Subject: [PATCH] fix: null sqlite3 handle after close to prevent double-free Set raw pointer to null after sqlite3_close_v2 in disconnect() to prevent use-after-free when the handle is closed twice. This fixes issue #2251 where LibsqlConnection::Drop calls disconnect() and then the inner Connection::Drop also calls disconnect(), causing a double-close of the sqlite3 handle. The fix ensures disconnect() is idempotent by nulling the raw pointer after the first close, so subsequent calls are safe no-ops. --- libsql/src/local/connection.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libsql/src/local/connection.rs b/libsql/src/local/connection.rs index 7012651699..4c54f5acd0 100644 --- a/libsql/src/local/connection.rs +++ b/libsql/src/local/connection.rs @@ -107,7 +107,10 @@ impl Connection { /// Disconnect from the database. pub fn disconnect(&mut self) { if Arc::get_mut(&mut self.drop_ref).is_some() { - unsafe { libsql_sys::ffi::sqlite3_close_v2(self.raw) }; + unsafe { + libsql_sys::ffi::sqlite3_close_v2(self.raw); + self.raw = std::ptr::null_mut(); + }; } }