Skip to content

Commit

Permalink
chore: handle close when runtime is dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq committed Nov 14, 2024
1 parent 6cd446a commit 75ea7f3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/storage/kv/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,28 @@ impl Store {
impl Drop for Store {
fn drop(&mut self) {
if let Some(inner) = self.inner.take() {
// Close the store asynchronously
tokio::spawn(async move {
if let Err(err) = inner.close().await {
// Try to get existing runtime handle first
if let Ok(handle) = tokio::runtime::Handle::try_current() {
// We're in a runtime, spawn normally
handle.spawn(async move {
if let Err(err) = inner.close().await {
// TODO: use log/tracing instead of eprintln
eprintln!("Error closing store: {}", err);
}
});
} else {
// No runtime, create a temporary one
if let Ok(rt) = tokio::runtime::Runtime::new() {
// Block until close completes
if let Err(err) = rt.block_on(inner.close()) {
// TODO: use log/tracing instead of eprintln
eprintln!("Error closing store: {}", err);
}
} else {
// TODO: use log/tracing instead of eprintln
eprintln!("Error occurred while closing the kv store: {}", err);
eprintln!("Failed to create runtime for store cleanup");
}
});
}
}
}
}
Expand Down

0 comments on commit 75ea7f3

Please sign in to comment.