kobako 0.30.0 → 0.31.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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +335 -0
- data/Cargo.lock +469 -11
- data/README.md +43 -3
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +219 -0
- data/crates/kobako-runtime/Cargo.toml +51 -3
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
- data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +6 -3
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking.rs +10 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +67 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +130 -29
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +101 -196
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
- data/crates/kobako-runtime/src/vfs/file.rs +199 -203
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +9 -6
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
- data/crates/kobako-wasmtime/Cargo.toml +26 -7
- data/crates/kobako-wasmtime/README.md +3 -2
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +17 -37
- data/crates/kobako-wasmtime/src/ambient.rs +33 -18
- data/crates/kobako-wasmtime/src/cache.rs +23 -15
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +48 -63
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +74 -12
- data/crates/kobako-wasmtime/src/lib.rs +5 -3
- data/crates/kobako-wasmtime/src/limiter.rs +70 -5
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +46 -42
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +24 -9
- data/crates/kobako-wasmtime/src/wasi.rs +210 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +30 -29
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +221 -143
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +5 -27
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +4 -4
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/value_budget.rb +132 -40
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +9 -2
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/pool.rb +45 -23
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +92 -35
- data/lib/kobako/sandbox_options.rb +41 -106
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +6 -2
- data/release-please-config.json +15 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/value_budget.rbs +47 -4
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/pool.rbs +7 -3
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +19 -2
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +84 -35
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
//! The one thread that waits on every host socket of the process, as Linux's
|
|
2
|
+
//! softirq hands each arriving segment to the socket it belongs to: an event
|
|
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 runs the tasks an HTTP
|
|
5
|
+
//! exchange and a name lookup need, and wakes a task whose timer is due, so
|
|
6
|
+
//! no socket holds a thread of its own. A forked child, which inherits no
|
|
7
|
+
//! thread, starts a reactor of its own.
|
|
8
|
+
|
|
9
|
+
use std::collections::{HashMap, VecDeque};
|
|
10
|
+
use std::future::Future;
|
|
11
|
+
use std::io;
|
|
12
|
+
use std::pin::Pin;
|
|
13
|
+
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
14
|
+
use std::sync::{Arc, Mutex, PoisonError, Weak};
|
|
15
|
+
use std::task::{Context, Wake, Waker};
|
|
16
|
+
use std::thread;
|
|
17
|
+
use std::time::{Duration, Instant};
|
|
18
|
+
|
|
19
|
+
use mio::event::Source;
|
|
20
|
+
use mio::{Events, Interest, Registry, Token};
|
|
21
|
+
|
|
22
|
+
use crate::fork::{Pid, ProcessLocal};
|
|
23
|
+
|
|
24
|
+
/// What a host socket does once the reactor saw it change, as a socket's
|
|
25
|
+
/// `sk_data_ready` and `sk_write_space` do: move what it can without blocking,
|
|
26
|
+
/// then wake whoever waits on it.
|
|
27
|
+
pub(crate) trait Ready: Send + Sync {
|
|
28
|
+
fn on_event(&self);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// The token that wakes the reactor to run the tasks queued for it.
|
|
32
|
+
const WAKE_TOKEN: Token = Token(0);
|
|
33
|
+
|
|
34
|
+
pub(crate) struct Reactor {
|
|
35
|
+
registry: Registry,
|
|
36
|
+
waker: mio::Waker,
|
|
37
|
+
next: AtomicUsize,
|
|
38
|
+
/// The socket each token belongs to, held weakly so the reactor keeps no
|
|
39
|
+
/// socket alive past its release.
|
|
40
|
+
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
|
+
/// The sockets whose last turn ended at its bound with more waiting, each
|
|
44
|
+
/// once, run again after the next round of events, as NAPI puts a poll
|
|
45
|
+
/// that spent its weight back at the tail of `poll_list`.
|
|
46
|
+
repoll: Mutex<Vec<Weak<dyn Ready>>>,
|
|
47
|
+
/// Whether the thread stopped, its poll having failed, so the next socket
|
|
48
|
+
/// starts another reactor rather than register on one nobody waits on.
|
|
49
|
+
stopped: AtomicBool,
|
|
50
|
+
/// The wakers due at an instant, as hrtimers wake a task whose timeout
|
|
51
|
+
/// ends.
|
|
52
|
+
timers: Mutex<Vec<(Instant, Waker)>>,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// The process's reactor.
|
|
56
|
+
static CURRENT: ProcessLocal<Arc<Reactor>> = ProcessLocal::new();
|
|
57
|
+
|
|
58
|
+
impl Reactor {
|
|
59
|
+
/// The process's reactor, started at the first socket; a forked child
|
|
60
|
+
/// starts its own, since its parent's thread never came along, and so does
|
|
61
|
+
/// the socket after a reactor whose poll failed.
|
|
62
|
+
pub(crate) fn current() -> io::Result<Arc<Self>> {
|
|
63
|
+
CURRENT.with(|current| {
|
|
64
|
+
if let Some(reactor) = current.as_ref() {
|
|
65
|
+
if !reactor.stopped.load(Ordering::Acquire) {
|
|
66
|
+
return Ok(reactor.clone());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
let reactor = Self::start()?;
|
|
70
|
+
*current = Some(reactor.clone());
|
|
71
|
+
Ok(reactor)
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn start() -> io::Result<Arc<Self>> {
|
|
76
|
+
let mut poll = mio::Poll::new()?;
|
|
77
|
+
let reactor = Arc::new(Self {
|
|
78
|
+
registry: poll.registry().try_clone()?,
|
|
79
|
+
waker: mio::Waker::new(poll.registry(), WAKE_TOKEN)?,
|
|
80
|
+
next: AtomicUsize::new(1),
|
|
81
|
+
sockets: Mutex::default(),
|
|
82
|
+
run_queue: Mutex::default(),
|
|
83
|
+
repoll: Mutex::default(),
|
|
84
|
+
stopped: AtomicBool::new(false),
|
|
85
|
+
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)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn run(&self, poll: &mut mio::Poll) {
|
|
95
|
+
let mut events = Events::with_capacity(256);
|
|
96
|
+
loop {
|
|
97
|
+
// What asked for another turn before this round runs after its
|
|
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() {
|
|
104
|
+
true => self.poll_timeout(),
|
|
105
|
+
false => Some(Duration::ZERO),
|
|
106
|
+
};
|
|
107
|
+
if let Err(err) = poll.poll(&mut events, wait) {
|
|
108
|
+
if err.kind() == io::ErrorKind::Interrupted {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
self.stopped.store(true, Ordering::Release);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
let mut run_tasks = false;
|
|
115
|
+
let ready: Vec<Arc<dyn Ready>> = {
|
|
116
|
+
let sockets = self.sockets.lock().unwrap_or_else(PoisonError::into_inner);
|
|
117
|
+
events
|
|
118
|
+
.iter()
|
|
119
|
+
.filter_map(|event| {
|
|
120
|
+
if event.token() == WAKE_TOKEN {
|
|
121
|
+
run_tasks = true;
|
|
122
|
+
return None;
|
|
123
|
+
}
|
|
124
|
+
sockets.get(&event.token()).and_then(Weak::upgrade)
|
|
125
|
+
})
|
|
126
|
+
.collect()
|
|
127
|
+
};
|
|
128
|
+
ready.iter().for_each(|socket| socket.on_event());
|
|
129
|
+
again
|
|
130
|
+
.iter()
|
|
131
|
+
.filter_map(Weak::upgrade)
|
|
132
|
+
.for_each(|socket| socket.on_event());
|
|
133
|
+
self.wake_due_timers();
|
|
134
|
+
if run_tasks {
|
|
135
|
+
self.run_tasks();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// Wake `waker` once `at` passes, in place of any instant its task asked
|
|
141
|
+
/// for before, as `hrtimer_start` moves a task's one timer. The timer
|
|
142
|
+
/// lasts until it is due or its `Timer` drops, so a wait that ends or is
|
|
143
|
+
/// cancelled leaves none behind.
|
|
144
|
+
#[cfg(any(test, all(unix, not(target_os = "macos"))))]
|
|
145
|
+
pub(crate) fn wake_at(self: &Arc<Self>, at: Instant, waker: Waker) -> Timer {
|
|
146
|
+
self.disarm(&waker);
|
|
147
|
+
self.timers
|
|
148
|
+
.lock()
|
|
149
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
150
|
+
.push((at, waker.clone()));
|
|
151
|
+
let _ = self.waker.wake();
|
|
152
|
+
Timer {
|
|
153
|
+
reactor: self.clone(),
|
|
154
|
+
waker,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// Drop the timer `waker`'s task holds, as `hrtimer_cancel` does.
|
|
159
|
+
#[cfg(any(test, all(unix, not(target_os = "macos"))))]
|
|
160
|
+
fn disarm(&self, waker: &Waker) {
|
|
161
|
+
self.timers
|
|
162
|
+
.lock()
|
|
163
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
164
|
+
.retain(|(_, armed)| !armed.will_wake(waker));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// How long the poll may wait before the soonest timer is due.
|
|
168
|
+
fn poll_timeout(&self) -> Option<Duration> {
|
|
169
|
+
let timers = self.timers.lock().unwrap_or_else(PoisonError::into_inner);
|
|
170
|
+
let soonest = timers.iter().map(|(at, _)| *at).min()?;
|
|
171
|
+
Some(soonest.saturating_duration_since(Instant::now()))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn wake_due_timers(&self) {
|
|
175
|
+
let now = Instant::now();
|
|
176
|
+
let due: Vec<Waker> = {
|
|
177
|
+
let mut timers = self.timers.lock().unwrap_or_else(PoisonError::into_inner);
|
|
178
|
+
let (due, pending) = std::mem::take(&mut *timers)
|
|
179
|
+
.into_iter()
|
|
180
|
+
.partition(|(at, _)| *at <= now);
|
|
181
|
+
*timers = pending;
|
|
182
|
+
due.into_iter().map(|(_, waker)| waker).collect()
|
|
183
|
+
};
|
|
184
|
+
due.into_iter().for_each(Waker::wake);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/// Watch `source` for reading and writing, handing each change to `ready`'s
|
|
188
|
+
/// `on_event`.
|
|
189
|
+
pub(crate) fn register(
|
|
190
|
+
self: &Arc<Self>,
|
|
191
|
+
source: &mut impl Source,
|
|
192
|
+
ready: Weak<dyn Ready>,
|
|
193
|
+
) -> io::Result<Registration> {
|
|
194
|
+
let token = Token(self.next.fetch_add(1, Ordering::Relaxed));
|
|
195
|
+
self.sockets
|
|
196
|
+
.lock()
|
|
197
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
198
|
+
.insert(token, ready);
|
|
199
|
+
if let Err(err) =
|
|
200
|
+
self.registry
|
|
201
|
+
.register(source, token, Interest::READABLE | Interest::WRITABLE)
|
|
202
|
+
{
|
|
203
|
+
self.forget(token);
|
|
204
|
+
return Err(err);
|
|
205
|
+
}
|
|
206
|
+
Ok(Registration {
|
|
207
|
+
token,
|
|
208
|
+
reactor: self.clone(),
|
|
209
|
+
pid: Pid::current(),
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
fn forget(&self, token: Token) {
|
|
214
|
+
self.sockets
|
|
215
|
+
.lock()
|
|
216
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
217
|
+
.remove(&token);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// Run `future` on the reactor until it ends or the task is cancelled.
|
|
221
|
+
pub(crate) fn spawn(
|
|
222
|
+
self: &Arc<Self>,
|
|
223
|
+
future: impl Future<Output = ()> + Send + 'static,
|
|
224
|
+
) -> Arc<Task> {
|
|
225
|
+
let task = Arc::new(Task {
|
|
226
|
+
future: Mutex::new(Some(Box::pin(future))),
|
|
227
|
+
reactor: Arc::downgrade(self),
|
|
228
|
+
queued: AtomicBool::new(false),
|
|
229
|
+
});
|
|
230
|
+
task.clone().wake();
|
|
231
|
+
task
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn run_tasks(&self) {
|
|
235
|
+
loop {
|
|
236
|
+
let Some(task) = self
|
|
237
|
+
.run_queue
|
|
238
|
+
.lock()
|
|
239
|
+
.unwrap_or_else(PoisonError::into_inner)
|
|
240
|
+
.pop_front()
|
|
241
|
+
else {
|
|
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
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// One host socket the reactor watches; the socket deregisters it as it
|
|
261
|
+
/// lets the host socket go.
|
|
262
|
+
pub(crate) struct Registration {
|
|
263
|
+
token: Token,
|
|
264
|
+
reactor: Arc<Reactor>,
|
|
265
|
+
pid: Pid,
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
impl Registration {
|
|
269
|
+
/// Stop watching `source`, which the socket closes next; the token goes
|
|
270
|
+
/// once the registration drops. A forked child leaves its parent's
|
|
271
|
+
/// registration be: the epoll instance is shared, and removing the one
|
|
272
|
+
/// `<fd, file>` entry would stop the parent watching its own socket.
|
|
273
|
+
pub(crate) fn deregister(&self, source: &mut impl Source) {
|
|
274
|
+
if self.pid.is_current() {
|
|
275
|
+
let _ = self.reactor.registry.deregister(source);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/// Run `ready`'s `on_event` again after the reactor's next round of
|
|
280
|
+
/// events, once however often it asks, as NAPI repolls a device that
|
|
281
|
+
/// spent its weight with more to take and `NAPI_STATE_SCHED` keeps it
|
|
282
|
+
/// queued once; no further event may come for what already waits.
|
|
283
|
+
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);
|
|
289
|
+
if !repoll.iter().any(|queued| queued.ptr_eq(&ready)) {
|
|
290
|
+
repoll.push(ready);
|
|
291
|
+
}
|
|
292
|
+
drop(repoll);
|
|
293
|
+
let _ = self.reactor.waker.wake();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
impl Drop for Registration {
|
|
298
|
+
fn drop(&mut self) {
|
|
299
|
+
self.reactor.forget(self.token);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// A task's armed timer, disarmed as it drops.
|
|
304
|
+
#[cfg(any(test, all(unix, not(target_os = "macos"))))]
|
|
305
|
+
pub(crate) struct Timer {
|
|
306
|
+
reactor: Arc<Reactor>,
|
|
307
|
+
waker: Waker,
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
#[cfg(any(test, all(unix, not(target_os = "macos"))))]
|
|
311
|
+
impl Drop for Timer {
|
|
312
|
+
fn drop(&mut self) {
|
|
313
|
+
self.reactor.disarm(&self.waker);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
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
|
+
#[cfg(test)]
|
|
354
|
+
mod tests {
|
|
355
|
+
use std::sync::mpsc;
|
|
356
|
+
use std::task::Poll;
|
|
357
|
+
use std::time::Duration;
|
|
358
|
+
|
|
359
|
+
use super::*;
|
|
360
|
+
|
|
361
|
+
// A reactor started in another process, as a forked child inherits its
|
|
362
|
+
// parent's, is never handed out: the child starts its own.
|
|
363
|
+
#[test]
|
|
364
|
+
fn a_reactor_started_in_another_process_is_replaced() {
|
|
365
|
+
let inherited = Reactor::current().expect("a reactor starts");
|
|
366
|
+
CURRENT.inherit();
|
|
367
|
+
let own = Reactor::current().expect("a reactor starts");
|
|
368
|
+
assert!(
|
|
369
|
+
!Arc::ptr_eq(&inherited, &own),
|
|
370
|
+
"Reactor::current must start a reactor of its own once the one it holds was started \
|
|
371
|
+
in another process"
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/// A socket the reactor tells of each change through a channel.
|
|
376
|
+
struct Tells(Mutex<mpsc::Sender<()>>);
|
|
377
|
+
|
|
378
|
+
impl Ready for Tells {
|
|
379
|
+
fn on_event(&self) {
|
|
380
|
+
let _ = self.0.lock().unwrap().send(());
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// A forked child shares its parent's epoll instance, where an inherited
|
|
385
|
+
// socket's `<fd, file>` entry stands once for both, so a child letting the
|
|
386
|
+
// socket go must leave the parent watching it.
|
|
387
|
+
// @behavior NW-232
|
|
388
|
+
#[test]
|
|
389
|
+
fn a_registration_made_in_another_process_stays_watched_once_let_go() {
|
|
390
|
+
let reactor = Reactor::current().expect("a reactor starts");
|
|
391
|
+
let (tx, rx) = mpsc::channel();
|
|
392
|
+
let ready: Arc<dyn Ready> = Arc::new(Tells(Mutex::new(tx)));
|
|
393
|
+
let addr = "127.0.0.1:0".parse().unwrap();
|
|
394
|
+
let mut socket = mio::net::UdpSocket::bind(addr).expect("a UDP socket binds");
|
|
395
|
+
let mut registration = reactor
|
|
396
|
+
.register(&mut socket, Arc::downgrade(&ready))
|
|
397
|
+
.expect("the socket registers");
|
|
398
|
+
while rx.recv_timeout(Duration::from_millis(50)).is_ok() {}
|
|
399
|
+
registration.pid = Pid::parent();
|
|
400
|
+
registration.deregister(&mut socket);
|
|
401
|
+
let sender = std::net::UdpSocket::bind(addr).expect("a UDP socket binds");
|
|
402
|
+
sender
|
|
403
|
+
.send_to(b"x", socket.local_addr().unwrap())
|
|
404
|
+
.expect("the datagram goes");
|
|
405
|
+
assert!(
|
|
406
|
+
rx.recv_timeout(Duration::from_secs(5)).is_ok(),
|
|
407
|
+
"a forked child letting go of a guest's socket its parent watches must leave the \
|
|
408
|
+
parent woken by that socket's events"
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// A task woken from another thread runs on the reactor and ends.
|
|
413
|
+
#[test]
|
|
414
|
+
fn a_spawned_task_runs_to_its_end_once_woken() {
|
|
415
|
+
let reactor = Reactor::current().expect("a reactor starts");
|
|
416
|
+
let (tx, rx) = mpsc::channel();
|
|
417
|
+
let gate = Arc::new(Mutex::new((false, None::<Waker>)));
|
|
418
|
+
let waiting = gate.clone();
|
|
419
|
+
reactor.spawn(async move {
|
|
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"
|
|
442
|
+
);
|
|
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
|
+
assert!(
|
|
466
|
+
rx.recv_timeout(Duration::from_secs(5)).is_ok(),
|
|
467
|
+
"a task the reactor was asked to wake at an instant must run again once it passes"
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// A waker asked to be woken again holds one timer, the newest, however
|
|
472
|
+
// often it asked before.
|
|
473
|
+
#[test]
|
|
474
|
+
fn a_waker_asking_again_holds_one_timer() {
|
|
475
|
+
let reactor = Reactor::current().expect("a reactor starts");
|
|
476
|
+
let waker = Waker::noop().clone();
|
|
477
|
+
let later = Instant::now() + Duration::from_secs(30);
|
|
478
|
+
let armed: Vec<Timer> = (0..3)
|
|
479
|
+
.map(|_| reactor.wake_at(later, waker.clone()))
|
|
480
|
+
.collect();
|
|
481
|
+
let held = timers_of(&reactor, &waker);
|
|
482
|
+
drop(armed);
|
|
483
|
+
assert_eq!(
|
|
484
|
+
(held, timers_of(&reactor, &waker)),
|
|
485
|
+
(1, 0),
|
|
486
|
+
"a waker asked to be woken three times must hold one timer, the newest, and none \
|
|
487
|
+
once its timers drop"
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/// How many timers `reactor` holds for `waker`.
|
|
492
|
+
fn timers_of(reactor: &Reactor, waker: &Waker) -> usize {
|
|
493
|
+
reactor
|
|
494
|
+
.timers
|
|
495
|
+
.lock()
|
|
496
|
+
.unwrap()
|
|
497
|
+
.iter()
|
|
498
|
+
.filter(|(_, armed)| armed.will_wake(waker))
|
|
499
|
+
.count()
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// A task cancelled while its timer is armed leaves no timer behind, so a
|
|
503
|
+
// guest dropping lookups never fills the reactor's timers.
|
|
504
|
+
#[test]
|
|
505
|
+
fn a_cancelled_task_leaves_no_timer() {
|
|
506
|
+
let reactor = Reactor::current().expect("a reactor starts");
|
|
507
|
+
let (armed_tx, armed_rx) = mpsc::channel();
|
|
508
|
+
let timing = reactor.clone();
|
|
509
|
+
let later = Instant::now() + Duration::from_secs(30);
|
|
510
|
+
let task = reactor.spawn(async move {
|
|
511
|
+
let mut timer = None;
|
|
512
|
+
std::future::poll_fn(|cx| {
|
|
513
|
+
timer.get_or_insert_with(|| timing.wake_at(later, cx.waker().clone()));
|
|
514
|
+
let _ = armed_tx.send(());
|
|
515
|
+
Poll::<()>::Pending
|
|
516
|
+
})
|
|
517
|
+
.await;
|
|
518
|
+
});
|
|
519
|
+
armed_rx
|
|
520
|
+
.recv_timeout(Duration::from_secs(5))
|
|
521
|
+
.expect("the task arms its timer");
|
|
522
|
+
let waker = Waker::from(task.clone());
|
|
523
|
+
let held = timers_of(&reactor, &waker);
|
|
524
|
+
task.cancel();
|
|
525
|
+
assert_eq!(
|
|
526
|
+
(held, timers_of(&reactor, &waker)),
|
|
527
|
+
(1, 0),
|
|
528
|
+
"a cancelled lookup's timer must leave the reactor with the lookup, not stay until due"
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
}
|