kobako 0.31.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.githooks/linux-tests.sh +29 -0
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +125 -0
- data/Cargo.lock +457 -494
- data/README.md +2 -3
- data/ROADMAP.md +0 -1
- data/crates/kobako-runtime/CHANGELOG.md +90 -0
- data/crates/kobako-runtime/Cargo.toml +14 -15
- data/crates/kobako-runtime/README.md +1 -1
- data/crates/kobako-runtime/src/cred.rs +32 -20
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
- data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
- data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
- data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
- data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
- data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
- data/crates/kobako-runtime/src/filesystem.rs +2 -0
- data/crates/kobako-runtime/src/fork.rs +239 -16
- data/crates/kobako-runtime/src/limits.rs +17 -41
- data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
- data/crates/kobako-runtime/src/locking.rs +508 -113
- data/crates/kobako-runtime/src/memcg.rs +83 -160
- data/crates/kobako-runtime/src/mount.rs +0 -30
- data/crates/kobako-runtime/src/net/connections.rs +53 -13
- data/crates/kobako-runtime/src/net/egress.rs +44 -24
- data/crates/kobako-runtime/src/net/http.rs +888 -456
- data/crates/kobako-runtime/src/net/lookup.rs +150 -50
- data/crates/kobako-runtime/src/net/namespace.rs +97 -95
- data/crates/kobako-runtime/src/net/reactor.rs +298 -203
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
- data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
- data/crates/kobako-runtime/src/net/resolv.rs +39 -15
- data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
- data/crates/kobako-runtime/src/net/socket.rs +51 -52
- data/crates/kobako-runtime/src/net/task.rs +223 -0
- data/crates/kobako-runtime/src/net/tcp.rs +410 -158
- data/crates/kobako-runtime/src/net/tls.rs +65 -3
- data/crates/kobako-runtime/src/net/transport.rs +72 -47
- data/crates/kobako-runtime/src/net/udp.rs +231 -129
- data/crates/kobako-runtime/src/net.rs +3 -0
- data/crates/kobako-runtime/src/sched.rs +244 -58
- data/crates/kobako-runtime/src/security.rs +27 -16
- data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
- data/crates/kobako-runtime/src/vfs/file.rs +52 -53
- data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
- data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
- data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
- data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
- data/crates/kobako-runtime/src/vfs.rs +1 -0
- data/crates/kobako-runtime/src/wait.rs +75 -50
- data/crates/kobako-transport/CHANGELOG.md +7 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +1 -1
- data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
- data/crates/kobako-wasmtime/Cargo.toml +27 -6
- data/crates/kobako-wasmtime/README.md +3 -3
- data/crates/kobako-wasmtime/src/abi.rs +2 -1
- data/crates/kobako-wasmtime/src/ambient.rs +11 -2
- data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
- data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
- data/crates/kobako-wasmtime/src/cache.rs +88 -103
- data/crates/kobako-wasmtime/src/driver.rs +40 -27
- data/crates/kobako-wasmtime/src/invocation.rs +21 -7
- data/crates/kobako-wasmtime/src/lib.rs +2 -0
- data/crates/kobako-wasmtime/src/limiter.rs +78 -25
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
- data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
- data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
- data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
- data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
- data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
- data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
- data/crates/kobako-wasmtime/src/preview1.rs +85 -9
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
- data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
- data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
- data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
- data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
- data/crates/kobako-wasmtime/src/trap.rs +68 -23
- data/crates/kobako-wasmtime/src/waits.rs +59 -0
- data/crates/kobako-wasmtime/src/wasi.rs +91 -10
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +6 -2
- data/ext/kobako/src/escape.rs +332 -26
- data/ext/kobako/src/filesystem/grants.rs +5 -7
- data/ext/kobako/src/filesystem/ruby.rs +10 -3
- data/ext/kobako/src/filesystem.rs +47 -8
- data/ext/kobako/src/fork.rs +72 -0
- data/ext/kobako/src/gvl.rs +499 -76
- data/ext/kobako/src/handoff.rs +490 -0
- data/ext/kobako/src/lib.rs +10 -0
- data/ext/kobako/src/network.rs +5 -3
- data/ext/kobako/src/pinned_value.rs +28 -15
- data/ext/kobako/src/runtime/bridge.rs +32 -25
- data/ext/kobako/src/runtime.rs +74 -43
- data/lib/kobako/catalog/handles.rb +9 -5
- data/lib/kobako/codec/decoder.rb +1 -1
- data/lib/kobako/codec/state.rb +2 -18
- data/lib/kobako/codec/value_budget.rb +4 -4
- data/lib/kobako/codec.rb +5 -6
- data/lib/kobako/errors.rb +4 -7
- data/lib/kobako/filesystem.rb +1 -1
- data/lib/kobako/fork.rb +42 -0
- data/lib/kobako/handle.rb +1 -2
- data/lib/kobako/sandbox.rb +17 -36
- data/lib/kobako/sandbox_options.rb +7 -9
- data/lib/kobako/usage.rb +4 -2
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +2 -2
- data/sig/kobako/catalog/handles.rbs +2 -0
- data/sig/kobako/codec/state.rbs +0 -4
- data/sig/kobako/codec/value_budget.rbs +1 -1
- data/sig/kobako/codec.rbs +2 -2
- data/sig/kobako/errors.rbs +0 -3
- data/sig/kobako/fork.rbs +17 -0
- data/sig/kobako/runtime.rbs +2 -2
- data/sig/kobako/sandbox.rbs +3 -12
- data/sig/kobako/sandbox_options.rbs +0 -2
- metadata +15 -4
- data/lib/kobako/pool.rb +0 -162
- data/sig/core/thread.rbs +0 -6
- data/sig/kobako/pool.rbs +0 -46
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
//! The one thread that waits on every host socket of the process, as Linux's
|
|
2
2
|
//! softirq hands each arriving segment to the socket it belongs to: an event
|
|
3
3
|
//! calls its socket's `on_event`, which moves what it can without blocking and
|
|
4
|
-
//! wakes the socket's wait queue. The same thread
|
|
5
|
-
//!
|
|
6
|
-
//!
|
|
7
|
-
//! thread, starts a reactor of its own.
|
|
4
|
+
//! wakes the socket's wait queue. The same thread wakes a waker whose timer is
|
|
5
|
+
//! due, so no socket holds a thread of its own. It only wakes: an HTTP
|
|
6
|
+
//! exchange or a name lookup moves on the thread that waits for it. A forked
|
|
7
|
+
//! child, which inherits no thread, starts a reactor of its own.
|
|
8
8
|
|
|
9
|
-
use std::collections::
|
|
10
|
-
use std::future::Future;
|
|
9
|
+
use std::collections::HashMap;
|
|
11
10
|
use std::io;
|
|
12
|
-
use std::pin::Pin;
|
|
13
11
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
14
|
-
use std::sync::{Arc, Mutex, PoisonError, Weak};
|
|
15
|
-
use std::task::
|
|
12
|
+
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
|
|
13
|
+
use std::task::Waker;
|
|
16
14
|
use std::thread;
|
|
17
15
|
use std::time::{Duration, Instant};
|
|
18
16
|
|
|
@@ -20,15 +18,54 @@ use mio::event::Source;
|
|
|
20
18
|
use mio::{Events, Interest, Registry, Token};
|
|
21
19
|
|
|
22
20
|
use crate::fork::{Pid, ProcessLocal};
|
|
21
|
+
use crate::vfs::PollMask;
|
|
22
|
+
|
|
23
|
+
/// The poll mask `event` reports: a hang-up once both directions closed, as
|
|
24
|
+
/// a reset or two FINs leave a socket, which Windows' AFD reports alone.
|
|
25
|
+
fn poll_mask_of(event: &mio::event::Event) -> PollMask {
|
|
26
|
+
let mut mask = PollMask::default();
|
|
27
|
+
if event.is_readable() {
|
|
28
|
+
mask = mask | PollMask::IN;
|
|
29
|
+
}
|
|
30
|
+
if event.is_writable() {
|
|
31
|
+
mask = mask | PollMask::OUT;
|
|
32
|
+
}
|
|
33
|
+
if event.is_error() {
|
|
34
|
+
mask = mask | PollMask::ERR;
|
|
35
|
+
}
|
|
36
|
+
if event.is_read_closed() && event.is_write_closed() {
|
|
37
|
+
mask = mask | PollMask::HUP;
|
|
38
|
+
}
|
|
39
|
+
mask
|
|
40
|
+
}
|
|
23
41
|
|
|
24
42
|
/// What a host socket does once the reactor saw it change, as a socket's
|
|
25
43
|
/// `sk_data_ready` and `sk_write_space` do: move what it can without blocking,
|
|
26
|
-
/// then wake whoever waits on it.
|
|
44
|
+
/// then wake whoever waits on it. `events` is what the change reported, as a
|
|
45
|
+
/// wakeup's key carries the poll mask; a repoll reports nothing. A socket
|
|
46
|
+
/// that stops short of `WouldBlock` hears of the host socket again only
|
|
47
|
+
/// through `events`, a repoll, or its guest's next call.
|
|
27
48
|
pub(crate) trait Ready: Send + Sync {
|
|
28
|
-
fn on_event(&self);
|
|
49
|
+
fn on_event(&self, events: PollMask);
|
|
50
|
+
|
|
51
|
+
/// Fail the socket from outside, as the reactor stopping does, and wake
|
|
52
|
+
/// whoever waits on it, as `tcp_abort` and `udp_abort` end one.
|
|
53
|
+
fn abort(&self);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// What the reactor watches a socket for: what arrives, always, and room to
|
|
57
|
+
/// write only while a writer found none, as Linux wakes a socket's writers
|
|
58
|
+
/// once `SOCK_NOSPACE` marks it. A socket that always has room would
|
|
59
|
+
/// otherwise be reported again each time a host re-arms its watch, as
|
|
60
|
+
/// Windows' AFD does after every read that would block.
|
|
61
|
+
fn interest(nospace: bool) -> Interest {
|
|
62
|
+
match nospace {
|
|
63
|
+
true => Interest::READABLE | Interest::WRITABLE,
|
|
64
|
+
false => Interest::READABLE,
|
|
65
|
+
}
|
|
29
66
|
}
|
|
30
67
|
|
|
31
|
-
/// The token that wakes the reactor to
|
|
68
|
+
/// The token that wakes the reactor to read its timers and repolls again.
|
|
32
69
|
const WAKE_TOKEN: Token = Token(0);
|
|
33
70
|
|
|
34
71
|
pub(crate) struct Reactor {
|
|
@@ -38,14 +75,13 @@ pub(crate) struct Reactor {
|
|
|
38
75
|
/// The socket each token belongs to, held weakly so the reactor keeps no
|
|
39
76
|
/// socket alive past its release.
|
|
40
77
|
sockets: Mutex<HashMap<Token, Weak<dyn Ready>>>,
|
|
41
|
-
/// The tasks woken and waiting their turn, as a scheduler's run queue.
|
|
42
|
-
run_queue: Mutex<VecDeque<Arc<Task>>>,
|
|
43
78
|
/// The sockets whose last turn ended at its bound with more waiting, each
|
|
44
79
|
/// once, run again after the next round of events, as NAPI puts a poll
|
|
45
80
|
/// that spent its weight back at the tail of `poll_list`.
|
|
46
81
|
repoll: Mutex<Vec<Weak<dyn Ready>>>,
|
|
47
|
-
/// Whether the thread stopped, its poll having failed
|
|
48
|
-
/// starts another reactor rather than
|
|
82
|
+
/// Whether the thread stopped, its poll having failed or a callback
|
|
83
|
+
/// panicked, so the next socket starts another reactor rather than
|
|
84
|
+
/// register on one nobody waits on.
|
|
49
85
|
stopped: AtomicBool,
|
|
50
86
|
/// The wakers due at an instant, as hrtimers wake a task whose timeout
|
|
51
87
|
/// ends.
|
|
@@ -58,7 +94,7 @@ static CURRENT: ProcessLocal<Arc<Reactor>> = ProcessLocal::new();
|
|
|
58
94
|
impl Reactor {
|
|
59
95
|
/// The process's reactor, started at the first socket; a forked child
|
|
60
96
|
/// starts its own, since its parent's thread never came along, and so does
|
|
61
|
-
/// the socket after a reactor
|
|
97
|
+
/// the socket after a reactor that stopped.
|
|
62
98
|
pub(crate) fn current() -> io::Result<Arc<Self>> {
|
|
63
99
|
CURRENT.with(|current| {
|
|
64
100
|
if let Some(reactor) = current.as_ref() {
|
|
@@ -74,66 +110,66 @@ impl Reactor {
|
|
|
74
110
|
|
|
75
111
|
fn start() -> io::Result<Arc<Self>> {
|
|
76
112
|
let mut poll = mio::Poll::new()?;
|
|
77
|
-
let reactor =
|
|
113
|
+
let reactor = Self::on(&poll)?;
|
|
114
|
+
let running = reactor.clone();
|
|
115
|
+
thread::Builder::new()
|
|
116
|
+
.name("kobako-reactor".into())
|
|
117
|
+
.spawn(move || running.run(|events, wait| poll.poll(events, wait)))?;
|
|
118
|
+
Ok(reactor)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// A reactor registering on `poll`, whose events nobody reads yet.
|
|
122
|
+
fn on(poll: &mio::Poll) -> io::Result<Arc<Self>> {
|
|
123
|
+
Ok(Arc::new(Self {
|
|
78
124
|
registry: poll.registry().try_clone()?,
|
|
79
125
|
waker: mio::Waker::new(poll.registry(), WAKE_TOKEN)?,
|
|
80
126
|
next: AtomicUsize::new(1),
|
|
81
127
|
sockets: Mutex::default(),
|
|
82
|
-
run_queue: Mutex::default(),
|
|
83
128
|
repoll: Mutex::default(),
|
|
84
129
|
stopped: AtomicBool::new(false),
|
|
85
130
|
timers: Mutex::default(),
|
|
86
|
-
})
|
|
87
|
-
let running = reactor.clone();
|
|
88
|
-
thread::Builder::new()
|
|
89
|
-
.name("kobako-reactor".into())
|
|
90
|
-
.spawn(move || running.run(&mut poll))?;
|
|
91
|
-
Ok(reactor)
|
|
131
|
+
}))
|
|
92
132
|
}
|
|
93
133
|
|
|
94
|
-
|
|
134
|
+
/// Serve each round of the events `poll` waits for, until it fails.
|
|
135
|
+
fn run(&self, mut poll: impl FnMut(&mut Events, Option<Duration>) -> io::Result<()>) {
|
|
136
|
+
let _stopping = Stopping(self);
|
|
95
137
|
let mut events = Events::with_capacity(256);
|
|
96
138
|
loop {
|
|
97
|
-
|
|
98
|
-
// events, as `net_rx_action` takes the polls newly scheduled
|
|
99
|
-
// ahead of its repoll list; what asks during the round waits for
|
|
100
|
-
// the next.
|
|
101
|
-
let again =
|
|
102
|
-
std::mem::take(&mut *self.repoll.lock().unwrap_or_else(PoisonError::into_inner));
|
|
103
|
-
let wait = match again.is_empty() {
|
|
139
|
+
let wait = match self.repoll().is_empty() {
|
|
104
140
|
true => self.poll_timeout(),
|
|
105
141
|
false => Some(Duration::ZERO),
|
|
106
142
|
};
|
|
107
|
-
if let Err(err) = poll
|
|
143
|
+
if let Err(err) = poll(&mut events, wait) {
|
|
108
144
|
if err.kind() == io::ErrorKind::Interrupted {
|
|
109
145
|
continue;
|
|
110
146
|
}
|
|
111
|
-
self.stopped.store(true, Ordering::Release);
|
|
112
147
|
return;
|
|
113
148
|
}
|
|
114
|
-
|
|
115
|
-
|
|
149
|
+
// What asked for another turn before this round runs after its
|
|
150
|
+
// events, as `net_rx_action` takes the polls newly scheduled
|
|
151
|
+
// ahead of its repoll list; what asks during the round waits for
|
|
152
|
+
// the next. A poll a signal interrupted takes none, so each
|
|
153
|
+
// waits on in place, as `poll_list` keeps its entries.
|
|
154
|
+
let again = std::mem::take(&mut *self.repoll());
|
|
155
|
+
let ready: Vec<(Arc<dyn Ready>, PollMask)> = {
|
|
116
156
|
let sockets = self.sockets.lock().unwrap_or_else(PoisonError::into_inner);
|
|
117
157
|
events
|
|
118
158
|
.iter()
|
|
119
159
|
.filter_map(|event| {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return None;
|
|
123
|
-
}
|
|
124
|
-
sockets.get(&event.token()).and_then(Weak::upgrade)
|
|
160
|
+
let socket = sockets.get(&event.token()).and_then(Weak::upgrade)?;
|
|
161
|
+
Some((socket, poll_mask_of(event)))
|
|
125
162
|
})
|
|
126
163
|
.collect()
|
|
127
164
|
};
|
|
128
|
-
ready
|
|
165
|
+
ready
|
|
166
|
+
.iter()
|
|
167
|
+
.for_each(|(socket, events)| socket.on_event(*events));
|
|
129
168
|
again
|
|
130
169
|
.iter()
|
|
131
170
|
.filter_map(Weak::upgrade)
|
|
132
|
-
.for_each(|socket| socket.on_event());
|
|
171
|
+
.for_each(|socket| socket.on_event(PollMask::default()));
|
|
133
172
|
self.wake_due_timers();
|
|
134
|
-
if run_tasks {
|
|
135
|
-
self.run_tasks();
|
|
136
|
-
}
|
|
137
173
|
}
|
|
138
174
|
}
|
|
139
175
|
|
|
@@ -149,6 +185,11 @@ impl Reactor {
|
|
|
149
185
|
.unwrap_or_else(PoisonError::into_inner)
|
|
150
186
|
.push((at, waker.clone()));
|
|
151
187
|
let _ = self.waker.wake();
|
|
188
|
+
// A reactor that stopped never reads its timers again.
|
|
189
|
+
if self.stopped.load(Ordering::Acquire) {
|
|
190
|
+
self.disarm(&waker);
|
|
191
|
+
waker.wake_by_ref();
|
|
192
|
+
}
|
|
152
193
|
Timer {
|
|
153
194
|
reactor: self.clone(),
|
|
154
195
|
waker,
|
|
@@ -164,6 +205,10 @@ impl Reactor {
|
|
|
164
205
|
.retain(|(_, armed)| !armed.will_wake(waker));
|
|
165
206
|
}
|
|
166
207
|
|
|
208
|
+
fn repoll(&self) -> MutexGuard<'_, Vec<Weak<dyn Ready>>> {
|
|
209
|
+
self.repoll.lock().unwrap_or_else(PoisonError::into_inner)
|
|
210
|
+
}
|
|
211
|
+
|
|
167
212
|
/// How long the poll may wait before the soonest timer is due.
|
|
168
213
|
fn poll_timeout(&self) -> Option<Duration> {
|
|
169
214
|
let timers = self.timers.lock().unwrap_or_else(PoisonError::into_inner);
|
|
@@ -184,29 +229,37 @@ impl Reactor {
|
|
|
184
229
|
due.into_iter().for_each(Waker::wake);
|
|
185
230
|
}
|
|
186
231
|
|
|
187
|
-
/// Watch `source` for
|
|
188
|
-
/// `on_event
|
|
232
|
+
/// Watch `source` for what arrives, and for room to write while `nospace`
|
|
233
|
+
/// holds, handing each change to `ready`'s `on_event`; a reactor that
|
|
234
|
+
/// stopped refuses it with `ECONNABORTED`, as nobody would wake its
|
|
235
|
+
/// socket.
|
|
189
236
|
pub(crate) fn register(
|
|
190
237
|
self: &Arc<Self>,
|
|
191
238
|
source: &mut impl Source,
|
|
192
239
|
ready: Weak<dyn Ready>,
|
|
240
|
+
nospace: bool,
|
|
193
241
|
) -> io::Result<Registration> {
|
|
194
242
|
let token = Token(self.next.fetch_add(1, Ordering::Relaxed));
|
|
195
243
|
self.sockets
|
|
196
244
|
.lock()
|
|
197
245
|
.unwrap_or_else(PoisonError::into_inner)
|
|
198
246
|
.insert(token, ready);
|
|
199
|
-
if let Err(err) =
|
|
200
|
-
self.registry
|
|
201
|
-
.register(source, token, Interest::READABLE | Interest::WRITABLE)
|
|
202
|
-
{
|
|
247
|
+
if let Err(err) = self.registry.register(source, token, interest(nospace)) {
|
|
203
248
|
self.forget(token);
|
|
204
249
|
return Err(err);
|
|
205
250
|
}
|
|
251
|
+
// Read after the token is in place, so a reactor stopping meanwhile
|
|
252
|
+
// either aborts the socket or is seen here.
|
|
253
|
+
if self.stopped.load(Ordering::Acquire) {
|
|
254
|
+
let _ = self.registry.deregister(source);
|
|
255
|
+
self.forget(token);
|
|
256
|
+
return Err(io::ErrorKind::ConnectionAborted.into());
|
|
257
|
+
}
|
|
206
258
|
Ok(Registration {
|
|
207
259
|
token,
|
|
208
260
|
reactor: self.clone(),
|
|
209
261
|
pid: Pid::current(),
|
|
262
|
+
nospace,
|
|
210
263
|
})
|
|
211
264
|
}
|
|
212
265
|
|
|
@@ -216,44 +269,33 @@ impl Reactor {
|
|
|
216
269
|
.unwrap_or_else(PoisonError::into_inner)
|
|
217
270
|
.remove(&token);
|
|
218
271
|
}
|
|
272
|
+
}
|
|
219
273
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
274
|
+
/// Stops the reactor as its thread leaves the loop, by a failed poll or a
|
|
275
|
+
/// panic alike: no socket registers on it again, and each socket and timer
|
|
276
|
+
/// it watched is aborted or woken, so no waiter waits on a thread that is
|
|
277
|
+
/// gone.
|
|
278
|
+
struct Stopping<'a>(&'a Reactor);
|
|
279
|
+
|
|
280
|
+
impl Drop for Stopping<'_> {
|
|
281
|
+
fn drop(&mut self) {
|
|
282
|
+
let reactor = self.0;
|
|
283
|
+
reactor.stopped.store(true, Ordering::Release);
|
|
284
|
+
let watched: Vec<Arc<dyn Ready>> = reactor
|
|
285
|
+
.sockets
|
|
286
|
+
.lock()
|
|
287
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
288
|
+
.values()
|
|
289
|
+
.filter_map(Weak::upgrade)
|
|
290
|
+
.collect();
|
|
291
|
+
watched.iter().for_each(|socket| socket.abort());
|
|
292
|
+
let timers = std::mem::take(
|
|
293
|
+
&mut *reactor
|
|
294
|
+
.timers
|
|
238
295
|
.lock()
|
|
239
|
-
.unwrap_or_else(PoisonError::into_inner)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return;
|
|
243
|
-
};
|
|
244
|
-
task.queued.store(false, Ordering::Release);
|
|
245
|
-
let waker = Waker::from(task.clone());
|
|
246
|
-
let mut future = task.future.lock().unwrap_or_else(PoisonError::into_inner);
|
|
247
|
-
let ended = future.as_mut().is_some_and(|running| {
|
|
248
|
-
running
|
|
249
|
-
.as_mut()
|
|
250
|
-
.poll(&mut Context::from_waker(&waker))
|
|
251
|
-
.is_ready()
|
|
252
|
-
});
|
|
253
|
-
if ended {
|
|
254
|
-
*future = None;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
296
|
+
.unwrap_or_else(PoisonError::into_inner),
|
|
297
|
+
);
|
|
298
|
+
timers.into_iter().for_each(|(_, waker)| waker.wake());
|
|
257
299
|
}
|
|
258
300
|
}
|
|
259
301
|
|
|
@@ -263,9 +305,27 @@ pub(crate) struct Registration {
|
|
|
263
305
|
token: Token,
|
|
264
306
|
reactor: Arc<Reactor>,
|
|
265
307
|
pid: Pid,
|
|
308
|
+
nospace: bool,
|
|
266
309
|
}
|
|
267
310
|
|
|
268
311
|
impl Registration {
|
|
312
|
+
/// Watch `source` for room to write while `nospace` holds, as a writer
|
|
313
|
+
/// that found none sets `SOCK_NOSPACE` and one that wrote clears it. A
|
|
314
|
+
/// forked child leaves its parent's watch as it was, as `deregister` does.
|
|
315
|
+
pub(crate) fn set_nospace(&mut self, source: &mut impl Source, nospace: bool) {
|
|
316
|
+
if self.nospace == nospace || !self.pid.is_current() {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if self
|
|
320
|
+
.reactor
|
|
321
|
+
.registry
|
|
322
|
+
.reregister(source, self.token, interest(nospace))
|
|
323
|
+
.is_ok()
|
|
324
|
+
{
|
|
325
|
+
self.nospace = nospace;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
269
329
|
/// Stop watching `source`, which the socket closes next; the token goes
|
|
270
330
|
/// once the registration drops. A forked child leaves its parent's
|
|
271
331
|
/// registration be: the epoll instance is shared, and removing the one
|
|
@@ -281,11 +341,7 @@ impl Registration {
|
|
|
281
341
|
/// spent its weight with more to take and `NAPI_STATE_SCHED` keeps it
|
|
282
342
|
/// queued once; no further event may come for what already waits.
|
|
283
343
|
pub(crate) fn repoll(&self, ready: Weak<dyn Ready>) {
|
|
284
|
-
let mut repoll = self
|
|
285
|
-
.reactor
|
|
286
|
-
.repoll
|
|
287
|
-
.lock()
|
|
288
|
-
.unwrap_or_else(PoisonError::into_inner);
|
|
344
|
+
let mut repoll = self.reactor.repoll();
|
|
289
345
|
if !repoll.iter().any(|queued| queued.ptr_eq(&ready)) {
|
|
290
346
|
repoll.push(ready);
|
|
291
347
|
}
|
|
@@ -314,48 +370,13 @@ impl Drop for Timer {
|
|
|
314
370
|
}
|
|
315
371
|
}
|
|
316
372
|
|
|
317
|
-
/// A future the reactor runs, as an executor's task.
|
|
318
|
-
pub(crate) struct Task {
|
|
319
|
-
future: Mutex<Option<Pin<Box<dyn Future<Output = ()> + Send>>>>,
|
|
320
|
-
reactor: Weak<Reactor>,
|
|
321
|
-
queued: AtomicBool,
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
impl Task {
|
|
325
|
-
/// Drop the future where it stands, with every socket it holds.
|
|
326
|
-
pub(crate) fn cancel(&self) {
|
|
327
|
-
let future = self
|
|
328
|
-
.future
|
|
329
|
-
.lock()
|
|
330
|
-
.unwrap_or_else(PoisonError::into_inner)
|
|
331
|
-
.take();
|
|
332
|
-
drop(future);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
impl Wake for Task {
|
|
337
|
-
fn wake(self: Arc<Self>) {
|
|
338
|
-
let Some(reactor) = self.reactor.upgrade() else {
|
|
339
|
-
return;
|
|
340
|
-
};
|
|
341
|
-
if self.queued.swap(true, Ordering::AcqRel) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
reactor
|
|
345
|
-
.run_queue
|
|
346
|
-
.lock()
|
|
347
|
-
.unwrap_or_else(PoisonError::into_inner)
|
|
348
|
-
.push_back(self);
|
|
349
|
-
let _ = reactor.waker.wake();
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
373
|
#[cfg(test)]
|
|
354
374
|
mod tests {
|
|
355
375
|
use std::sync::mpsc;
|
|
356
|
-
use std::task::Poll;
|
|
376
|
+
use std::task::{Poll, Wake};
|
|
357
377
|
use std::time::Duration;
|
|
358
378
|
|
|
379
|
+
use super::super::task::Task;
|
|
359
380
|
use super::*;
|
|
360
381
|
|
|
361
382
|
// A reactor started in another process, as a forked child inherits its
|
|
@@ -376,9 +397,128 @@ mod tests {
|
|
|
376
397
|
struct Tells(Mutex<mpsc::Sender<()>>);
|
|
377
398
|
|
|
378
399
|
impl Ready for Tells {
|
|
379
|
-
fn on_event(&self) {
|
|
400
|
+
fn on_event(&self, _events: PollMask) {
|
|
380
401
|
let _ = self.0.lock().unwrap().send(());
|
|
381
402
|
}
|
|
403
|
+
|
|
404
|
+
fn abort(&self) {}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/// A socket that tells only of its abort.
|
|
408
|
+
struct TellsAbort(Mutex<mpsc::Sender<&'static str>>);
|
|
409
|
+
|
|
410
|
+
impl Ready for TellsAbort {
|
|
411
|
+
fn on_event(&self, _events: PollMask) {}
|
|
412
|
+
|
|
413
|
+
fn abort(&self) {
|
|
414
|
+
let _ = self.0.lock().unwrap().send("the socket");
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/// A socket whose callback fails once armed, as a panic in the code it
|
|
419
|
+
/// runs would. Unarmed, it lets pass the event its registration brings,
|
|
420
|
+
/// as a socket is writable the moment it registers.
|
|
421
|
+
struct PanicsOnceArmed(AtomicBool);
|
|
422
|
+
|
|
423
|
+
impl Ready for PanicsOnceArmed {
|
|
424
|
+
fn on_event(&self, _events: PollMask) {
|
|
425
|
+
if self.0.load(Ordering::Acquire) {
|
|
426
|
+
panic!("a socket's callback failed on the reactor");
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
fn abort(&self) {}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/// A timer's task that tells as it wakes.
|
|
434
|
+
struct TellsWake(Mutex<mpsc::Sender<&'static str>>);
|
|
435
|
+
|
|
436
|
+
impl Wake for TellsWake {
|
|
437
|
+
fn wake(self: Arc<Self>) {
|
|
438
|
+
let _ = self.0.lock().unwrap().send("the timer");
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// A reactor whose thread stops, here as a socket's callback panics,
|
|
443
|
+
// aborts each socket still registered and wakes each timer at once,
|
|
444
|
+
// rather than leave their waiters to their deadlines, and is handed out
|
|
445
|
+
// no more. A poll that fails leaves the loop through the same guard.
|
|
446
|
+
// @behavior NW-235
|
|
447
|
+
#[test]
|
|
448
|
+
fn a_reactor_that_stops_aborts_every_socket_and_timer_it_watches() {
|
|
449
|
+
let reactor = Reactor::start().expect("a reactor starts");
|
|
450
|
+
let (tx, rx) = mpsc::channel();
|
|
451
|
+
let waiting: Arc<dyn Ready> = Arc::new(TellsAbort(Mutex::new(tx.clone())));
|
|
452
|
+
let panics = Arc::new(PanicsOnceArmed(AtomicBool::new(false)));
|
|
453
|
+
let failing: Arc<dyn Ready> = panics.clone();
|
|
454
|
+
let addr = "127.0.0.1:0".parse().unwrap();
|
|
455
|
+
let mut watched = mio::net::UdpSocket::bind(addr).expect("a UDP socket binds");
|
|
456
|
+
let mut failed = mio::net::UdpSocket::bind(addr).expect("a UDP socket binds");
|
|
457
|
+
let _watching = reactor
|
|
458
|
+
.register(&mut watched, Arc::downgrade(&waiting), false)
|
|
459
|
+
.expect("the socket registers");
|
|
460
|
+
let _failing = reactor
|
|
461
|
+
.register(&mut failed, Arc::downgrade(&failing), false)
|
|
462
|
+
.expect("the socket registers");
|
|
463
|
+
let _timer = reactor.wake_at(
|
|
464
|
+
Instant::now() + Duration::from_secs(30),
|
|
465
|
+
Waker::from(Arc::new(TellsWake(Mutex::new(tx)))),
|
|
466
|
+
);
|
|
467
|
+
panics.0.store(true, Ordering::Release);
|
|
468
|
+
std::net::UdpSocket::bind(addr)
|
|
469
|
+
.expect("a UDP socket binds")
|
|
470
|
+
.send_to(b"x", failed.local_addr().unwrap())
|
|
471
|
+
.expect("the datagram goes");
|
|
472
|
+
let mut told: Vec<_> = (0..2)
|
|
473
|
+
.filter_map(|_| rx.recv_timeout(Duration::from_secs(5)).ok())
|
|
474
|
+
.collect();
|
|
475
|
+
told.sort_unstable();
|
|
476
|
+
assert_eq!(
|
|
477
|
+
(told, reactor.stopped.load(Ordering::Acquire)),
|
|
478
|
+
(vec!["the socket", "the timer"], true),
|
|
479
|
+
"a reactor whose thread stops must abort each socket registered on it, wake each \
|
|
480
|
+
timer at once, and mark itself stopped so no later socket registers on it"
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/// A socket that counts each turn it is given.
|
|
485
|
+
#[derive(Default)]
|
|
486
|
+
struct CountsTurns(AtomicUsize);
|
|
487
|
+
|
|
488
|
+
impl Ready for CountsTurns {
|
|
489
|
+
fn on_event(&self, _events: PollMask) {
|
|
490
|
+
self.0.fetch_add(1, Ordering::SeqCst);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
fn abort(&self) {}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// A socket that spent its turn's bound waits for its next turn, and a
|
|
497
|
+
// signal interrupting the reactor's wait in between must not drop it:
|
|
498
|
+
// with no further event to come, it would wait until its deadline.
|
|
499
|
+
// @behavior NW-174
|
|
500
|
+
#[test]
|
|
501
|
+
fn a_repoll_runs_once_past_an_interrupted_wait() {
|
|
502
|
+
let poll = mio::Poll::new().expect("a poll opens");
|
|
503
|
+
let reactor = Reactor::on(&poll).expect("a reactor is made");
|
|
504
|
+
let counts = Arc::new(CountsTurns::default());
|
|
505
|
+
let ready: Arc<dyn Ready> = counts.clone();
|
|
506
|
+
reactor.repoll().push(Arc::downgrade(&ready));
|
|
507
|
+
// Interrupted, then a round with no event, then a failure that stops
|
|
508
|
+
// the thread.
|
|
509
|
+
let mut waits = [
|
|
510
|
+
Err(io::ErrorKind::Interrupted.into()),
|
|
511
|
+
Ok(()),
|
|
512
|
+
Err(io::ErrorKind::Other.into()),
|
|
513
|
+
]
|
|
514
|
+
.into_iter();
|
|
515
|
+
reactor.run(|_, _| waits.next().expect("the reactor stopped at the failure"));
|
|
516
|
+
assert_eq!(
|
|
517
|
+
counts.0.load(Ordering::SeqCst),
|
|
518
|
+
1,
|
|
519
|
+
"a socket asking for another turn must get it once in the round after a reactor \
|
|
520
|
+
wait a signal interrupted"
|
|
521
|
+
);
|
|
382
522
|
}
|
|
383
523
|
|
|
384
524
|
// A forked child shares its parent's epoll instance, where an inherited
|
|
@@ -393,7 +533,7 @@ mod tests {
|
|
|
393
533
|
let addr = "127.0.0.1:0".parse().unwrap();
|
|
394
534
|
let mut socket = mio::net::UdpSocket::bind(addr).expect("a UDP socket binds");
|
|
395
535
|
let mut registration = reactor
|
|
396
|
-
.register(&mut socket, Arc::downgrade(&ready))
|
|
536
|
+
.register(&mut socket, Arc::downgrade(&ready), false)
|
|
397
537
|
.expect("the socket registers");
|
|
398
538
|
while rx.recv_timeout(Duration::from_millis(50)).is_ok() {}
|
|
399
539
|
registration.pid = Pid::parent();
|
|
@@ -409,62 +549,19 @@ mod tests {
|
|
|
409
549
|
);
|
|
410
550
|
}
|
|
411
551
|
|
|
412
|
-
// A
|
|
552
|
+
// A waker asked to be woken at an instant is woken once it passes, with
|
|
553
|
+
// nothing else waking it.
|
|
413
554
|
#[test]
|
|
414
|
-
fn
|
|
555
|
+
fn a_timer_wakes_its_waker_once_due() {
|
|
415
556
|
let reactor = Reactor::current().expect("a reactor starts");
|
|
416
557
|
let (tx, rx) = mpsc::channel();
|
|
417
|
-
let
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
std::future::poll_fn(|cx| {
|
|
421
|
-
let mut gate = waiting.lock().unwrap();
|
|
422
|
-
if gate.0 {
|
|
423
|
-
Poll::Ready(())
|
|
424
|
-
} else {
|
|
425
|
-
gate.1 = Some(cx.waker().clone());
|
|
426
|
-
Poll::Pending
|
|
427
|
-
}
|
|
428
|
-
})
|
|
429
|
-
.await;
|
|
430
|
-
tx.send(()).unwrap();
|
|
431
|
-
});
|
|
432
|
-
thread::sleep(Duration::from_millis(20));
|
|
433
|
-
let waker = {
|
|
434
|
-
let mut gate = gate.lock().unwrap();
|
|
435
|
-
gate.0 = true;
|
|
436
|
-
gate.1.take()
|
|
437
|
-
};
|
|
438
|
-
waker.expect("the task waited").wake();
|
|
439
|
-
assert!(
|
|
440
|
-
rx.recv_timeout(Duration::from_secs(5)).is_ok(),
|
|
441
|
-
"a task woken from another thread must run on the reactor to its end"
|
|
558
|
+
let _timer = reactor.wake_at(
|
|
559
|
+
Instant::now() + Duration::from_millis(50),
|
|
560
|
+
Waker::from(Arc::new(TellsWake(Mutex::new(tx)))),
|
|
442
561
|
);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// A task that asks to be woken at an instant runs again once it passes,
|
|
446
|
-
// with nothing else waking it.
|
|
447
|
-
#[test]
|
|
448
|
-
fn a_timer_wakes_its_task_once_due() {
|
|
449
|
-
let reactor = Reactor::current().expect("a reactor starts");
|
|
450
|
-
let (tx, rx) = mpsc::channel();
|
|
451
|
-
let due = Instant::now() + Duration::from_millis(50);
|
|
452
|
-
let timing = reactor.clone();
|
|
453
|
-
reactor.spawn(async move {
|
|
454
|
-
let mut timer = None;
|
|
455
|
-
std::future::poll_fn(|cx| {
|
|
456
|
-
if Instant::now() >= due {
|
|
457
|
-
return Poll::Ready(());
|
|
458
|
-
}
|
|
459
|
-
timer.get_or_insert_with(|| timing.wake_at(due, cx.waker().clone()));
|
|
460
|
-
Poll::Pending
|
|
461
|
-
})
|
|
462
|
-
.await;
|
|
463
|
-
tx.send(()).unwrap();
|
|
464
|
-
});
|
|
465
562
|
assert!(
|
|
466
563
|
rx.recv_timeout(Duration::from_secs(5)).is_ok(),
|
|
467
|
-
"a
|
|
564
|
+
"a waker the reactor was asked to wake at an instant must be woken once it passes"
|
|
468
565
|
);
|
|
469
566
|
}
|
|
470
567
|
|
|
@@ -499,31 +596,29 @@ mod tests {
|
|
|
499
596
|
.count()
|
|
500
597
|
}
|
|
501
598
|
|
|
502
|
-
// A
|
|
503
|
-
// guest dropping lookups never fills the reactor's timers.
|
|
599
|
+
// A request cancelled while its timer is armed leaves no timer behind, so
|
|
600
|
+
// a guest dropping lookups never fills the reactor's timers.
|
|
504
601
|
#[test]
|
|
505
|
-
fn
|
|
506
|
-
let reactor = Reactor::
|
|
507
|
-
let (armed_tx, armed_rx) = mpsc::channel();
|
|
602
|
+
fn a_cancelled_request_leaves_no_timer() {
|
|
603
|
+
let reactor = Reactor::start().expect("a reactor starts");
|
|
508
604
|
let timing = reactor.clone();
|
|
509
605
|
let later = Instant::now() + Duration::from_secs(30);
|
|
510
|
-
let task =
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
let
|
|
523
|
-
let held = timers_of(&reactor, &waker);
|
|
606
|
+
let task = Task::new(
|
|
607
|
+
async move {
|
|
608
|
+
let mut timer = None;
|
|
609
|
+
std::future::poll_fn(|cx| {
|
|
610
|
+
timer.get_or_insert_with(|| timing.wake_at(later, cx.waker().clone()));
|
|
611
|
+
Poll::<()>::Pending
|
|
612
|
+
})
|
|
613
|
+
.await;
|
|
614
|
+
},
|
|
615
|
+
Arc::default(),
|
|
616
|
+
);
|
|
617
|
+
task.poll();
|
|
618
|
+
let held = reactor.timers.lock().unwrap().len();
|
|
524
619
|
task.cancel();
|
|
525
620
|
assert_eq!(
|
|
526
|
-
(held,
|
|
621
|
+
(held, reactor.timers.lock().unwrap().len()),
|
|
527
622
|
(1, 0),
|
|
528
623
|
"a cancelled lookup's timer must leave the reactor with the lookup, not stay until due"
|
|
529
624
|
);
|