Skip to content

Commit 327b1ad

Browse files
committed
Allow sync drops for 'sync_db_pools' connections.
Prior to this commit, connections from 'sync_db_pools' assumed that they were being dropped in an async context. This is overwhelmingly the common case as connections are typically dropped immediately after request processing. Nothing requires this, however, so holding a connection beyond the scope of the async context was possible (i.e. by storing a connection in managed state). Given the connection's `Drop` impl calls `spawn_blocking`, this resulted in a panic on drop. This commit resolves the issue by modifying `Drop` so that it calls `spawn_blocking` only when it is executing inside an async context. If not, the connection is dropped normally, without `spawn_blocking`.
1 parent 7fdcf2d commit 327b1ad

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

contrib/sync_db_pools/lib/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ default-features = false
4848
[build-dependencies]
4949
version_check = "0.9.1"
5050

51+
[dev-dependencies.rocket]
52+
version = "0.6.0-dev"
53+
path = "../../../core/lib"
54+
default-features = false
55+
features = ["trace"]
56+
5157
[package.metadata.docs.rs]
5258
all-features = true

contrib/sync_db_pools/lib/src/connection.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -182,31 +182,42 @@ impl<K, C: Poolable> Drop for Connection<K, C> {
182182
let connection = self.connection.clone();
183183
let permit = self.permit.take();
184184

185-
// See same motivation above for this arrangement of spawn_blocking/block_on
186-
tokio::task::spawn_blocking(move || {
187-
let mut connection = tokio::runtime::Handle::current().block_on(async {
188-
connection.lock_owned().await
189-
});
185+
// Only use spawn_blocking if the Tokio runtime is still available
186+
if let Ok(handle) = tokio::runtime::Handle::try_current() {
187+
// See above for motivation of this arrangement of spawn_blocking/block_on
188+
handle.spawn_blocking(move || {
189+
let mut connection = tokio::runtime::Handle::current()
190+
.block_on(async { connection.lock_owned().await });
190191

191-
if let Some(conn) = connection.take() {
192+
if let Some(conn) = connection.take() {
193+
drop(conn);
194+
}
195+
});
196+
} else {
197+
warn!(type_name = std::any::type_name::<K>(),
198+
"database connection is being dropped outside of an async context\n\
199+
this means you have stored a connection beyond a request's lifetime\n\
200+
this is not recommended: connections are not valid indefinitely\n\
201+
instead, store a connection pool and get connections as needed");
202+
203+
if let Some(conn) = connection.blocking_lock().take() {
192204
drop(conn);
193205
}
206+
}
194207

195-
// Explicitly dropping the permit here so that it's only
196-
// released after the connection is.
197-
drop(permit);
198-
});
208+
// Explicitly drop permit here to release only after dropping connection.
209+
drop(permit);
199210
}
200211
}
201212

202213
impl<K, C: Poolable> Drop for ConnectionPool<K, C> {
203214
fn drop(&mut self) {
215+
// Use spawn_blocking if the Tokio runtime is still available. Otherwise
216+
// the pool will be dropped on the current thread.
204217
let pool = self.pool.take();
205-
// Only use spawn_blocking if the Tokio runtime is still available
206218
if let Ok(handle) = tokio::runtime::Handle::try_current() {
207219
handle.spawn_blocking(move || drop(pool));
208220
}
209-
// Otherwise the pool will be dropped on the current thread
210221
}
211222
}
212223

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![cfg(feature = "diesel_sqlite_pool")]
2+
3+
use rocket::figment::Figment;
4+
use rocket_sync_db_pools::database;
5+
6+
#[database("example")]
7+
struct ExampleDb(diesel::SqliteConnection);
8+
9+
#[test]
10+
fn can_drop_connection_in_sync_context() {
11+
let conn = rocket::execute(async {
12+
let figment = Figment::from(rocket::Config::debug_default())
13+
.merge(("databases.example.url", ":memory:"));
14+
15+
let rocket = rocket::custom(figment)
16+
.attach(ExampleDb::fairing())
17+
.ignite().await
18+
.expect("rocket");
19+
20+
ExampleDb::get_one(&rocket).await
21+
.expect("attach => connection")
22+
});
23+
24+
drop(conn);
25+
}

0 commit comments

Comments
 (0)