Skip to content

Commit

Permalink
await worker shutdown after dropping SqliteConnection
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Sep 23, 2021
1 parent fe094dc commit 2a3d241
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 6 additions & 3 deletions sqlx-core/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ impl Connection for SqliteConnection {

fn close(mut self) -> BoxFuture<'static, Result<(), Error>> {
Box::pin(async move {
self.statements.clear();
self.statement.take();
self.worker.shutdown().await
let shutdown = self.worker.shutdown();
// Drop the statement worker and any outstanding statements, which should
// cover all references to the connection handle outside of the worker thread
drop(self);
// Ensure the worker thread has terminated
shutdown.await
})
}

Expand Down
14 changes: 10 additions & 4 deletions sqlx-core/src/sqlite/statement/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,19 @@ impl StatementWorker {
/// A `WorkerCrashed` error may be returned if the thread has already stopped.
/// Subsequent calls to `step()`, `reset()`, or this method will fail with
/// `WorkerCrashed`. Ensure that any associated statements are dropped first.
pub(crate) async fn shutdown(&mut self) -> Result<(), Error> {
pub(crate) fn shutdown(&mut self) -> impl Future<Output = Result<(), Error>> {
let (tx, rx) = oneshot::channel();

self.tx
let send_res = self
.tx
.send(StatementWorkerCommand::Shutdown { tx })
.map_err(|_| Error::WorkerCrashed)?;
.map_err(|_| Error::WorkerCrashed);

rx.await.map_err(|_| Error::WorkerCrashed)
async move {
send_res?;

// wait for the response
rx.await.map_err(|_| Error::WorkerCrashed)
}
}
}

0 comments on commit 2a3d241

Please sign in to comment.