diff --git a/libsql/Cargo.toml b/libsql/Cargo.toml index e8a7e0f909..c6cc39c8e9 100644 --- a/libsql/Cargo.toml +++ b/libsql/Cargo.toml @@ -49,13 +49,16 @@ chrono = { version = "0.4", optional = true } [dev-dependencies] criterion = { version = "0.5", features = ["html_reports", "async", "async_futures", "async_tokio"] } -pprof = { version = "0.14.0", features = ["criterion", "flamegraph"] } + tokio = { version = "1.29.1", features = ["full"] } tokio-test = "0.4" tracing-subscriber = "0.3" tempfile = { version = "3.7.0" } rand = "0.8.5" +[target.'cfg(not(windows))'.dev-dependencies] +pprof = { version = "0.14.0", features = ["criterion", "flamegraph"] } + [features] default = ["core", "replication", "remote", "sync", "tls"] core = [ diff --git a/libsql/src/local/connection.rs b/libsql/src/local/connection.rs index 4c54f5acd0..40f4065d0b 100644 --- a/libsql/src/local/connection.rs +++ b/libsql/src/local/connection.rs @@ -108,8 +108,14 @@ impl Connection { pub fn disconnect(&mut self) { if Arc::get_mut(&mut self.drop_ref).is_some() { unsafe { - libsql_sys::ffi::sqlite3_close_v2(self.raw); - self.raw = std::ptr::null_mut(); + if !self.raw.is_null() { + let old_data = ffi::sqlite3_update_hook(self.raw, None, std::ptr::null_mut()); + if !old_data.is_null() { + let _ = Box::from_raw(old_data as *mut Container); + } + libsql_sys::ffi::sqlite3_close_v2(self.raw); + self.raw = std::ptr::null_mut(); + } }; } } diff --git a/libsql/tests/integration_tests.rs b/libsql/tests/integration_tests.rs index 697ac220ef..fe5c501575 100644 --- a/libsql/tests/integration_tests.rs +++ b/libsql/tests/integration_tests.rs @@ -99,6 +99,26 @@ async fn add_update_hook() { ); } +#[tokio::test] +async fn update_hook_closure_dropped_on_disconnect() { + let conn = setup().await; + + let flag = Arc::new(()); + let weak = Arc::downgrade(&flag); + + conn.add_update_hook(Box::new(move |_op, _db, _table, _row_id| { + let _ = &flag; + })) + .unwrap(); + + assert!(weak.upgrade().is_some()); + drop(conn); + assert!( + weak.upgrade().is_none(), + "update hook closure captures must be deallocated on connection drop" + ); +} + #[tokio::test] async fn connection_drops_before_statements() { let db = Database::open(":memory:").unwrap();