Skip to content

Commit fbda149

Browse files
committed
Runtime(fix): store Waker in Scheduler
1 parent 7ebe828 commit fbda149

4 files changed

Lines changed: 90 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ nix = "0.30.1"
5252
once_cell = "1.18.0"
5353
os_pipe = "1.1.4"
5454
paste = "1.0.14"
55+
pin-project-lite = "0.2.16"
5556
rand = "0.9.0"
5657
rustls = { version = "0.23.1", default-features = false }
5758
rustls-native-certs = "0.8.0"

compio-io/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ compio-buf = { workspace = true, features = ["arrayvec", "bytes"] }
1515
futures-util = { workspace = true, features = ["sink"] }
1616
paste = { workspace = true }
1717
thiserror = { workspace = true, optional = true }
18-
pin-project-lite = { version = "0.2.14", optional = true }
18+
pin-project-lite = { workspace = true, optional = true }
1919
serde = { version = "1.0.219", optional = true }
2020
serde_json = { version = "1.0.140", optional = true }
2121

compio-runtime/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ once_cell = { workspace = true }
4343
scoped-tls = "1.0.1"
4444
slab = { workspace = true }
4545
socket2 = { workspace = true }
46-
pin-project-lite = "0.2.16"
46+
pin-project-lite = { workspace = true }
4747

4848
# Windows specific dependencies
4949
[target.'cfg(windows)'.dependencies]
@@ -67,6 +67,14 @@ time = []
6767
# Enable it to always notify the driver when a task schedules.
6868
notify-always = []
6969

70+
[[test]]
71+
name = "custom_loop"
72+
required-features = ["event", "time"]
73+
7074
[[test]]
7175
name = "event"
7276
required-features = ["event"]
77+
78+
[[test]]
79+
name = "drop"
80+
required-features = ["time"]

compio-runtime/tests/drop.rs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
1-
#[cfg(feature = "time")]
1+
use futures_util::task::AtomicWaker;
2+
use std::{
3+
future::Future,
4+
pin::Pin,
5+
sync::Arc,
6+
task::{Context, Poll},
7+
thread::{self, ThreadId},
8+
};
9+
10+
struct DropWatcher {
11+
waker: Arc<AtomicWaker>,
12+
thread_id: ThreadId,
13+
}
14+
15+
impl DropWatcher {
16+
fn new(waker: Arc<AtomicWaker>) -> Self {
17+
Self {
18+
waker,
19+
thread_id: thread::current().id(),
20+
}
21+
}
22+
}
23+
24+
impl Future for DropWatcher {
25+
type Output = ();
26+
27+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
28+
self.waker.register(cx.waker());
29+
Poll::Pending
30+
}
31+
}
32+
33+
impl Drop for DropWatcher {
34+
fn drop(&mut self) {
35+
if self.thread_id != thread::current().id() {
36+
panic!("DropWatcher dropped on a different thread!!!");
37+
}
38+
}
39+
}
40+
241
#[test]
3-
fn test_drop() {
42+
fn test_drop_with_timer() {
443
compio_runtime::Runtime::new().unwrap().block_on(async {
544
compio_runtime::spawn(async {
645
loop {
@@ -10,3 +49,41 @@ fn test_drop() {
1049
.detach();
1150
})
1251
}
52+
53+
#[test]
54+
fn test_wake_after_runtime_drop() {
55+
let waker = Arc::new(AtomicWaker::new());
56+
let waker_clone = waker.clone();
57+
58+
let rt = compio_runtime::Runtime::new().unwrap();
59+
60+
rt.block_on(async move {
61+
compio_runtime::spawn(DropWatcher::new(waker_clone)).detach();
62+
});
63+
64+
drop(rt);
65+
66+
// Use `unwrap()` to ensure there is a waker stored.
67+
waker.take().unwrap().wake();
68+
}
69+
70+
#[test]
71+
fn test_wake_from_another_thread_after_runtime_drop() {
72+
let waker = Arc::new(AtomicWaker::new());
73+
let waker_clone = waker.clone();
74+
75+
let rt = compio_runtime::Runtime::new().unwrap();
76+
77+
rt.block_on(async move {
78+
compio_runtime::spawn(DropWatcher::new(waker_clone)).detach();
79+
});
80+
81+
drop(rt);
82+
83+
thread::spawn(move || {
84+
// Use `unwrap()` to ensure there is a waker stored.
85+
waker.take().unwrap().wake();
86+
})
87+
.join()
88+
.unwrap();
89+
}

0 commit comments

Comments
 (0)