Skip to content

Commit 2a3d241

Browse files
await worker shutdown after dropping SqliteConnection
Signed-off-by: Andrew Whitehead <[email protected]>
1 parent fe094dc commit 2a3d241

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

sqlx-core/src/sqlite/connection/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ impl Connection for SqliteConnection {
6464

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

sqlx-core/src/sqlite/statement/worker.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,19 @@ impl StatementWorker {
140140
/// A `WorkerCrashed` error may be returned if the thread has already stopped.
141141
/// Subsequent calls to `step()`, `reset()`, or this method will fail with
142142
/// `WorkerCrashed`. Ensure that any associated statements are dropped first.
143-
pub(crate) async fn shutdown(&mut self) -> Result<(), Error> {
143+
pub(crate) fn shutdown(&mut self) -> impl Future<Output = Result<(), Error>> {
144144
let (tx, rx) = oneshot::channel();
145145

146-
self.tx
146+
let send_res = self
147+
.tx
147148
.send(StatementWorkerCommand::Shutdown { tx })
148-
.map_err(|_| Error::WorkerCrashed)?;
149+
.map_err(|_| Error::WorkerCrashed);
149150

150-
rx.await.map_err(|_| Error::WorkerCrashed)
151+
async move {
152+
send_res?;
153+
154+
// wait for the response
155+
rx.await.map_err(|_| Error::WorkerCrashed)
156+
}
151157
}
152158
}

0 commit comments

Comments
 (0)