
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! The scheduler's glue: its counters (`running`, `pending`, |
| 2 | //! `io_waiting`), idle and deadlock detection, and the reactor's wake-ups. |
| 3 | //! The hand-offs it is built on have loom models (`handoff`, `timers`, |
| 4 | //! `otp`, `inbox`); these run whole programs of test actors, on one thread |
| 5 | //! and on several, and check what the scheduler decides. |
| 6 | //! |
| 7 | //! Test actors talk through statics: each test runs alone ([`test_turn`]). |
| 8 | //! An assertion that fails in an actor aborts the test binary (a panic |
| 9 | //! cannot unwind out of its `extern "C"` entry), as in the crate's other |
| 10 | //! actor tests; a wake-up that never comes fails [`yield_until`]. |
| 11 | |
| 12 | mod dist; |
| 13 | #[cfg(feature = "tls")] |
| 14 | mod transport; |
| 15 | |
| 16 | use std::io::{ErrorKind, Read, Write}; |
| 17 | use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; |
| 18 | use std::sync::{Condvar, Mutex}; |
| 19 | use std::thread::JoinHandle; |
| 20 | use std::time::{Duration, Instant}; |
| 21 | |
| 22 | use mio::net::UnixStream; |
| 23 | use mio::{Interest, Token}; |
| 24 | |
| 25 | use crate::abi::{Block, Ctx, UNIT, Value, YIELD_KILL, YIELD_NONE, int, int_get}; |
| 26 | use crate::fiber::Stack; |
| 27 | use crate::heap::drop_value; |
| 28 | use crate::otp::{Received, Tags, Timeout, Wait}; |
| 29 | |
| 30 | use super::{ |
| 31 | Body, EntryFn, Exit, HANDLER_STACK_POOL, HandlerStacks, STACK_SIZE, Shared, Tie, |
| 32 | WARM_HANDLER_STACKS, park, preempt_root, root_stack_size, run, spawn_actor, test_turn, this, |
| 33 | }; |
| 34 | |
| 35 | /// Thread counts each program runs with: one (the interpreter's order), |
| 36 | /// and several (stealing, one idle thread for all). |
| 37 | const THREADS: [usize; 2] = [1, 4]; |
| 38 | |
| 39 | #[test] |
| 40 | fn actor_roots_accept_all_five_and_reject_old_runtime_mismatches() { |
| 41 | use crate::process::RootCap; |
| 42 | assert_eq!( |
| 43 | super::parse_actor_caps(&[0, 1, 2, 3, 4]), |
| 44 | Some(vec![ |
| 45 | RootCap::Proc, |
| 46 | RootCap::Clock, |
| 47 | RootCap::Random, |
| 48 | RootCap::Fs, |
| 49 | RootCap::Net |
| 50 | ]) |
| 51 | ); |
| 52 | // The table is a positional contract, not a sorted-set invariant: this |
| 53 | // definition-id order was emitted by an older compiler. Accepting it keeps |
| 54 | // old binaries working while new compiler output uses canonical kind order. |
| 55 | assert_eq!( |
| 56 | super::parse_actor_caps(&[0, 2, 4, 3, 1]), |
| 57 | Some(vec![ |
| 58 | RootCap::Proc, |
| 59 | RootCap::Random, |
| 60 | RootCap::Net, |
| 61 | RootCap::Fs, |
| 62 | RootCap::Clock |
| 63 | ]) |
| 64 | ); |
| 65 | assert_eq!(super::parse_actor_caps(&[0, 1, 2, 3, 5]), None); |
| 66 | assert_eq!( |
| 67 | super::parse_actor_caps(&[0, 1, 2, 3, 4, 0]), |
| 68 | None, |
| 69 | "over five" |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /// Run `entry` as the root actor on `threads` threads, as `tl_main` does, |
| 74 | /// then check that the scheduler is at rest: nothing runs, is queued or |
| 75 | /// waits on a socket, and every actor is gone. Returns the root's exit. |
| 76 | fn run_root(threads: usize, entry: EntryFn) -> Exit { |
| 77 | run_root_clock(threads, false, entry) |
| 78 | } |
| 79 | |
| 80 | /// `run_root` with the `LOUISE_VIRTUAL_TIME` opt-in set explicitly, so |
| 81 | /// the tests do not depend on the environment (ADR 0055). |
| 82 | fn run_root_clock(threads: usize, virtual_time: bool, entry: EntryFn) -> Exit { |
| 83 | let _turn = test_turn(); |
| 84 | let mut sched = Shared::new(threads); |
| 85 | sched.virtual_time = virtual_time; |
| 86 | let (root, _) = spawn_actor( |
| 87 | &sched, |
| 88 | Body::Entry(entry), |
| 89 | root_stack_size(), |
| 90 | None, |
| 91 | Tie::None, |
| 92 | ); |
| 93 | let exit = run(&sched, root, threads); |
| 94 | crate::console::flush(); |
| 95 | assert_eq!(sched.running.load(Ordering::SeqCst), 0, "running"); |
| 96 | assert_eq!(sched.pending.load(Ordering::SeqCst), 0, "pending"); |
| 97 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0, "io_waiting"); |
| 98 | assert!(sched.control().io_waiters.is_empty(), "io_waiters"); |
| 99 | assert!(sched.actors.all().is_empty(), "actors left"); |
| 100 | exit |
| 101 | } |
| 102 | |
| 103 | /// Spawn an actor running `entry`, untied to the running actor. |
| 104 | /// |
| 105 | /// # Safety |
| 106 | /// `ctx` must be the running actor's context. |
| 107 | unsafe fn spawn(ctx: *mut Ctx, entry: EntryFn) -> i64 { |
| 108 | // SAFETY: forwarded caller guarantee. |
| 109 | let (a, sched) = unsafe { this(ctx) }; |
| 110 | spawn_actor( |
| 111 | sched, |
| 112 | Body::Entry(entry), |
| 113 | STACK_SIZE, |
| 114 | Some(a.pid), |
| 115 | Tie::None, |
| 116 | ) |
| 117 | .0 |
| 118 | } |
| 119 | |
| 120 | /// How long [`yield_until`] waits before it fails: far more than any |
| 121 | /// condition here takes (microseconds), so a lost wake-up fails the test |
| 122 | /// instead of hanging it. |
| 123 | const PATIENCE: Duration = Duration::from_secs(10); |
| 124 | |
| 125 | /// Yield until `done` holds (`what`, for the failure); the other actors |
| 126 | /// run meanwhile. |
| 127 | /// |
| 128 | /// # Safety |
| 129 | /// `ctx` must be the running actor's context, on its own stack. |
| 130 | unsafe fn yield_until(ctx: *mut Ctx, what: &str, done: impl Fn(&Shared) -> bool) { |
| 131 | let limit = Instant::now() + PATIENCE; |
| 132 | // SAFETY: forwarded caller guarantee; the scheduler outlives its actors. |
| 133 | unsafe { |
| 134 | let (_, sched) = this(ctx); |
| 135 | while !done(sched) { |
| 136 | assert!(Instant::now() < limit, "still waiting: {what}"); |
| 137 | preempt_root(ctx); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Wait for a message nobody sends; returns once the actor is stopped. |
| 143 | /// |
| 144 | /// # Safety |
| 145 | /// `ctx` must be the running actor's context. |
| 146 | unsafe fn wait_forever(ctx: *mut Ctx) { |
| 147 | // SAFETY: forwarded caller guarantee. |
| 148 | unsafe { |
| 149 | let (a, sched) = this(ctx); |
| 150 | let tag = sched.tag(); |
| 151 | let received = a.with_proc(|p| p.receive(a.inbox(), Tags::One(tag), false, Timeout::Never)); |
| 152 | assert!(matches!(received, Received::Parked(_)), "no message"); |
| 153 | let woken = park(ctx); |
| 154 | drop_value(ctx, woken); |
| 155 | assert_ne!((*ctx).yielding, YIELD_NONE, "only an exit ends the wait"); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// A connected pair of sockets; the first registered with the running |
| 160 | /// actor's reactor, for reading only (so its only events are the second |
| 161 | /// writing to it), with its token. |
| 162 | /// |
| 163 | /// # Safety |
| 164 | /// `ctx` must be the running actor's context. |
| 165 | unsafe fn socket_pair(ctx: *mut Ctx) -> (UnixStream, UnixStream, Token) { |
| 166 | // SAFETY: forwarded caller guarantee. |
| 167 | let (_, sched) = unsafe { this(ctx) }; |
| 168 | let (mut reader, writer) = UnixStream::pair().expect("a socket pair"); |
| 169 | let token = sched.io_token(); |
| 170 | sched |
| 171 | .with_registry(|r| r.register(&mut reader, token, Interest::READABLE)) |
| 172 | .expect("registered"); |
| 173 | (reader, writer, token) |
| 174 | } |
| 175 | |
| 176 | /// Park on socket `token`, as `net` does, unless an event came since the |
| 177 | /// count `seen`; false if the actor was stopped instead. |
| 178 | /// |
| 179 | /// # Safety |
| 180 | /// `ctx` must be the running actor's context. |
| 181 | unsafe fn wait_on_socket(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 182 | // SAFETY: forwarded caller guarantee. |
| 183 | unsafe { crate::net::wait_for(ctx, token, seen) } |
| 184 | } |
| 185 | |
| 186 | static COUNTED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 187 | |
| 188 | /// Yields a few times, then finishes. |
| 189 | unsafe extern "C" fn yields_then_ends(ctx: *mut Ctx) -> Value { |
| 190 | for _ in 0..3 { |
| 191 | // SAFETY: an actor's entry, on its own stack. |
| 192 | unsafe { preempt_root(ctx) }; |
| 193 | } |
| 194 | UNIT |
| 195 | } |
| 196 | |
| 197 | /// Waits on the socket in `COUNTED_TOKEN` until woken, then finishes. |
| 198 | unsafe extern "C" fn waits_on_the_socket(ctx: *mut Ctx) -> Value { |
| 199 | let token = Token(COUNTED_TOKEN.load(Ordering::SeqCst)); |
| 200 | // SAFETY: an actor's entry. |
| 201 | unsafe { |
| 202 | let seen = this(ctx).1.io_events(); |
| 203 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 204 | } |
| 205 | UNIT |
| 206 | } |
| 207 | |
| 208 | /// Spawns actors that yield and finish, and actors that wait on a socket |
| 209 | /// until the root writes to it; checks the counters while they wait. |
| 210 | unsafe extern "C" fn counts_actors(ctx: *mut Ctx) -> Value { |
| 211 | // SAFETY: the root actor's entry. |
| 212 | unsafe { |
| 213 | let (reader, mut writer, token) = socket_pair(ctx); |
| 214 | COUNTED_TOKEN.store(token.0, Ordering::SeqCst); |
| 215 | for _ in 0..4 { |
| 216 | spawn(ctx, yields_then_ends); |
| 217 | spawn(ctx, waits_on_the_socket); |
| 218 | } |
| 219 | // Only the root runs, nothing is queued, four wait on the socket. |
| 220 | yield_until(ctx, "the waiters to park", |s| { |
| 221 | s.io_waiting.load(Ordering::SeqCst) == 4 |
| 222 | && s.running.load(Ordering::SeqCst) == 1 |
| 223 | && s.pending.load(Ordering::SeqCst) == 0 |
| 224 | && s.actors.all().len() == 5 |
| 225 | }); |
| 226 | let (_, sched) = this(ctx); |
| 227 | assert_eq!(sched.control().io_waiters[&token].len(), 4); |
| 228 | writer.write_all(b"x").expect("written"); |
| 229 | yield_until(ctx, "the waiters to finish", |s| s.actors.all().len() == 1); |
| 230 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 231 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 232 | drop(reader); |
| 233 | } |
| 234 | UNIT |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn counters_rest_once_actors_ran_waited_on_a_socket_and_finished() { |
| 239 | for threads in THREADS { |
| 240 | assert_eq!( |
| 241 | run_root(threads, counts_actors), |
| 242 | Exit::Normal, |
| 243 | "{threads} threads" |
| 244 | ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /// Waits for a message nobody sends. |
| 249 | unsafe extern "C" fn waits_forever(ctx: *mut Ctx) -> Value { |
| 250 | // SAFETY: an actor's entry. |
| 251 | unsafe { wait_forever(ctx) }; |
| 252 | UNIT |
| 253 | } |
| 254 | |
| 255 | /// Spawns an actor waiting forever, then waits forever too. |
| 256 | unsafe extern "C" fn all_wait_forever(ctx: *mut Ctx) -> Value { |
| 257 | // SAFETY: the root actor's entry. |
| 258 | unsafe { |
| 259 | spawn(ctx, waits_forever); |
| 260 | wait_forever(ctx); |
| 261 | } |
| 262 | UNIT |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn actors_all_waiting_for_messages_are_a_deadlock() { |
| 267 | for threads in THREADS { |
| 268 | assert_eq!( |
| 269 | run_root(threads, all_wait_forever), |
| 270 | Exit::Crashed("deadlock: every actor is waiting for a message".into()), |
| 271 | "{threads} threads" |
| 272 | ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static BUSY_STARTED: AtomicUsize = AtomicUsize::new(0); |
| 277 | static BUSY_DONE: AtomicUsize = AtomicUsize::new(0); |
| 278 | |
| 279 | /// Counts itself started, then holds its thread without yielding until |
| 280 | /// the other busy actor started too: it can only start on another thread. |
| 281 | unsafe extern "C" fn busy_until_both_run(_ctx: *mut Ctx) -> Value { |
| 282 | BUSY_STARTED.fetch_add(1, Ordering::SeqCst); |
| 283 | let limit = Instant::now() + PATIENCE; |
| 284 | while BUSY_STARTED.load(Ordering::SeqCst) < 2 { |
| 285 | assert!(Instant::now() < limit, "the other busy actor never ran"); |
| 286 | std::hint::spin_loop(); |
| 287 | } |
| 288 | BUSY_DONE.fetch_add(1, Ordering::SeqCst); |
| 289 | UNIT |
| 290 | } |
| 291 | |
| 292 | /// Once the other threads parked, spawns two busy actors in one slice, |
| 293 | /// then waits for both. |
| 294 | unsafe extern "C" fn spawns_two_busy(ctx: *mut Ctx) -> Value { |
| 295 | // SAFETY: the root actor's entry. |
| 296 | unsafe { |
| 297 | yield_until(ctx, "the other threads parked", |sched| { |
| 298 | sched.parking.parked() == 3 |
| 299 | }); |
| 300 | spawn(ctx, busy_until_both_run); |
| 301 | spawn(ctx, busy_until_both_run); |
| 302 | yield_until(ctx, "both busy actors done", |_| { |
| 303 | BUSY_DONE.load(Ordering::SeqCst) == 2 |
| 304 | }); |
| 305 | } |
| 306 | UNIT |
| 307 | } |
| 308 | |
| 309 | /// The second of two actors spawned in one slice waits in the spawner's |
| 310 | /// queue (the first is in its next slot) while every other thread sleeps: |
| 311 | /// one is woken to take it, so the two run in parallel (ADR 0004, |
| 312 | /// Revision 8). |
| 313 | #[test] |
| 314 | fn actors_spawned_together_run_in_parallel() { |
| 315 | BUSY_STARTED.store(0, Ordering::SeqCst); |
| 316 | BUSY_DONE.store(0, Ordering::SeqCst); |
| 317 | assert_eq!(run_root(4, spawns_two_busy), Exit::Normal); |
| 318 | } |
| 319 | |
| 320 | /// Sleeps (in virtual time) while another actor waits forever. |
| 321 | unsafe extern "C" fn sleeps_while_another_waits(ctx: *mut Ctx) -> Value { |
| 322 | // SAFETY: the root actor's entry. |
| 323 | unsafe { |
| 324 | spawn(ctx, waits_forever); |
| 325 | let (a, sched) = this(ctx); |
| 326 | let deadline = sched.clock() + 60_000; |
| 327 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 328 | sched.add_timer(deadline, a.pid, wait); |
| 329 | let woken = park(ctx); |
| 330 | assert_eq!(woken, UNIT); |
| 331 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 332 | // Virtual time: the clock jumped to the deadline. |
| 333 | assert_eq!(sched.clock(), deadline); |
| 334 | } |
| 335 | UNIT |
| 336 | } |
| 337 | |
| 338 | #[test] |
| 339 | fn an_actor_waiting_on_a_timer_is_no_deadlock() { |
| 340 | for threads in THREADS { |
| 341 | assert_eq!( |
| 342 | run_root(threads, sleeps_while_another_waits), |
| 343 | Exit::Normal, |
| 344 | "{threads} threads" |
| 345 | ); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static VTIME_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 350 | |
| 351 | /// Waits on the socket in `VTIME_TOKEN` until it is stopped: while it |
| 352 | /// waits, the root's timer must fire by a clock jump (LOUISE_VIRTUAL_TIME). |
| 353 | unsafe extern "C" fn waits_on_the_vtime_socket(ctx: *mut Ctx) -> Value { |
| 354 | let token = Token(VTIME_TOKEN.load(Ordering::SeqCst)); |
| 355 | // SAFETY: an actor's entry. |
| 356 | unsafe { |
| 357 | let seen = this(ctx).1.io_events(); |
| 358 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 359 | } |
| 360 | UNIT |
| 361 | } |
| 362 | |
| 363 | /// With LOUISE_VIRTUAL_TIME, opening a socket does not switch the clock |
| 364 | /// to real time, and a timer fires by a clock jump while another actor is |
| 365 | /// parked on a socket that never becomes ready (ADR 0055). |
| 366 | unsafe extern "C" fn sleeps_while_another_waits_on_a_socket(ctx: *mut Ctx) -> Value { |
| 367 | // SAFETY: the root actor's entry. |
| 368 | unsafe { |
| 369 | let (_reader, _writer, token) = socket_pair(ctx); |
| 370 | VTIME_TOKEN.store(token.0, Ordering::SeqCst); |
| 371 | let waiter = spawn(ctx, waits_on_the_vtime_socket); |
| 372 | yield_until(ctx, "the waiter to park", |s| { |
| 373 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 374 | }); |
| 375 | let (a, sched) = this(ctx); |
| 376 | assert!(!sched.real_time(), "a socket did not switch to real time"); |
| 377 | let deadline = sched.clock() + 60_000; |
| 378 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 379 | sched.add_timer(deadline, a.pid, wait); |
| 380 | let woken = park(ctx); |
| 381 | assert_eq!(woken, UNIT); |
| 382 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 383 | // Virtual time: the clock jumped to the deadline. |
| 384 | assert_eq!(sched.clock(), deadline); |
| 385 | assert!(!sched.real_time(), "the clock stayed virtual"); |
| 386 | super::exit_actor(sched, waiter, Exit::Killed); |
| 387 | yield_until(ctx, "the waiter to finish", |s| s.actors.all().len() == 1); |
| 388 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 389 | } |
| 390 | UNIT |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn virtual_time_jumps_while_an_actor_waits_on_a_socket() { |
| 395 | for threads in THREADS { |
| 396 | assert_eq!( |
| 397 | run_root_clock(threads, true, sleeps_while_another_waits_on_a_socket), |
| 398 | Exit::Normal, |
| 399 | "{threads} threads" |
| 400 | ); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /// Without LOUISE_VIRTUAL_TIME, the first socket switches the clock to |
| 405 | /// real time (ADR 0004, Revision 3). |
| 406 | unsafe extern "C" fn opens_a_socket(ctx: *mut Ctx) -> Value { |
| 407 | // SAFETY: the root actor's entry. |
| 408 | unsafe { |
| 409 | let (_reader, _writer, _token) = socket_pair(ctx); |
| 410 | assert!( |
| 411 | this(ctx).1.real_time(), |
| 412 | "the first socket switches to real time" |
| 413 | ); |
| 414 | } |
| 415 | UNIT |
| 416 | } |
| 417 | |
| 418 | #[test] |
| 419 | fn a_socket_switches_to_real_time_by_default() { |
| 420 | for threads in THREADS { |
| 421 | assert_eq!( |
| 422 | run_root(threads, opens_a_socket), |
| 423 | Exit::Normal, |
| 424 | "{threads} threads" |
| 425 | ); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /// Write a byte to `writer` from a thread outside the scheduler, once |
| 430 | /// every actor waits, one of them on a socket, and a scheduler thread |
| 431 | /// idles: that thread must wait on the reactor, not report a deadlock. |
| 432 | /// |
| 433 | /// # Safety |
| 434 | /// `sched` must outlive the thread: join it before the actor that |
| 435 | /// started it returns. |
| 436 | unsafe fn write_when_idle(sched: &Shared, mut writer: UnixStream) -> JoinHandle<()> { |
| 437 | let address = std::ptr::from_ref(sched) as usize; |
| 438 | std::thread::spawn(move || { |
| 439 | // SAFETY: caller guarantee. |
| 440 | let sched = unsafe { &*(address as *const Shared) }; |
| 441 | while !(sched.idling.load(Ordering::SeqCst) && sched.io_waiting.load(Ordering::SeqCst) == 1) |
| 442 | { |
| 443 | std::thread::sleep(Duration::from_micros(100)); |
| 444 | } |
| 445 | writer.write_all(b"x").expect("written"); |
| 446 | }) |
| 447 | } |
| 448 | |
| 449 | /// Waits on a socket while another actor waits forever, until a thread |
| 450 | /// outside the scheduler writes to it. |
| 451 | unsafe extern "C" fn reads_while_another_waits(ctx: *mut Ctx) -> Value { |
| 452 | // SAFETY: the root actor's entry; it joins the writer before it |
| 453 | // returns. |
| 454 | unsafe { |
| 455 | spawn(ctx, waits_forever); |
| 456 | let (mut reader, writer, token) = socket_pair(ctx); |
| 457 | let (_, sched) = this(ctx); |
| 458 | let outside = write_when_idle(sched, writer); |
| 459 | let mut byte = [0]; |
| 460 | loop { |
| 461 | let seen = sched.io_events(); |
| 462 | match reader.read(&mut byte) { |
| 463 | Ok(n) => { |
| 464 | assert_eq!(n, 1); |
| 465 | break; |
| 466 | } |
| 467 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 468 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 469 | } |
| 470 | Err(e) => panic!("read: {e}"), |
| 471 | } |
| 472 | } |
| 473 | outside.join().expect("the writer"); |
| 474 | } |
| 475 | UNIT |
| 476 | } |
| 477 | |
| 478 | #[test] |
| 479 | fn an_actor_waiting_on_a_socket_is_no_deadlock() { |
| 480 | for threads in THREADS { |
| 481 | assert_eq!( |
| 482 | run_root(threads, reads_while_another_waits), |
| 483 | Exit::Normal, |
| 484 | "{threads} threads" |
| 485 | ); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | static SHARED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 490 | static WOKEN: AtomicUsize = AtomicUsize::new(0); |
| 491 | |
| 492 | /// Waits on the socket in `SHARED_TOKEN`, then counts itself woken. |
| 493 | unsafe extern "C" fn waits_on_the_shared_socket(ctx: *mut Ctx) -> Value { |
| 494 | let token = Token(SHARED_TOKEN.load(Ordering::SeqCst)); |
| 495 | // SAFETY: an actor's entry. |
| 496 | unsafe { |
| 497 | let seen = this(ctx).1.io_events(); |
| 498 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 499 | } |
| 500 | WOKEN.fetch_add(1, Ordering::SeqCst); |
| 501 | UNIT |
| 502 | } |
| 503 | |
| 504 | /// Three actors wait on one socket; one event wakes them all. |
| 505 | unsafe extern "C" fn wakes_every_waiter(ctx: *mut Ctx) -> Value { |
| 506 | // SAFETY: the root actor's entry. |
| 507 | unsafe { |
| 508 | WOKEN.store(0, Ordering::SeqCst); |
| 509 | let (reader, mut writer, token) = socket_pair(ctx); |
| 510 | SHARED_TOKEN.store(token.0, Ordering::SeqCst); |
| 511 | let mut pids: Vec<i64> = (0..3) |
| 512 | .map(|_| spawn(ctx, waits_on_the_shared_socket)) |
| 513 | .collect(); |
| 514 | yield_until(ctx, "three waiters", |s| { |
| 515 | s.io_waiting.load(Ordering::SeqCst) == 3 |
| 516 | }); |
| 517 | let (_, sched) = this(ctx); |
| 518 | let mut waiting = sched.control().io_waiters[&token].clone(); |
| 519 | waiting.sort_unstable(); |
| 520 | pids.sort_unstable(); |
| 521 | assert_eq!(waiting, pids, "every waiter is kept"); |
| 522 | assert_eq!(WOKEN.load(Ordering::SeqCst), 0); |
| 523 | writer.write_all(b"x").expect("written"); |
| 524 | yield_until(ctx, "three woken", |_| WOKEN.load(Ordering::SeqCst) == 3); |
| 525 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 526 | drop(reader); |
| 527 | } |
| 528 | UNIT |
| 529 | } |
| 530 | |
| 531 | #[test] |
| 532 | fn every_actor_waiting_on_a_socket_is_woken_by_its_event() { |
| 533 | for threads in THREADS { |
| 534 | assert_eq!( |
| 535 | run_root(threads, wakes_every_waiter), |
| 536 | Exit::Normal, |
| 537 | "{threads} threads" |
| 538 | ); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static KEEPER_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 543 | |
| 544 | /// Waits on the socket in `KEEPER_TOKEN` until it is stopped: while it |
| 545 | /// waits, scheduler threads poll the reactor between slices. |
| 546 | unsafe extern "C" fn waits_until_stopped(ctx: *mut Ctx) -> Value { |
| 547 | let token = Token(KEEPER_TOKEN.load(Ordering::SeqCst)); |
| 548 | // SAFETY: an actor's entry. |
| 549 | unsafe { |
| 550 | let seen = this(ctx).1.io_events(); |
| 551 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 552 | } |
| 553 | UNIT |
| 554 | } |
| 555 | |
| 556 | /// An event that comes between an operation's `WouldBlock` and its |
| 557 | /// parking: the parking is refused, and the retry finds the data. |
| 558 | unsafe extern "C" fn retries_after_a_missed_event(ctx: *mut Ctx) -> Value { |
| 559 | // SAFETY: the root actor's entry. |
| 560 | unsafe { |
| 561 | let (a, sched) = this(ctx); |
| 562 | let (_keeper_reader, _keeper_writer, keeper_token) = socket_pair(ctx); |
| 563 | KEEPER_TOKEN.store(keeper_token.0, Ordering::SeqCst); |
| 564 | let keeper = spawn(ctx, waits_until_stopped); |
| 565 | yield_until(ctx, "the keeper to park", |s| { |
| 566 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 567 | }); |
| 568 | |
| 569 | let (mut reader, mut writer, token) = socket_pair(ctx); |
| 570 | let mut byte = [0]; |
| 571 | let seen = sched.io_events(); |
| 572 | let blocked = reader.read(&mut byte).expect_err("nothing to read yet"); |
| 573 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 574 | // The event comes now, and another thread's poll takes it: nobody |
| 575 | // waits on this socket, but the count moves. |
| 576 | writer.write_all(b"x").expect("written"); |
| 577 | yield_until(ctx, "the event to be counted", |s| s.io_events() != seen); |
| 578 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 579 | assert!(!sched.io_wait(token, a.pid, seen), "parking refused"); |
| 580 | a.with_proc(|p| p.wait = Wait::Ready); |
| 581 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 582 | assert_eq!( |
| 583 | sched.io_waiting.load(Ordering::SeqCst), |
| 584 | 1, |
| 585 | "the keeper only" |
| 586 | ); |
| 587 | assert_eq!(reader.read(&mut byte).expect("the retry reads"), 1); |
| 588 | |
| 589 | // With no event since, the same parking is accepted. |
| 590 | let seen = sched.io_events(); |
| 591 | let blocked = reader.read(&mut byte).expect_err("nothing to read"); |
| 592 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 593 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 594 | assert!(sched.io_wait(token, a.pid, seen), "parked"); |
| 595 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 2); |
| 596 | sched.io_unwait(token, a.pid); |
| 597 | a.with_proc(|p| p.wait = Wait::Ready); |
| 598 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 1); |
| 599 | |
| 600 | // A stopped waiter's wait is forgotten. |
| 601 | super::exit_actor(sched, keeper, Exit::Killed); |
| 602 | yield_until(ctx, "the keeper to finish", |s| s.actors.all().len() == 1); |
| 603 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 604 | assert!(sched.control().io_waiters.is_empty()); |
| 605 | } |
| 606 | UNIT |
| 607 | } |
| 608 | |
| 609 | #[test] |
| 610 | fn an_event_before_parking_makes_the_operation_retry() { |
| 611 | for threads in THREADS { |
| 612 | assert_eq!( |
| 613 | run_root(threads, retries_after_a_missed_event), |
| 614 | Exit::Normal, |
| 615 | "{threads} threads" |
| 616 | ); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | const TEST_HANDLER_STACK: usize = 64 << 10; |
| 621 | |
| 622 | /// Write a marker near the unused top so warm retention and `release` are |
| 623 | /// observable. `top - 16` is aligned and above the guard page. |
| 624 | fn mark_stack(stack: &Stack, marker: usize) -> usize { |
| 625 | let address = stack.top() - 16; |
| 626 | // SAFETY: an unused stack owns this aligned, mapped word. |
| 627 | unsafe { (address as *mut usize).write_volatile(marker) }; |
| 628 | address |
| 629 | } |
| 630 | |
| 631 | fn stack_marker(address: usize) -> usize { |
| 632 | // SAFETY: the caller keeps the marked stack alive. |
| 633 | unsafe { (address as *const usize).read_volatile() } |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn handler_stack_pool_miss_hit_and_size() { |
| 638 | let _turn = test_turn(); |
| 639 | let pool = HandlerStacks::new(2, 4); |
| 640 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "empty miss"); |
| 641 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 642 | assert_eq!(stack.size(), TEST_HANDLER_STACK); |
| 643 | let top = stack.top(); |
| 644 | pool.pool(stack, TEST_HANDLER_STACK); |
| 645 | let reused = pool.take(TEST_HANDLER_STACK).expect("warm hit"); |
| 646 | assert_eq!(reused.top(), top, "the same mapping was reused"); |
| 647 | assert_eq!(reused.size(), TEST_HANDLER_STACK); |
| 648 | pool.pool(reused, TEST_HANDLER_STACK); |
| 649 | // Ordinary pool drop unmaps the retained stack. |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn handler_stack_pool_releases_only_past_warm_limit() { |
| 654 | let _turn = test_turn(); |
| 655 | let warm = HandlerStacks::new(1, 2); |
| 656 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 657 | let marked = mark_stack(&stack, 0x5a17); |
| 658 | warm.pool(stack, TEST_HANDLER_STACK); |
| 659 | let stack = warm.take(TEST_HANDLER_STACK).expect("retained"); |
| 660 | assert_eq!(stack_marker(marked), 0x5a17, "warm pages retained"); |
| 661 | |
| 662 | // Keep one warm entry, then pool another: the latter is cold-released |
| 663 | // and reads as zero when taken again, without any large mapping stress. |
| 664 | warm.pool(stack, TEST_HANDLER_STACK); |
| 665 | let cold = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 666 | let cleared = mark_stack(&cold, 0x5a17); |
| 667 | warm.pool(cold, TEST_HANDLER_STACK); |
| 668 | let cold = warm |
| 669 | .take(TEST_HANDLER_STACK) |
| 670 | .expect("cold retained mapping"); |
| 671 | assert_eq!( |
| 672 | cold.size(), |
| 673 | TEST_HANDLER_STACK, |
| 674 | "cold mapping keeps its size" |
| 675 | ); |
| 676 | assert_eq!(stack_marker(cleared), 0, "cold pages released"); |
| 677 | } |
| 678 | |
| 679 | #[test] |
| 680 | fn handler_stack_pool_releases_over_capacity_without_double_insertion() { |
| 681 | let _turn = test_turn(); |
| 682 | let pool = HandlerStacks::new(0, 2); |
| 683 | let first = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 684 | let second = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 685 | let surplus = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 686 | let first_top = first.top(); |
| 687 | let second_top = second.top(); |
| 688 | pool.pool(first, TEST_HANDLER_STACK); |
| 689 | pool.pool(second, TEST_HANDLER_STACK); |
| 690 | // Over capacity: dropped/unmapped here, outside the pool mutex. |
| 691 | pool.pool(surplus, TEST_HANDLER_STACK); |
| 692 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), second_top); |
| 693 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), first_top); |
| 694 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "no duplicate"); |
| 695 | let stats = pool.stats(); |
| 696 | assert_eq!((stats.hits, stats.misses), (2, 1)); |
| 697 | assert_eq!(stats.unmaps, 1); |
| 698 | } |
| 699 | |
| 700 | #[test] |
| 701 | fn handler_stack_pool_production_cap_is_stable_across_refills() { |
| 702 | let _turn = test_turn(); |
| 703 | // Small mappings exercise the real 16-warm/64-cap policy without making |
| 704 | // this unit test reserve the production 72 × 18.131 MiB extent. |
| 705 | let pool = HandlerStacks::new(WARM_HANDLER_STACKS, HANDLER_STACK_POOL); |
| 706 | let mut live = Vec::new(); |
| 707 | for round in 0..4 { |
| 708 | while let Some(stack) = pool.take(TEST_HANDLER_STACK) { |
| 709 | live.push(stack); |
| 710 | } |
| 711 | while live.len() < 72 { |
| 712 | live.push(Stack::new(TEST_HANDLER_STACK).unwrap()); |
| 713 | } |
| 714 | for stack in live.drain(..) { |
| 715 | pool.pool(stack, TEST_HANDLER_STACK); |
| 716 | } |
| 717 | assert_eq!(pool.stacks().len(), HANDLER_STACK_POOL); |
| 718 | let stats = pool.stats(); |
| 719 | assert_eq!(stats.warm_inserts, WARM_HANDLER_STACKS * (round + 1)); |
| 720 | assert_eq!( |
| 721 | stats.cold_releases, |
| 722 | (HANDLER_STACK_POOL - WARM_HANDLER_STACKS) * (round + 1) |
| 723 | ); |
| 724 | assert_eq!(stats.unmaps, (72 - HANDLER_STACK_POOL) * (round + 1)); |
| 725 | } |
| 726 | let stats = pool.stats(); |
| 727 | assert_eq!(stats.hits, HANDLER_STACK_POOL * 3); |
| 728 | assert_eq!(stats.misses, 4); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn ordinary_handler_pool_drop_unmaps_retained_stack() { |
| 733 | // Shared's ordinary (non-PROGRAM_MODE) teardown drops this same vector. |
| 734 | // Probe by address, without dereferencing the dropped stack: an exclusive |
| 735 | // no-replace mapping can succeed only if the old extent was unmapped. |
| 736 | let _turn = test_turn(); |
| 737 | let pool = HandlerStacks::new(1, 1); |
| 738 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 739 | let (base, len) = stack.map_extent(); |
| 740 | pool.pool(stack, TEST_HANDLER_STACK); |
| 741 | drop(pool); |
| 742 | // SAFETY: `base..base+len` was just released; NOREPLACE refuses rather |
| 743 | // than replacing anything another thread may have mapped there. The new |
| 744 | // PROT_NONE mapping is this test's own and is unmapped exactly once. |
| 745 | unsafe { |
| 746 | let replacement = libc::mmap( |
| 747 | base.cast(), |
| 748 | len, |
| 749 | libc::PROT_NONE, |
| 750 | libc::MAP_PRIVATE |
| 751 | | libc::MAP_ANONYMOUS |
| 752 | | libc::MAP_NORESERVE |
| 753 | | libc::MAP_FIXED_NOREPLACE, |
| 754 | -1, |
| 755 | 0, |
| 756 | ); |
| 757 | assert_ne!( |
| 758 | replacement, |
| 759 | libc::MAP_FAILED, |
| 760 | "retained extent is still mapped: {}", |
| 761 | std::io::Error::last_os_error() |
| 762 | ); |
| 763 | assert_eq!(libc::munmap(replacement, len), 0); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // ---- fs owned adapters: a deterministic kill while their actor parks ------ |
| 768 | |
| 769 | /// One scenario's synchronization and lifecycle evidence. Each run owns a |
| 770 | /// fresh value, whose address is kept by the caller until scheduler and pool |
| 771 | /// work finish; there is no test-global state or real file operation. |
| 772 | #[derive(Default)] |
| 773 | struct FsAdapterControl { |
| 774 | worker_started: AtomicBool, |
| 775 | release_worker: Mutex<bool>, |
| 776 | release: Condvar, |
| 777 | killed_while_blocked: AtomicBool, |
| 778 | killer_done: AtomicBool, |
| 779 | worker_done: AtomicBool, |
| 780 | worker_on_io_pool: AtomicBool, |
| 781 | argument_finalizers: AtomicUsize, |
| 782 | result_finalizers: AtomicUsize, |
| 783 | unwound: AtomicUsize, |
| 784 | } |
| 785 | |
| 786 | unsafe extern "C" fn fs_argument_finalizer(ptr: *mut std::ffi::c_void) { |
| 787 | // SAFETY: every test resource carries the still-live scenario address. |
| 788 | let control = unsafe { &*(ptr as *const FsAdapterControl) }; |
| 789 | control.argument_finalizers.fetch_add(1, Ordering::SeqCst); |
| 790 | } |
| 791 | |
| 792 | unsafe extern "C" fn fs_result_finalizer(ptr: *mut std::ffi::c_void) { |
| 793 | // SAFETY: as `fs_argument_finalizer`. |
| 794 | let control = unsafe { &*(ptr as *const FsAdapterControl) }; |
| 795 | control.result_finalizers.fetch_add(1, Ordering::SeqCst); |
| 796 | } |
| 797 | |
| 798 | /// A closure with at most two immediate fields, for the test root and killer. |
| 799 | /// Its parameters are `(self, proc root, unit, ...)` because test scheduler |
| 800 | /// programs use the default one-root actor layout. |
| 801 | unsafe fn fs_test_closure(ctx: *mut Ctx, code: crate::process::Code, env: &[Value]) -> Value { |
| 802 | assert!(env.len() <= 2); |
| 803 | // SAFETY: both entries have the runtime closure code-pointer layout; |
| 804 | // `immortal_cap` uses the same type erasure for runtime root clauses. |
| 805 | let closure = unsafe { |
| 806 | crate::values::tl_closure_alloc( |
| 807 | ctx, |
| 808 | std::mem::transmute::<crate::process::Code, crate::values::ClosureFn>(code), |
| 809 | std::mem::transmute::<crate::process::Code, crate::values::ClosureFn>( |
| 810 | crate::values::tl_borrowed0, |
| 811 | ), |
| 812 | env.len() as u8, |
| 813 | ) |
| 814 | }; |
| 815 | // SAFETY: the closure was allocated with exactly `env.len()` fields. |
| 816 | unsafe { |
| 817 | Block::fields_ptr(closure as *mut Block).copy_from_nonoverlapping(env.as_ptr(), env.len()); |
| 818 | } |
| 819 | closure |
| 820 | } |
| 821 | |
| 822 | /// A controlled I/O-pool callback. The observed arguments are resources with |
| 823 | /// finalizers, so freeing either an input or the result twice is visible. |
| 824 | unsafe extern "C" fn controlled_fs_thunk(ctx: *mut Ctx, args: *const Value) -> Value { |
| 825 | // SAFETY: the adapter passes one/two live borrowed test resources. |
| 826 | let ptr = unsafe { crate::resource::tl_resource_get(args.read()) }; |
| 827 | let control = ptr as *mut FsAdapterControl; |
| 828 | // SAFETY: the scenario owns `control` until this worker is complete. |
| 829 | let control = unsafe { &*control }; |
| 830 | control.worker_on_io_pool.store( |
| 831 | std::thread::current() |
| 832 | .name() |
| 833 | .is_some_and(|name| name.starts_with("louise-blocking-io-")), |
| 834 | Ordering::SeqCst, |
| 835 | ); |
| 836 | assert!(!control.worker_started.swap(true, Ordering::SeqCst)); |
| 837 | |
| 838 | // Hold the actor in Wait::Blocking until the killer has completed its |
| 839 | // logical exit. No operation, disk access or unbounded work runs here. |
| 840 | let mut released = control.release_worker.lock().expect("release lock"); |
| 841 | while !*released { |
| 842 | released = control.release.wait(released).expect("release wait"); |
| 843 | } |
| 844 | drop(released); |
| 845 | |
| 846 | // Return a fresh owned result even though the actor is already dead. |
| 847 | // Scheduler wake-up/park must dispose of it once while unwinding. |
| 848 | let result = crate::resource::tl_resource_new( |
| 849 | ctx, |
| 850 | std::ptr::from_ref(control) as *mut std::ffi::c_void, |
| 851 | Some(fs_result_finalizer), |
| 852 | true, |
| 853 | ); |
| 854 | control.worker_done.store(true, Ordering::SeqCst); |
| 855 | result |
| 856 | } |
| 857 | |
| 858 | /// The second actor observes the controlled worker start, kills its parent |
| 859 | /// and only then releases the worker. On either one or four scheduler threads |
| 860 | /// the parent cannot resume before both checkpoints have happened. |
| 861 | unsafe extern "C" fn fs_adapter_killer( |
| 862 | ctx: *mut Ctx, |
| 863 | this_: Value, |
| 864 | proc: Value, |
| 865 | _unit: Value, |
| 866 | _: Value, |
| 867 | _: Value, |
| 868 | _: Value, |
| 869 | ) -> Value { |
| 870 | // SAFETY: closure of `fs_test_closure` and the program actor convention. |
| 871 | unsafe { |
| 872 | let fields = Block::fields_ptr(this_ as *mut Block); |
| 873 | let control = int_get(fields.read()) as usize as *const FsAdapterControl; |
| 874 | let target = int_get(fields.add(1).read()); |
| 875 | drop_value(ctx, this_); |
| 876 | drop_value(ctx, proc); |
| 877 | let limit = Instant::now() + PATIENCE; |
| 878 | while !(*control).worker_started.load(Ordering::SeqCst) { |
| 879 | assert!(Instant::now() < limit, "the fs worker did not start"); |
| 880 | preempt_root(ctx); |
| 881 | } |
| 882 | let (_, sched) = this(ctx); |
| 883 | super::exit_actor(sched, target, Exit::Killed); |
| 884 | (*control) |
| 885 | .killed_while_blocked |
| 886 | .store(true, Ordering::SeqCst); |
| 887 | (*control).killer_done.store(true, Ordering::SeqCst); |
| 888 | { |
| 889 | let mut released = (*control).release_worker.lock().expect("release lock"); |
| 890 | *released = true; |
| 891 | (*control).release.notify_one(); |
| 892 | } |
| 893 | UNIT |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /// Parent/blocked actor. It never reads the inputs after handing them to the |
| 898 | /// adapter, nor reads the worker's result after park disposes of it. |
| 899 | unsafe extern "C" fn killed_in_fs_adapter( |
| 900 | ctx: *mut Ctx, |
| 901 | this_: Value, |
| 902 | proc: Value, |
| 903 | _unit: Value, |
| 904 | _: Value, |
| 905 | _: Value, |
| 906 | _: Value, |
| 907 | ) -> Value { |
| 908 | // SAFETY: as `fs_adapter_killer`. |
| 909 | unsafe { |
| 910 | let fields = Block::fields_ptr(this_ as *mut Block); |
| 911 | let control = int_get(fields.read()) as usize as *mut FsAdapterControl; |
| 912 | let arity = int_get(fields.add(1).read()); |
| 913 | drop_value(ctx, this_); |
| 914 | drop_value(ctx, proc); |
| 915 | assert!(arity == 1 || arity == 2); |
| 916 | let mut args = [UNIT; 2]; |
| 917 | for arg in args.iter_mut().take(arity as usize) { |
| 918 | *arg = crate::resource::tl_resource_new( |
| 919 | ctx, |
| 920 | control as *mut std::ffi::c_void, |
| 921 | Some(fs_argument_finalizer), |
| 922 | true, |
| 923 | ); |
| 924 | } |
| 925 | let (me, _) = this(ctx); |
| 926 | let killer = fs_test_closure( |
| 927 | ctx, |
| 928 | fs_adapter_killer, |
| 929 | &[int(control as usize as i64), int(me.pid)], |
| 930 | ); |
| 931 | crate::sched::spawn_program(ctx, killer, Tie::None); |
| 932 | let out = if arity == 1 { |
| 933 | crate::fs::test_blocking1(ctx, args[0], controlled_fs_thunk) |
| 934 | } else { |
| 935 | crate::fs::test_blocking2(ctx, args[0], args[1], controlled_fs_thunk) |
| 936 | }; |
| 937 | assert_eq!(out, UNIT, "a killed adapter returns unit"); |
| 938 | assert_eq!( |
| 939 | (*ctx).yielding, |
| 940 | YIELD_KILL, |
| 941 | "the completed worker resumes one unwinding" |
| 942 | ); |
| 943 | (*control).unwound.fetch_add(1, Ordering::SeqCst); |
| 944 | assert_eq!( |
| 945 | (*control).argument_finalizers.load(Ordering::SeqCst), |
| 946 | arity as usize |
| 947 | ); |
| 948 | assert_eq!((*control).result_finalizers.load(Ordering::SeqCst), 1); |
| 949 | assert!((*control).killed_while_blocked.load(Ordering::SeqCst)); |
| 950 | assert!((*control).worker_done.load(Ordering::SeqCst)); |
| 951 | assert!((*control).worker_on_io_pool.load(Ordering::SeqCst)); |
| 952 | UNIT |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /// Run one fresh scenario with its control address in the root closure. |
| 957 | fn run_fs_adapter_kill(threads: usize, arity: i64) { |
| 958 | let _turn = test_turn(); |
| 959 | let control = FsAdapterControl::default(); |
| 960 | // The root actor owns a closure holding only this address and arity. |
| 961 | // `run` does not return until all finalizers and the one unwind finished. |
| 962 | // SAFETY: `control` stays allocated at this stack address until `run` |
| 963 | // returns; it is not moved while the root/killer closures, pool thunk and |
| 964 | // argument/result finalizers can hold its borrowed address. No use remains |
| 965 | // after that synchronous join. `fs_test_closure` is given the matching |
| 966 | // owned entry and exactly two immediate fields, both initialized here. |
| 967 | let body = unsafe { |
| 968 | fs_test_closure( |
| 969 | std::ptr::null_mut(), |
| 970 | killed_in_fs_adapter, |
| 971 | &[ |
| 972 | int(std::ptr::from_ref(&control) as usize as i64), |
| 973 | int(arity), |
| 974 | ], |
| 975 | ) |
| 976 | }; |
| 977 | let sched = Shared::new(threads); |
| 978 | let (root, _) = spawn_actor( |
| 979 | &sched, |
| 980 | Body::Program(body), |
| 981 | root_stack_size(), |
| 982 | None, |
| 983 | Tie::None, |
| 984 | ); |
| 985 | assert_eq!( |
| 986 | run(&sched, root, threads), |
| 987 | Exit::Killed, |
| 988 | "{threads} threads" |
| 989 | ); |
| 990 | assert!(control.killer_done.load(Ordering::SeqCst)); |
| 991 | crate::console::flush(); |
| 992 | assert_eq!(control.unwound.load(Ordering::SeqCst), 1); |
| 993 | assert!(sched.control().io_waiters.is_empty()); |
| 994 | assert!(sched.actors.all().is_empty(), "actors left"); |
| 995 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 996 | } |
| 997 | |
| 998 | #[test] |
| 999 | fn fs_owned_adapters_destroy_args_and_result_when_killed_while_blocked() { |
| 1000 | for threads in THREADS { |
| 1001 | // Both exact helper bodies: one resource input through blocking1 and |
| 1002 | // two independently owned inputs through blocking2. Four tiny |
| 1003 | // scenarios use no real filesystem objects or large allocation. |
| 1004 | run_fs_adapter_kill(threads, 1); |
| 1005 | run_fs_adapter_kill(threads, 2); |
| 1006 | } |
| 1007 | } |