-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: panic if
EnterGuard
dropped incorrect order (#5772)
Calling `Handle::enter()` returns a `EnterGuard` value, which resets the thread-local context on drop. The drop implementation assumes that guards from nested `enter()` calls are dropped in reverse order. However, there is no static enforcement of this requirement. This patch checks that the guards are dropped in reverse order and panics otherwise. A future PR will deprecate `Handle::enter()` in favor of a method that takes a closure, ensuring the guard is dropped appropriately.
- Loading branch information
1 parent
038c4d9
commit cbb3c15
Showing
4 changed files
with
162 additions
and
21 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
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,67 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(feature = "full")] | ||
|
||
use tokio::runtime::Runtime; | ||
|
||
#[test] | ||
fn basic_enter() { | ||
let rt1 = rt(); | ||
let rt2 = rt(); | ||
|
||
let enter1 = rt1.enter(); | ||
let enter2 = rt2.enter(); | ||
|
||
drop(enter2); | ||
drop(enter1); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn interleave_enter_different_rt() { | ||
let rt1 = rt(); | ||
let rt2 = rt(); | ||
|
||
let enter1 = rt1.enter(); | ||
let enter2 = rt2.enter(); | ||
|
||
drop(enter1); | ||
drop(enter2); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn interleave_enter_same_rt() { | ||
let rt1 = rt(); | ||
|
||
let _enter1 = rt1.enter(); | ||
let enter2 = rt1.enter(); | ||
let enter3 = rt1.enter(); | ||
|
||
drop(enter2); | ||
drop(enter3); | ||
} | ||
|
||
#[test] | ||
#[cfg(not(tokio_wasi))] | ||
fn interleave_then_enter() { | ||
let _ = std::panic::catch_unwind(|| { | ||
let rt1 = rt(); | ||
let rt2 = rt(); | ||
|
||
let enter1 = rt1.enter(); | ||
let enter2 = rt2.enter(); | ||
|
||
drop(enter1); | ||
drop(enter2); | ||
}); | ||
|
||
// Can still enter | ||
let rt3 = rt(); | ||
let _enter = rt3.enter(); | ||
} | ||
|
||
fn rt() -> Runtime { | ||
tokio::runtime::Builder::new_current_thread() | ||
.build() | ||
.unwrap() | ||
} |