Skip to content

Commit 5485ea9

Browse files
fix: prevent FFI event flooding and memory leak in progress callbacks
This commit addresses two critical issues in the FFI layer: 1. Event Throttling: Limit event draining to 500 events per call to prevent UI/main thread flooding. Remaining events stay queued for the next drain. 2. Memory Leak Fix: Properly free heap-allocated stage_message strings in FFIDetailedSyncProgress after each progress callback to avoid per-callback memory leaks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent bba5e0c commit 5485ea9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

dash-spv-ffi/src/client.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,14 @@ impl FFIDashSpvClient {
239239
return;
240240
};
241241
let callbacks = self.event_callbacks.lock().unwrap();
242+
// Prevent flooding the UI/main thread by limiting events per drain call.
243+
// Remaining events stay queued and will be drained on the next tick.
244+
let max_events_per_call: usize = 500;
245+
let mut processed: usize = 0;
242246
loop {
247+
if processed >= max_events_per_call {
248+
break;
249+
}
243250
match rx.try_recv() {
244251
Ok(event) => match event {
245252
dash_spv::types::SpvEvent::BalanceUpdate {
@@ -354,6 +361,7 @@ impl FFIDashSpvClient {
354361
break;
355362
}
356363
}
364+
processed += 1;
357365
}
358366
}
359367
}
@@ -823,6 +831,13 @@ pub unsafe extern "C" fn dash_spv_ffi_client_sync_to_tip_with_progress(
823831
// and accessed through thread-safe mechanisms. The registry ensures
824832
// proper lifetime management without raw pointer passing across threads.
825833
callback(ffi_progress.as_ref(), *user_data);
834+
835+
// Free any heap-allocated strings inside the progress struct
836+
// to avoid leaking per-callback allocations (e.g., stage_message).
837+
unsafe {
838+
// Destroy stage_message allocated in FFIDetailedSyncProgress::from
839+
crate::types::dash_spv_ffi_string_destroy(std::ptr::read(&ffi_progress.stage_message));
840+
}
826841
}
827842
}
828843
}

0 commit comments

Comments
 (0)