Skip to content

Commit

Permalink
chore: handle close when runtime is dropped (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq authored Nov 14, 2024
1 parent 6cd446a commit dd01ff5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
target: [ 'x86_64-unknown-linux-gnu', 'aarch64-unknown-linux-gnu', 'x86_64-pc-windows-msvc', 'x86_64-apple-darwin', 'aarch64-apple-darwin' ]
target: [ 'x86_64-unknown-linux-gnu', 'aarch64-unknown-linux-gnu', 'x86_64-pc-windows-msvc', 'x86_64-apple-darwin', 'aarch64-apple-darwin', 'wasm32-unknown-unknown' ]
steps:
- uses: actions/checkout@v2
- name: Cache
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ tokio = { version = "1.36", features = ["rt", "sync"] }
quick_cache = "0.6.0"
vart = "0.7.0"
revision = "0.10.0"
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.15", features = ["js"] }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
Expand Down
19 changes: 12 additions & 7 deletions src/storage/kv/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,18 @@ 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 {
// TODO: use log/tracing instead of eprintln
eprintln!("Error occurred while closing the kv store: {}", err);
}
});
// 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 {
eprintln!("No runtime available for closing the store correctly");
}
}
}
}
Expand Down

0 comments on commit dd01ff5

Please sign in to comment.