Skip to content

Commit 5461eee

Browse files
committed
sqlite: use Weak poiter to StatementHandle in the worker
Otherwise some tests fail to close connection.
1 parent 5eebc05 commit 5461eee

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crossbeam_channel::{unbounded, Sender};
44
use either::Either;
55
use futures_channel::oneshot;
66
use libsqlite3_sys::{sqlite3_step, SQLITE_DONE, SQLITE_ROW};
7-
use std::sync::Arc;
7+
use std::sync::{Arc, Weak};
88
use std::thread;
99

1010
// Each SQLite connection has a dedicated thread.
@@ -19,7 +19,7 @@ pub(crate) struct StatementWorker {
1919

2020
enum StatementWorkerCommand {
2121
Step {
22-
statement: Arc<StatementHandle>,
22+
statement: Weak<StatementHandle>,
2323
tx: oneshot::Sender<Result<Either<u64, ()>, Error>>,
2424
},
2525
}
@@ -32,14 +32,19 @@ impl StatementWorker {
3232
for cmd in rx {
3333
match cmd {
3434
StatementWorkerCommand::Step { statement, tx } => {
35-
let status = unsafe { sqlite3_step(statement.0.as_ptr()) };
35+
let resp = if let Some(statement) = statement.upgrade() {
36+
let status = unsafe { sqlite3_step(statement.0.as_ptr()) };
3637

37-
let resp = match status {
38-
SQLITE_ROW => Ok(Either::Right(())),
39-
SQLITE_DONE => Ok(Either::Left(statement.changes())),
40-
_ => Err(statement.last_error().into()),
38+
let resp = match status {
39+
SQLITE_ROW => Ok(Either::Right(())),
40+
SQLITE_DONE => Ok(Either::Left(statement.changes())),
41+
_ => Err(statement.last_error().into()),
42+
};
43+
resp
44+
} else {
45+
// Statement is already finalized.
46+
Err(Error::WorkerCrashed)
4147
};
42-
4348
let _ = tx.send(resp);
4449
}
4550
}
@@ -57,7 +62,7 @@ impl StatementWorker {
5762

5863
self.tx
5964
.send(StatementWorkerCommand::Step {
60-
statement: Arc::clone(statement),
65+
statement: Arc::downgrade(statement),
6166
tx,
6267
})
6368
.map_err(|_| Error::WorkerCrashed)?;

0 commit comments

Comments
 (0)