Преглед на файлове

Add automatic rollback on drop for wallet transactions in FFI

Implements Drop trait for FfiWalletTransaction to ensure uncommitted database
transactions are automatically rolled back when dropped. This prevents
transaction leaks and ensures database consistency even when transactions are
not explicitly committed.
Cesar Rodas преди 3 месеца
родител
ревизия
548aa46304
променени са 1 файла, в които са добавени 13 реда и са изтрити 2 реда
  1. 13 2
      crates/cdk-ffi/src/database.rs

+ 13 - 2
crates/cdk-ffi/src/database.rs

@@ -826,13 +826,24 @@ where
 
 /// Transaction wrapper for FFI
 pub(crate) struct FfiWalletTransaction {
-    tx: Mutex<Option<DynWalletDatabaseTransaction>>,
+    tx: Arc<Mutex<Option<DynWalletDatabaseTransaction>>>,
+}
+
+impl Drop for FfiWalletTransaction {
+    fn drop(&mut self) {
+        let tx = self.tx.clone();
+        tokio::spawn(async move {
+            if let Some(s) = tx.lock().await.take() {
+                let _ = s.rollback().await;
+            }
+        });
+    }
 }
 
 impl FfiWalletTransaction {
     pub fn new(tx: DynWalletDatabaseTransaction) -> Arc<Self> {
         Arc::new(Self {
-            tx: Mutex::new(Some(tx)),
+            tx: Arc::new(Mutex::new(Some(tx))),
         })
     }
 }