-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`.
- Loading branch information
1 parent
7fdcf2d
commit 327b1ad
Showing
3 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![cfg(feature = "diesel_sqlite_pool")] | ||
|
||
use rocket::figment::Figment; | ||
use rocket_sync_db_pools::database; | ||
|
||
#[database("example")] | ||
struct ExampleDb(diesel::SqliteConnection); | ||
|
||
#[test] | ||
fn can_drop_connection_in_sync_context() { | ||
let conn = rocket::execute(async { | ||
let figment = Figment::from(rocket::Config::debug_default()) | ||
.merge(("databases.example.url", ":memory:")); | ||
|
||
let rocket = rocket::custom(figment) | ||
.attach(ExampleDb::fairing()) | ||
.ignite().await | ||
.expect("rocket"); | ||
|
||
ExampleDb::get_one(&rocket).await | ||
.expect("attach => connection") | ||
}); | ||
|
||
drop(conn); | ||
} |