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.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/.githooks/linux-tests.sh +29 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/CHANGELOG.md +125 -0
  5. data/Cargo.lock +457 -494
  6. data/README.md +2 -3
  7. data/ROADMAP.md +0 -1
  8. data/crates/kobako-runtime/CHANGELOG.md +90 -0
  9. data/crates/kobako-runtime/Cargo.toml +14 -15
  10. data/crates/kobako-runtime/README.md +1 -1
  11. data/crates/kobako-runtime/src/cred.rs +32 -20
  12. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +300 -22
  13. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +56 -22
  14. data/crates/kobako-runtime/src/filesystem/bind/unicode.rs +109 -0
  15. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +33 -14
  16. data/crates/kobako-runtime/src/filesystem/bind.rs +393 -220
  17. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +2 -0
  18. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +9 -10
  19. data/crates/kobako-runtime/src/filesystem/overlay.rs +277 -46
  20. data/crates/kobako-runtime/src/filesystem/rdir.rs +8 -27
  21. data/crates/kobako-runtime/src/filesystem/testing.rs +31 -0
  22. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +47 -13
  23. data/crates/kobako-runtime/src/filesystem.rs +2 -0
  24. data/crates/kobako-runtime/src/fork.rs +239 -16
  25. data/crates/kobako-runtime/src/limits.rs +17 -41
  26. data/crates/kobako-runtime/src/locking/seqlock.rs +29 -3
  27. data/crates/kobako-runtime/src/locking.rs +508 -113
  28. data/crates/kobako-runtime/src/memcg.rs +83 -160
  29. data/crates/kobako-runtime/src/mount.rs +0 -30
  30. data/crates/kobako-runtime/src/net/connections.rs +53 -13
  31. data/crates/kobako-runtime/src/net/egress.rs +44 -24
  32. data/crates/kobako-runtime/src/net/http.rs +888 -456
  33. data/crates/kobako-runtime/src/net/lookup.rs +150 -50
  34. data/crates/kobako-runtime/src/net/namespace.rs +97 -95
  35. data/crates/kobako-runtime/src/net/reactor.rs +298 -203
  36. data/crates/kobako-runtime/src/net/resolv/darwin.rs +8 -3
  37. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +50 -0
  38. data/crates/kobako-runtime/src/net/resolv/stub.rs +29 -23
  39. data/crates/kobako-runtime/src/net/resolv/windows.rs +14 -44
  40. data/crates/kobako-runtime/src/net/resolv.rs +39 -15
  41. data/crates/kobako-runtime/src/net/skbuff.rs +109 -0
  42. data/crates/kobako-runtime/src/net/socket.rs +51 -52
  43. data/crates/kobako-runtime/src/net/task.rs +223 -0
  44. data/crates/kobako-runtime/src/net/tcp.rs +410 -158
  45. data/crates/kobako-runtime/src/net/tls.rs +65 -3
  46. data/crates/kobako-runtime/src/net/transport.rs +72 -47
  47. data/crates/kobako-runtime/src/net/udp.rs +231 -129
  48. data/crates/kobako-runtime/src/net.rs +3 -0
  49. data/crates/kobako-runtime/src/sched.rs +244 -58
  50. data/crates/kobako-runtime/src/security.rs +27 -16
  51. data/crates/kobako-runtime/src/vfs/dentry.rs +80 -41
  52. data/crates/kobako-runtime/src/vfs/file.rs +52 -53
  53. data/crates/kobako-runtime/src/vfs/kern_path.rs +24 -11
  54. data/crates/kobako-runtime/src/vfs/namei.rs +399 -184
  55. data/crates/kobako-runtime/src/vfs/namespace.rs +123 -40
  56. data/crates/kobako-runtime/src/vfs/stat.rs +105 -16
  57. data/crates/kobako-runtime/src/vfs.rs +1 -0
  58. data/crates/kobako-runtime/src/wait.rs +75 -50
  59. data/crates/kobako-transport/CHANGELOG.md +7 -0
  60. data/crates/kobako-transport/Cargo.toml +1 -1
  61. data/crates/kobako-transport/README.md +1 -1
  62. data/crates/kobako-wasmtime/CHANGELOG.md +47 -0
  63. data/crates/kobako-wasmtime/Cargo.toml +27 -6
  64. data/crates/kobako-wasmtime/README.md +3 -3
  65. data/crates/kobako-wasmtime/src/abi.rs +2 -1
  66. data/crates/kobako-wasmtime/src/ambient.rs +11 -2
  67. data/crates/kobako-wasmtime/src/cache/unix.rs +95 -0
  68. data/crates/kobako-wasmtime/src/cache/windows.rs +359 -0
  69. data/crates/kobako-wasmtime/src/cache.rs +88 -103
  70. data/crates/kobako-wasmtime/src/driver.rs +40 -27
  71. data/crates/kobako-wasmtime/src/invocation.rs +21 -7
  72. data/crates/kobako-wasmtime/src/lib.rs +2 -0
  73. data/crates/kobako-wasmtime/src/limiter.rs +78 -25
  74. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +13 -17
  75. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +4 -5
  76. data/crates/kobako-wasmtime/src/preview1/differential.rs +1 -1
  77. data/crates/kobako-wasmtime/src/preview1/fd.rs +23 -19
  78. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +130 -197
  79. data/crates/kobako-wasmtime/src/preview1/path.rs +3 -3
  80. data/crates/kobako-wasmtime/src/preview1/poll.rs +28 -22
  81. data/crates/kobako-wasmtime/src/preview1/random.rs +125 -0
  82. data/crates/kobako-wasmtime/src/preview1/testing.rs +31 -0
  83. data/crates/kobako-wasmtime/src/preview1.rs +85 -9
  84. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +2 -2
  85. data/crates/kobako-wasmtime/src/preview2/host.rs +609 -249
  86. data/crates/kobako-wasmtime/src/preview2/http.rs +299 -215
  87. data/crates/kobako-wasmtime/src/preview2/io.rs +17 -4
  88. data/crates/kobako-wasmtime/src/preview2/table.rs +24 -72
  89. data/crates/kobako-wasmtime/src/trap.rs +68 -23
  90. data/crates/kobako-wasmtime/src/waits.rs +59 -0
  91. data/crates/kobako-wasmtime/src/wasi.rs +91 -10
  92. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +13 -0
  93. data/data/kobako.wasm +0 -0
  94. data/ext/kobako/Cargo.toml +6 -2
  95. data/ext/kobako/src/escape.rs +332 -26
  96. data/ext/kobako/src/filesystem/grants.rs +5 -7
  97. data/ext/kobako/src/filesystem/ruby.rs +10 -3
  98. data/ext/kobako/src/filesystem.rs +47 -8
  99. data/ext/kobako/src/fork.rs +72 -0
  100. data/ext/kobako/src/gvl.rs +499 -76
  101. data/ext/kobako/src/handoff.rs +490 -0
  102. data/ext/kobako/src/lib.rs +10 -0
  103. data/ext/kobako/src/network.rs +5 -3
  104. data/ext/kobako/src/pinned_value.rs +28 -15
  105. data/ext/kobako/src/runtime/bridge.rs +32 -25
  106. data/ext/kobako/src/runtime.rs +74 -43
  107. data/lib/kobako/catalog/handles.rb +9 -5
  108. data/lib/kobako/codec/decoder.rb +1 -1
  109. data/lib/kobako/codec/state.rb +2 -18
  110. data/lib/kobako/codec/value_budget.rb +4 -4
  111. data/lib/kobako/codec.rb +5 -6
  112. data/lib/kobako/errors.rb +4 -7
  113. data/lib/kobako/filesystem.rb +1 -1
  114. data/lib/kobako/fork.rb +42 -0
  115. data/lib/kobako/handle.rb +1 -2
  116. data/lib/kobako/sandbox.rb +17 -36
  117. data/lib/kobako/sandbox_options.rb +7 -9
  118. data/lib/kobako/usage.rb +4 -2
  119. data/lib/kobako/version.rb +1 -1
  120. data/lib/kobako.rb +2 -2
  121. data/sig/kobako/catalog/handles.rbs +2 -0
  122. data/sig/kobako/codec/state.rbs +0 -4
  123. data/sig/kobako/codec/value_budget.rbs +1 -1
  124. data/sig/kobako/codec.rbs +2 -2
  125. data/sig/kobako/errors.rbs +0 -3
  126. data/sig/kobako/fork.rbs +17 -0
  127. data/sig/kobako/runtime.rbs +2 -2
  128. data/sig/kobako/sandbox.rbs +3 -12
  129. data/sig/kobako/sandbox_options.rbs +0 -2
  130. metadata +15 -4
  131. data/lib/kobako/pool.rb +0 -162
  132. data/sig/core/thread.rbs +0 -6
  133. data/sig/kobako/pool.rbs +0 -46
@@ -10,7 +10,6 @@ use std::sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError};
10
10
  use socket2::SockRef;
11
11
 
12
12
  use crate::cred::Cred;
13
- use crate::memcg::MemoryCharge;
14
13
  use crate::vfs::{
15
14
  alloc_file_pseudo, Dentry, FMode, File, FileOperations, FileSystemType, FileType, Inode,
16
15
  InodeOperations, Kiocb, Kstat, OpenHow, PollMask, SuperBlock, VfsMount,
@@ -249,15 +248,11 @@ pub(crate) struct InetSock {
249
248
  /// Whether the bind named its port, which the socket then keeps once it
250
249
  /// closes, as `SOCK_BINDPORT_LOCK` does.
251
250
  bindport_locked: bool,
252
- /// What its queues may hold, reserved against the Invocation's memory
253
- /// until the socket is released.
254
- memory: Option<MemoryCharge>,
255
251
  }
256
252
 
257
253
  impl InetSock {
258
- /// A socket whose queues start at `buffers`, within what `memory`
259
- /// reserved.
260
- pub(crate) fn new(memory: MemoryCharge, buffers: Buffers) -> Self {
254
+ /// A socket whose queues start at `buffers`.
255
+ pub(crate) fn new(buffers: Buffers) -> Self {
261
256
  Self {
262
257
  num: None,
263
258
  sport: None,
@@ -270,7 +265,6 @@ impl InetSock {
270
265
  send_shutdown: false,
271
266
  place: None,
272
267
  bindport_locked: false,
273
- memory: Some(memory),
274
268
  }
275
269
  }
276
270
 
@@ -378,11 +372,10 @@ impl InetSock {
378
372
  }
379
373
  }
380
374
 
381
- /// Give back the socket's place and its memory, as `sk_free` does once
382
- /// the socket is released.
375
+ /// Give back the socket's place, as `sk_free` does once the socket is
376
+ /// released.
383
377
  pub(crate) fn release(&mut self) {
384
378
  self.place = None;
385
- self.memory = None;
386
379
  }
387
380
 
388
381
  /// What `opt` reads at the socket and IP levels, as `sk_getsockopt` and
@@ -437,9 +430,8 @@ const TCP_WMEM_DEFAULT: usize = 16 * 1024;
437
430
  const TCP_RMEM_DEFAULT: usize = 128 * 1024;
438
431
 
439
432
  /// How much a socket's queues hold, as `sk_rcvbuf` and `sk_sndbuf`. Each
440
- /// starts at Linux's default and never passes the buffer its memory
441
- /// reserved, which plays `rmem_max` and `wmem_max` as twice the most a set
442
- /// takes.
433
+ /// starts at Linux's default and never passes `MAX_SOCKET_BUFFER`, which
434
+ /// plays `rmem_max` and `wmem_max` as twice the most a set takes.
443
435
  pub(crate) struct Buffers {
444
436
  pub(crate) rcvbuf: usize,
445
437
  pub(crate) sndbuf: usize,
@@ -450,19 +442,20 @@ pub(crate) struct Buffers {
450
442
 
451
443
  impl Default for Buffers {
452
444
  /// A datagram socket's, Linux's `rmem_default` and `wmem_default`, both
453
- /// past the reserved buffer.
445
+ /// past the cap.
454
446
  fn default() -> Self {
455
- Self::within_reserve(MAX_SOCKET_BUFFER, MAX_SOCKET_BUFFER)
447
+ Self::new(MAX_SOCKET_BUFFER, MAX_SOCKET_BUFFER)
456
448
  }
457
449
  }
458
450
 
459
451
  impl Buffers {
460
452
  /// A TCP socket's, until it connects.
461
453
  pub(crate) fn tcp() -> Self {
462
- Self::within_reserve(TCP_RMEM_DEFAULT, TCP_WMEM_DEFAULT)
454
+ Self::new(TCP_RMEM_DEFAULT, TCP_WMEM_DEFAULT)
463
455
  }
464
456
 
465
- fn within_reserve(rcvbuf: usize, sndbuf: usize) -> Self {
457
+ /// Ones of `rcvbuf` and `sndbuf` bytes, each held to the cap.
458
+ fn new(rcvbuf: usize, sndbuf: usize) -> Self {
466
459
  Self {
467
460
  rcvbuf: rcvbuf.min(MAX_SOCKET_BUFFER),
468
461
  sndbuf: sndbuf.min(MAX_SOCKET_BUFFER),
@@ -471,8 +464,8 @@ impl Buffers {
471
464
  }
472
465
 
473
466
  /// Grow a connected TCP socket's send buffer, as `tcp_sndbuf_expand` makes
474
- /// room for its first window: two of `TCP_INIT_CWND` segments, which pass
475
- /// the reserved buffer at any MSS. One the guest set stays.
467
+ /// room for its window: two of `TCP_INIT_CWND` segments, which pass the
468
+ /// cap at any MSS. One the guest set stays.
476
469
  pub(crate) fn sndbuf_expand(&mut self) {
477
470
  if !self.sndbuf_locked {
478
471
  self.sndbuf = MAX_SOCKET_BUFFER;
@@ -648,44 +641,30 @@ pub(crate) fn check_family(family: AddressFamily) -> Result<(), Errno> {
648
641
  }
649
642
 
650
643
  impl TcpSock {
651
- /// A TCP socket of `family`, connected to nothing, in `net` for `cred`,
652
- /// as `__sock_create` makes one; it connects through `net`'s Protocol
653
- /// Family, and its buffers count against `cred`'s memory, as a socket's
654
- /// do against its creator's memory cgroup.
655
- pub fn create(
656
- net: &Namespace,
657
- cred: &Arc<Cred>,
658
- family: AddressFamily,
659
- ) -> Result<Arc<Self>, Errno> {
644
+ /// A TCP socket of `family`, connected to nothing, in `net`, as
645
+ /// `__sock_create` makes one; it connects through `net`'s Protocol
646
+ /// Family.
647
+ pub fn create(net: &Namespace, family: AddressFamily) -> Result<Arc<Self>, Errno> {
660
648
  check_family(family)?;
661
- Self::create_kern(net.family(), net.tcp_sockets(), cred)
649
+ Ok(Self::create_kern(net.family(), net.tcp_sockets()))
662
650
  }
663
651
 
664
652
  /// A TCP socket the runtime makes for itself, as `sock_create_kern` does:
665
- /// it connects through `family`, `sockets` holds it until the Invocation
666
- /// ends, and its buffers count against `cred`'s memory.
667
- pub(crate) fn create_kern(
668
- family: &Arc<dyn ProtocolFamily>,
669
- sockets: &TcpSockets,
670
- cred: &Arc<Cred>,
671
- ) -> Result<Arc<Self>, Errno> {
672
- let sock = Self::new(family.clone(), cred)?;
653
+ /// it connects through `family`, and `sockets` holds it until the
654
+ /// Invocation ends.
655
+ pub(crate) fn create_kern(family: &Arc<dyn ProtocolFamily>, sockets: &TcpSockets) -> Arc<Self> {
656
+ let sock = Self::new(family.clone());
673
657
  sockets.hold(&sock);
674
- Ok(sock)
658
+ sock
675
659
  }
676
660
  }
677
661
 
678
662
  impl UdpSock {
679
- /// A UDP socket of `family`, connected to nothing, in `net` for `cred`,
680
- /// as `__sock_create` makes one; its receive queue counts against
681
- /// `cred`'s memory.
682
- pub fn create(
683
- _net: &Namespace,
684
- cred: &Arc<Cred>,
685
- family: AddressFamily,
686
- ) -> Result<Arc<Self>, Errno> {
663
+ /// A UDP socket of `family`, connected to nothing, in `net`, as
664
+ /// `__sock_create` makes one.
665
+ pub fn create(_net: &Namespace, family: AddressFamily) -> Result<Arc<Self>, Errno> {
687
666
  check_family(family)?;
688
- Self::new(cred)
667
+ Ok(Self::new())
689
668
  }
690
669
  }
691
670
 
@@ -833,6 +812,27 @@ pub(crate) mod testing {
833
812
  within_max_wait(|| do_poll(|| None, |table| poll(table).contains(mask).then_some(())));
834
813
  assert_eq!(found, Ok(Some(())), "the socket's poll must find {mask:?}");
835
814
  }
815
+
816
+ /// How many times a wait on `poll` to read woke over `quiet`, and whether
817
+ /// it found something: each wake asks `poll` again, so a wait nothing
818
+ /// woke asks it once as it starts and once as it ends.
819
+ pub(crate) fn looks_waiting_to_read(
820
+ poll: impl Fn(Option<&mut PollTable>) -> PollMask,
821
+ quiet: std::time::Duration,
822
+ ) -> (usize, bool) {
823
+ let end = std::time::Instant::now() + quiet;
824
+ let mut looks = 0;
825
+ let found = within_max_wait(|| {
826
+ do_poll(
827
+ || Some(end),
828
+ |table| {
829
+ looks += 1;
830
+ poll(table).contains(PollMask::IN).then_some(())
831
+ },
832
+ )
833
+ });
834
+ (looks, found == Ok(Some(())))
835
+ }
836
836
  }
837
837
 
838
838
  #[cfg(test)]
@@ -850,11 +850,10 @@ mod tests {
850
850
  Arc::new(super::super::family::testing::CountsDials::default()),
851
851
  Arc::new(super::super::HyperOutbound::new()),
852
852
  );
853
- let cred = crate::security::prepare_creds(&[], None);
854
853
  let answers = (
855
- TcpSock::create(&net, &cred, AddressFamily::Inet6).err(),
856
- UdpSock::create(&net, &cred, AddressFamily::Inet6).err(),
857
- TcpSock::create(&net, &cred, AddressFamily::Inet).is_ok(),
854
+ TcpSock::create(&net, AddressFamily::Inet6).err(),
855
+ UdpSock::create(&net, AddressFamily::Inet6).err(),
856
+ TcpSock::create(&net, AddressFamily::Inet).is_ok(),
858
857
  );
859
858
  assert_eq!(
860
859
  answers,
@@ -0,0 +1,223 @@
1
+ //! The work an HTTP exchange or a name lookup does, as a future only the
2
+ //! guest waiting on it polls, as a process moves its own non-blocking
3
+ //! requests only while it runs. A request's slow step, such as a certificate
4
+ //! check, so holds its own guest's thread and never another Sandbox's wait.
5
+ //! The reactor only wakes the request's wait queue.
6
+
7
+ use std::future::Future;
8
+ use std::pin::Pin;
9
+ use std::sync::atomic::{AtomicUsize, Ordering};
10
+ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
11
+ use std::task::{Context, Poll, Wake, Waker};
12
+
13
+ use crate::wait::WaitQueueHead;
14
+
15
+ type Running = Pin<Box<dyn Future<Output = ()> + Send>>;
16
+
17
+ /// The steps one poll of a task may take on its resources, as tokio's
18
+ /// cooperative budget gives each poll of a task 128.
19
+ const STEPS_PER_POLL: usize = 128;
20
+
21
+ /// What one poll of a task may still move, as tokio's coop budget: each step
22
+ /// a resource takes spends one, and a resource that finds none left wakes
23
+ /// the task and answers `Pending`. A poll so returns however ready its
24
+ /// resources stay, and its guest looks at its deadline and its stop before
25
+ /// the next.
26
+ pub(crate) struct PollBudget(AtomicUsize);
27
+
28
+ impl Default for PollBudget {
29
+ fn default() -> Self {
30
+ Self(AtomicUsize::new(STEPS_PER_POLL))
31
+ }
32
+ }
33
+
34
+ impl PollBudget {
35
+ fn renew(&self) {
36
+ self.0.store(STEPS_PER_POLL, Ordering::Relaxed);
37
+ }
38
+
39
+ /// A step to take, or `Pending` with the task woken once none is left,
40
+ /// as `poll_proceed`; the step is given back unless it made progress.
41
+ pub(crate) fn poll_proceed(&self, cx: &mut Context<'_>) -> Poll<Step<'_>> {
42
+ let spent = self
43
+ .0
44
+ .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |left| {
45
+ left.checked_sub(1)
46
+ });
47
+ match spent {
48
+ Ok(_) => Poll::Ready(Step {
49
+ budget: self,
50
+ progressed: false,
51
+ }),
52
+ Err(_) => {
53
+ cx.waker().wake_by_ref();
54
+ Poll::Pending
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ /// One step a resource took, given back as it drops unless it made
61
+ /// progress, as tokio's `RestoreOnPending`.
62
+ pub(crate) struct Step<'a> {
63
+ budget: &'a PollBudget,
64
+ progressed: bool,
65
+ }
66
+
67
+ impl Step<'_> {
68
+ pub(crate) fn made_progress(mut self) {
69
+ self.progressed = true;
70
+ }
71
+ }
72
+
73
+ impl Drop for Step<'_> {
74
+ fn drop(&mut self) {
75
+ if !self.progressed {
76
+ self.budget.0.fetch_add(1, Ordering::Relaxed);
77
+ }
78
+ }
79
+ }
80
+
81
+ pub(crate) struct Task {
82
+ future: Mutex<Option<Running>>,
83
+ /// Wakes the queue the request's guest waits on, so it polls again.
84
+ waker: Waker,
85
+ /// Renewed for each poll, so one poll moves the future a bounded way.
86
+ budget: Arc<PollBudget>,
87
+ }
88
+
89
+ impl Task {
90
+ /// `future`, each wake of which wakes `wait`.
91
+ pub(crate) fn new(
92
+ future: impl Future<Output = ()> + Send + 'static,
93
+ wait: Arc<WaitQueueHead>,
94
+ ) -> Self {
95
+ Self::spending(future, wait, Arc::default())
96
+ }
97
+
98
+ /// `future`, whose resources spend `budget`, renewed for each poll.
99
+ pub(crate) fn spending(
100
+ future: impl Future<Output = ()> + Send + 'static,
101
+ wait: Arc<WaitQueueHead>,
102
+ budget: Arc<PollBudget>,
103
+ ) -> Self {
104
+ Self {
105
+ future: Mutex::new(Some(Box::pin(future))),
106
+ waker: Waker::from(Arc::new(WakesQueue(wait))),
107
+ budget,
108
+ }
109
+ }
110
+
111
+ /// Move the future as far as its budget lets it without waiting, letting
112
+ /// go of what it holds once it ends.
113
+ pub(crate) fn poll(&self) {
114
+ let mut future = self.future();
115
+ self.budget.renew();
116
+ let ended = future.as_mut().is_some_and(|running| {
117
+ running
118
+ .as_mut()
119
+ .poll(&mut Context::from_waker(&self.waker))
120
+ .is_ready()
121
+ });
122
+ if ended {
123
+ *future = None;
124
+ }
125
+ }
126
+
127
+ /// Drop the future where it stands, with every socket it holds.
128
+ pub(crate) fn cancel(&self) {
129
+ let future = self.future().take();
130
+ drop(future);
131
+ }
132
+
133
+ fn future(&self) -> MutexGuard<'_, Option<Running>> {
134
+ self.future.lock().unwrap_or_else(PoisonError::into_inner)
135
+ }
136
+ }
137
+
138
+ struct WakesQueue(Arc<WaitQueueHead>);
139
+
140
+ impl Wake for WakesQueue {
141
+ fn wake(self: Arc<Self>) {
142
+ self.0.wake_up();
143
+ }
144
+
145
+ fn wake_by_ref(self: &Arc<Self>) {
146
+ self.0.wake_up();
147
+ }
148
+ }
149
+
150
+ #[cfg(test)]
151
+ mod tests {
152
+ use std::sync::atomic::{AtomicBool, Ordering};
153
+ use std::task::Poll;
154
+
155
+ use super::*;
156
+ use crate::sched::testing::{count_waits, join, wait_until, within_max_wait};
157
+ use crate::wait::{do_poll, poll_wait};
158
+
159
+ // A request woken from another thread moves on once its guest polls it
160
+ // again, as its wake reaches the queue the guest waits on.
161
+ #[test]
162
+ fn a_task_woken_from_another_thread_moves_once_its_guest_polls_again() {
163
+ let wait = Arc::new(WaitQueueHead::default());
164
+ let gate = Arc::new(Mutex::new((false, None::<Waker>)));
165
+ let ended = Arc::new(AtomicBool::new(false));
166
+ let task = {
167
+ let (gate, ended) = (gate.clone(), ended.clone());
168
+ Task::new(
169
+ async move {
170
+ std::future::poll_fn(|cx| {
171
+ let mut gate = gate.lock().unwrap();
172
+ if gate.0 {
173
+ Poll::Ready(())
174
+ } else {
175
+ gate.1 = Some(cx.waker().clone());
176
+ Poll::Pending
177
+ }
178
+ })
179
+ .await;
180
+ ended.store(true, Ordering::SeqCst);
181
+ },
182
+ wait.clone(),
183
+ )
184
+ };
185
+ task.poll();
186
+ let waits = count_waits();
187
+ let opener = {
188
+ let (gate, waits) = (gate.clone(), waits.clone());
189
+ std::thread::spawn(move || {
190
+ wait_until("the guest waiting on the request's queue", || {
191
+ waits.load(Ordering::SeqCst) > 0
192
+ });
193
+ let waker = {
194
+ let mut gate = gate.lock().unwrap();
195
+ gate.0 = true;
196
+ gate.1.take()
197
+ };
198
+ waker.expect("the task waited").wake();
199
+ })
200
+ };
201
+ let answer = within_max_wait(|| {
202
+ do_poll(
203
+ || None,
204
+ |table| {
205
+ poll_wait(&wait, table);
206
+ // A poll once the deadline passed was not the wake's.
207
+ if crate::sched::fatal_signal_pending() {
208
+ return None;
209
+ }
210
+ task.poll();
211
+ ended.load(Ordering::SeqCst).then_some(())
212
+ },
213
+ )
214
+ });
215
+ join("the opener", opener);
216
+ assert_eq!(
217
+ answer,
218
+ Ok(Some(())),
219
+ "a request woken from another thread must wake the queue its guest waits on, and \
220
+ end once the guest polls it again, before the guest's deadline ends its wait"
221
+ );
222
+ }
223
+ }