//! Joining SQLite worker shutdown independently of a cancelled observer.
use tokio_rusqlite::Connection;
pub(crate) async fn close(connection: Connection) -> Result<(), String> {
// tokio-rusqlite 0.8 panics on its worker if close's response receiver is dropped.
// A blocking task keeps that receiver alive even when its caller is cancelled;
// runtime shutdown joins it instead of cancelling it with ordinary async tasks.
let runtime = tokio::runtime::Handle::current();
crate::task::joined(
tokio::task::spawn_blocking(move || {
runtime
.block_on(connection.close())
.map_err(|error| error.to_string())
})
.await,
)
.map_err(|error| error.to_string())?
}
/// A one-shot operation owns its worker until shutdown, even when the operation failed.
/// Preserve its original error; report a close failure only after a successful operation.
pub(super) async fn finish<T>(
connection: Connection,
result: Result<T, String>,
) -> Result<T, String> {
let closed = close(connection).await;
result.and_then(|value| closed.map(|()| value))
}
#[cfg(test)]
mod tests;