
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#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | //! Remote pids without a network (ADR 0072): `proc` operations on a |
| 2 | //! remote pid reach a fake transport's hooks, or act as on a dead pid |
| 3 | //! without any; what a transport delivers from its own thread reaches |
| 4 | //! local actors and their ties. |
| 5 | //! |
| 6 | //! The hooks are global: each test installs them in its root actor, under |
| 7 | //! the scheduler tests' turn, and removes them before returning. The fake |
| 8 | //! hooks make the local halves of ties as the transport's do, listing them |
| 9 | //! in a fake node's [`Dist`], fresh for each test. |
| 10 | //! |
| 11 | //! Back-pressure: a fake peer's queue of [`CAPACITY`] messages, drained by |
| 12 | //! the root actor, parks senders through `dist::send_parking`. |
| 13 | |
| 14 | use std::sync::atomic::{AtomicBool, AtomicI64, Ordering}; |
| 15 | use std::sync::{Arc, Mutex}; |
| 16 | |
| 17 | use crate::abi::{Block, Ctx, UNIT, Value, YIELD_NONE, YIELD_PANIC, int, int_get, is_ptr}; |
| 18 | use crate::dist::{ |
| 19 | Dist, Hooks, Signal, Try, Waiter, inbound, install, outbound, remote, send_parking, uninstall, |
| 20 | unsendable, wake_sender, |
| 21 | }; |
| 22 | use crate::heap::{drop_value, dup_value}; |
| 23 | use crate::process::{PROC_OPS, RootCap}; |
| 24 | |
| 25 | use super::super::{Exit, Shared, exit_actor, this}; |
| 26 | use super::{THREADS, run_root, spawn, wait_forever, waits_forever, yield_until}; |
| 27 | |
| 28 | /// What the fake transport was asked to do. |
| 29 | #[derive(Clone, Debug, PartialEq)] |
| 30 | enum Call { |
| 31 | /// (owner, tag, wire, the message, an `Int`). |
| 32 | Send(i64, i64, i64, i64), |
| 33 | Link(i64, i64), |
| 34 | /// (watcher, tag, watched). |
| 35 | Monitor(i64, i64, i64), |
| 36 | /// (watcher, tag, watched). |
| 37 | Demonitor(i64, i64, i64), |
| 38 | /// (unlinker, unlinked, id). |
| 39 | Unlink(i64, i64, u64), |
| 40 | /// The acknowledgement of a remote pid's unlink: (local, remote, id). |
| 41 | UnlinkAck(i64, i64, u64), |
| 42 | Signal(i64, i64, Signal), |
| 43 | /// (watched, watcher, tag, reason). |
| 44 | Down(i64, i64, i64, Exit), |
| 45 | /// A monitored spawn's answer, for this new actor. |
| 46 | Answered(i64), |
| 47 | } |
| 48 | |
| 49 | static CALLS: Mutex<Vec<Call>> = Mutex::new(Vec::new()); |
| 50 | |
| 51 | fn called(call: Call) { |
| 52 | CALLS.lock().expect("calls").push(call); |
| 53 | } |
| 54 | |
| 55 | fn calls() -> Vec<Call> { |
| 56 | CALLS.lock().expect("calls").clone() |
| 57 | } |
| 58 | |
| 59 | /// The fake node's ties: what a started node keeps (`node.rs`). |
| 60 | static DIST: Mutex<Option<Arc<Dist>>> = Mutex::new(None); |
| 61 | |
| 62 | fn dist() -> Arc<Dist> { |
| 63 | let mut dist = DIST.lock().expect("the ties"); |
| 64 | Arc::clone(dist.get_or_insert_with(Arc::default)) |
| 65 | } |
| 66 | |
| 67 | static FAKE: Hooks = Hooks { |
| 68 | send: |_, owner, tag, wire, message| { |
| 69 | called(Call::Send(owner, tag, wire, int_get(message))); |
| 70 | Ok(()) |
| 71 | }, |
| 72 | link: |sched, me, other| { |
| 73 | let mut meanwhile = None; |
| 74 | outbound::link(&dist(), me, other, || { |
| 75 | if KILL_WHILE_LINKING.swap(false, Ordering::SeqCst) { |
| 76 | meanwhile = Some(kill_meanwhile(sched, me.pid)); |
| 77 | } |
| 78 | if LOSE_NODE_WHILE_LINKING.swap(false, Ordering::SeqCst) { |
| 79 | meanwhile = Some(lose_node_meanwhile(sched, me.pid, other)); |
| 80 | } |
| 81 | called(Call::Link(me.pid, other)); |
| 82 | true |
| 83 | }); |
| 84 | if let Some(meanwhile) = meanwhile { |
| 85 | meanwhile.join().expect("the meanwhile"); |
| 86 | } |
| 87 | }, |
| 88 | monitor: |sched, me, watched, tag| { |
| 89 | if outbound::monitor(sched, &dist(), me, watched, tag) { |
| 90 | called(Call::Monitor(me.pid, tag, watched)); |
| 91 | } |
| 92 | true |
| 93 | }, |
| 94 | demonitor: |me, watched, tag| { |
| 95 | if outbound::demonitor(me, watched, tag) { |
| 96 | called(Call::Demonitor(me.pid, tag, watched)); |
| 97 | } |
| 98 | }, |
| 99 | unlink: |me, other| { |
| 100 | let id = dist().unlink_id(); |
| 101 | outbound::unlink(me, other, id, || { |
| 102 | if UNLINK_NOT_UP.swap(false, Ordering::SeqCst) { |
| 103 | return false; |
| 104 | } |
| 105 | called(Call::Unlink(me.pid, other, id)); |
| 106 | true |
| 107 | }); |
| 108 | }, |
| 109 | signal: |from, to, signal| called(Call::Signal(from, to, signal.clone())), |
| 110 | down: |watched, watcher, tag, reason| called(Call::Down(watched, watcher, tag, reason.clone())), |
| 111 | exit: |pid, nodes| dist().untie(pid, nodes), |
| 112 | }; |
| 113 | |
| 114 | /// The next link the fake transport sends, its actor is killed from |
| 115 | /// another thread while it sends it. |
| 116 | static KILL_WHILE_LINKING: AtomicBool = AtomicBool::new(false); |
| 117 | |
| 118 | /// The next link the fake transport sends, its node is lost as the frame |
| 119 | /// is queued, under the linking actor's lock. |
| 120 | static LOSE_NODE_WHILE_LINKING: AtomicBool = AtomicBool::new(false); |
| 121 | |
| 122 | /// The next unlink the fake transport sends finds its node not up. |
| 123 | static UNLINK_NOT_UP: AtomicBool = AtomicBool::new(false); |
| 124 | |
| 125 | /// Kill `pid` from a thread that is no scheduler's, after a pause long |
| 126 | /// enough for it to wait for `pid`'s lock if it is held: what the fake link |
| 127 | /// does while it queues its frame. |
| 128 | fn kill_meanwhile(sched: &Shared, pid: i64) -> std::thread::JoinHandle<()> { |
| 129 | let address = std::ptr::from_ref(sched) as usize; |
| 130 | let killer = std::thread::spawn(move || { |
| 131 | // SAFETY: the scheduler outlives the thread, joined by the hook. |
| 132 | exit_actor(unsafe { &*(address as *const Shared) }, pid, Exit::Killed); |
| 133 | }); |
| 134 | std::thread::sleep(std::time::Duration::from_millis(20)); |
| 135 | killer |
| 136 | } |
| 137 | |
| 138 | /// Lose `other`'s node from a thread that is no scheduler's, as the frame |
| 139 | /// linking `pid` to it is queued: return once the loss's fan-out has taken |
| 140 | /// the actors listed under the node (if `pid` is listed, it then waits for |
| 141 | /// `pid`'s lock), or has ended. |
| 142 | fn lose_node_meanwhile(sched: &Shared, pid: i64, other: i64) -> std::thread::JoinHandle<()> { |
| 143 | let node = crate::dist::parts(other).0; |
| 144 | let listed = dist().tied_to(node).contains(&pid); |
| 145 | let address = std::ptr::from_ref(sched) as usize; |
| 146 | let loser = std::thread::spawn(move || { |
| 147 | // SAFETY: the scheduler outlives the thread, joined by the hook. |
| 148 | inbound::node_down(unsafe { &*(address as *const Shared) }, &dist(), node); |
| 149 | }); |
| 150 | while !loser.is_finished() && !(listed && !dist().tied_to(node).contains(&pid)) { |
| 151 | std::thread::yield_now(); |
| 152 | } |
| 153 | loser |
| 154 | } |
| 155 | |
| 156 | /// Start the fake transport, forgetting earlier calls and ties. |
| 157 | fn start_fake() { |
| 158 | CALLS.lock().expect("calls").clear(); |
| 159 | *DIST.lock().expect("the ties") = Some(Arc::default()); |
| 160 | install(&FAKE); |
| 161 | } |
| 162 | |
| 163 | fn pid(node: u16, serial: i64) -> i64 { |
| 164 | remote(node, serial).expect("a remote pid") |
| 165 | } |
| 166 | |
| 167 | /// Perform `proc` operation `op` with `args` (owned), as generated code |
| 168 | /// does: through a clause of the actor's root capability. |
| 169 | /// |
| 170 | /// # Safety |
| 171 | /// `ctx` must be the running actor's context. |
| 172 | unsafe fn perform(ctx: *mut Ctx, op: &str, args: &[Value]) -> Value { |
| 173 | let i = PROC_OPS |
| 174 | .iter() |
| 175 | .position(|o| *o == op) |
| 176 | .expect("an operation"); |
| 177 | // SAFETY: forwarded caller guarantee; a capability holds a clause per |
| 178 | // operation. |
| 179 | unsafe { |
| 180 | let cap = crate::process::root_cap(RootCap::Proc); |
| 181 | let clause = dup_value(*Block::fields_ptr(cap as *mut Block).add(i)); |
| 182 | drop_value(ctx, cap); |
| 183 | crate::prims::call_closure(ctx, clause, args) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// Field `i` of a constructor block (borrowed). |
| 188 | /// |
| 189 | /// # Safety |
| 190 | /// `v` must be a live block with more than `i` fields. |
| 191 | unsafe fn field(v: Value, i: usize) -> Value { |
| 192 | // SAFETY: caller guarantee. |
| 193 | unsafe { *Block::fields_ptr(v as *mut Block).add(i) } |
| 194 | } |
| 195 | |
| 196 | /// A subject's tag. |
| 197 | /// |
| 198 | /// # Safety |
| 199 | /// `s` must be a live subject. |
| 200 | unsafe fn tag_of(s: Value) -> i64 { |
| 201 | // SAFETY: caller guarantee. |
| 202 | unsafe { int_get(field(s, 1)) } |
| 203 | } |
| 204 | |
| 205 | /// The message on `subject` (borrowed), waiting up to `timeout` (0: none). |
| 206 | /// |
| 207 | /// # Safety |
| 208 | /// `ctx` must be the running actor's context. |
| 209 | unsafe fn receive(ctx: *mut Ctx, subject: Value, timeout: i64) -> Option<Value> { |
| 210 | // SAFETY: forwarded caller guarantee; `receive` answers an `Option`. |
| 211 | unsafe { |
| 212 | let answer = perform(ctx, "receive", &[dup_value(subject), int(timeout)]); |
| 213 | assert_eq!((*ctx).yielding, YIELD_NONE, "the receive returned"); |
| 214 | if !is_ptr(answer) { |
| 215 | return None; |
| 216 | } |
| 217 | let message = dup_value(field(answer, 0)); |
| 218 | drop_value(ctx, answer); |
| 219 | Some(message) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /// An `ExitReason` value. |
| 224 | /// |
| 225 | /// # Safety |
| 226 | /// `v` must be a live `ExitReason`. |
| 227 | unsafe fn reason(v: Value) -> Exit { |
| 228 | if !is_ptr(v) { |
| 229 | return match int_get(v) { |
| 230 | 0 => Exit::Normal, |
| 231 | 1 => Exit::Killed, |
| 232 | 3 => Exit::Shutdown, |
| 233 | other => panic!("no reason {other}"), |
| 234 | }; |
| 235 | } |
| 236 | // SAFETY: caller guarantee: `Crashed(message)`. |
| 237 | let message = unsafe { crate::values::string_bytes(field(v, 0)) }; |
| 238 | Exit::Crashed(String::from_utf8_lossy(message).into_owned()) |
| 239 | } |
| 240 | |
| 241 | /// A `Down(pid, reason)` or `ExitSignal(pid, reason)` (owned, dropped). |
| 242 | /// |
| 243 | /// # Safety |
| 244 | /// `ctx` must be the running actor's context; `v` such a value. |
| 245 | unsafe fn signal_of(ctx: *mut Ctx, v: Value) -> (i64, Exit) { |
| 246 | // SAFETY: caller guarantee. |
| 247 | unsafe { |
| 248 | let parts = (int_get(field(v, 0)), reason(field(v, 1))); |
| 249 | drop_value(ctx, v); |
| 250 | parts |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | fn crashed(message: &str) -> Exit { |
| 255 | Exit::Crashed(message.into()) |
| 256 | } |
| 257 | |
| 258 | /// Run `f` with the scheduler on a thread that is no scheduler's, as the |
| 259 | /// transport's. |
| 260 | /// |
| 261 | /// # Safety |
| 262 | /// `ctx` must be the running actor's context; the thread must be joined |
| 263 | /// before the actor returns. |
| 264 | unsafe fn outside( |
| 265 | ctx: *mut Ctx, |
| 266 | f: impl FnOnce(&Shared) + Send + 'static, |
| 267 | ) -> std::thread::JoinHandle<()> { |
| 268 | // SAFETY: forwarded caller guarantee. |
| 269 | let address = std::ptr::from_ref(unsafe { this(ctx) }.1) as usize; |
| 270 | std::thread::spawn(move || { |
| 271 | // SAFETY: the scheduler outlives the thread (caller guarantee). |
| 272 | f(unsafe { &*(address as *const Shared) }); |
| 273 | }) |
| 274 | } |
| 275 | |
| 276 | /// [`outside`], waiting for it. |
| 277 | /// |
| 278 | /// # Safety |
| 279 | /// `ctx` must be the running actor's context. |
| 280 | unsafe fn from_outside(ctx: *mut Ctx, f: impl FnOnce(&Shared) + Send + 'static) { |
| 281 | // SAFETY: forwarded caller guarantee; joined at once. |
| 282 | unsafe { outside(ctx, f) } |
| 283 | .join() |
| 284 | .expect("the transport's thread"); |
| 285 | } |
| 286 | |
| 287 | // ---- without hooks --------------------------------------------------------------- |
| 288 | |
| 289 | /// Every operation on a remote pid, with no transport. |
| 290 | unsafe extern "C" fn treats_remote_pids_as_dead(ctx: *mut Ctx) -> Value { |
| 291 | // SAFETY: the root actor's entry. |
| 292 | unsafe { |
| 293 | uninstall(); |
| 294 | CALLS.lock().expect("calls").clear(); |
| 295 | let other = pid(3, 42); |
| 296 | let to = crate::values::cell(ctx, 1, &[int(other), int(7), int(0)]); |
| 297 | assert_eq!(perform(ctx, "send", &[to, int(5)]), UNIT, "dropped"); |
| 298 | perform(ctx, "link", &[int(other)]); |
| 299 | let a = this(ctx).0; |
| 300 | assert_eq!(a.with_proc(|p| p.unlink(other)), (false, false), "no link"); |
| 301 | let down = perform(ctx, "monitor", &[int(other)]); |
| 302 | let message = receive(ctx, down, 0).expect("a Down at once"); |
| 303 | assert_eq!(signal_of(ctx, message), (other, crashed("noproc"))); |
| 304 | drop_value(ctx, down); |
| 305 | perform(ctx, "kill", &[int(other)]); |
| 306 | perform(ctx, "stop", &[int(other)]); |
| 307 | assert_eq!((*ctx).yielding, YIELD_NONE, "still running"); |
| 308 | assert!(a.with_proc(|p| p.ties().nodes.is_empty()), "no ties"); |
| 309 | assert!(calls().is_empty()); |
| 310 | } |
| 311 | UNIT |
| 312 | } |
| 313 | |
| 314 | #[test] |
| 315 | fn without_a_transport_a_remote_pid_is_a_dead_one() { |
| 316 | for threads in THREADS { |
| 317 | assert_eq!( |
| 318 | run_root(threads, treats_remote_pids_as_dead), |
| 319 | Exit::Normal, |
| 320 | "{threads} threads" |
| 321 | ); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /// Receives on a `Remote` subject. |
| 326 | unsafe extern "C" fn receives_on_a_remote_subject(ctx: *mut Ctx) -> Value { |
| 327 | // SAFETY: the root actor's entry. |
| 328 | unsafe { |
| 329 | let from = crate::values::cell(ctx, 1, &[int(pid(0, 0)), int(7), int(0)]); |
| 330 | perform(ctx, "receive", &[from, int(0)]); |
| 331 | assert_eq!((*ctx).yielding, YIELD_PANIC); |
| 332 | } |
| 333 | UNIT |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn receiving_on_a_remote_subject_panics() { |
| 338 | for threads in THREADS { |
| 339 | assert_eq!( |
| 340 | run_root(threads, receives_on_a_remote_subject), |
| 341 | crashed("receive on a subject owned by another actor"), |
| 342 | "{threads} threads" |
| 343 | ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // ---- outbound ---------------------------------------------------------------------- |
| 348 | |
| 349 | static CRASHER_LINKS: AtomicI64 = AtomicI64::new(0); |
| 350 | |
| 351 | /// Links the pid in `CRASHER_LINKS`, then crashes. |
| 352 | unsafe extern "C" fn links_then_crashes(ctx: *mut Ctx) -> Value { |
| 353 | // SAFETY: an actor's entry. |
| 354 | unsafe { |
| 355 | perform(ctx, "link", &[int(CRASHER_LINKS.load(Ordering::SeqCst))]); |
| 356 | crate::effects::tl_panic_str(ctx, c"boom".as_ptr()) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /// Every operation on a remote pid, through the fake transport. |
| 361 | unsafe extern "C" fn reaches_the_transport(ctx: *mut Ctx) -> Value { |
| 362 | // SAFETY: the root actor's entry. |
| 363 | unsafe { |
| 364 | start_fake(); |
| 365 | let a = this(ctx).0; |
| 366 | let me = a.pid; |
| 367 | let other = pid(3, 42); |
| 368 | let to = crate::values::cell(ctx, 1, &[int(other), int(7), int(2)]); |
| 369 | perform(ctx, "send", &[to, int(5)]); |
| 370 | perform(ctx, "link", &[int(other)]); |
| 371 | let down = perform(ctx, "monitor", &[int(other)]); |
| 372 | let tag = tag_of(down); |
| 373 | assert_eq!(receive(ctx, down, 0), None, "no Down yet"); |
| 374 | drop_value(ctx, down); |
| 375 | perform(ctx, "kill", &[int(other)]); |
| 376 | perform(ctx, "stop", &[int(other)]); |
| 377 | assert_eq!( |
| 378 | calls(), |
| 379 | [ |
| 380 | Call::Send(other, 7, 2, 5), |
| 381 | Call::Link(me, other), |
| 382 | Call::Monitor(me, tag, other), |
| 383 | Call::Signal(me, other, Signal::Kill), |
| 384 | Call::Signal(me, other, Signal::Stop), |
| 385 | ] |
| 386 | ); |
| 387 | assert_eq!(dist().tied_to(3), [me]); |
| 388 | // An actor linked to a remote pid crashes: the transport is told, |
| 389 | // and the actor is no longer listed as tied to the node. |
| 390 | let linked = pid(4, 1); |
| 391 | CRASHER_LINKS.store(linked, Ordering::SeqCst); |
| 392 | let crasher = spawn(ctx, links_then_crashes); |
| 393 | yield_until(ctx, "the crasher to finish", |s| s.actors.all().len() == 1); |
| 394 | let told = Call::Signal(crasher, linked, Signal::Linked(crashed("boom"))); |
| 395 | assert_eq!(calls()[5..], [Call::Link(crasher, linked), told]); |
| 396 | assert!(dist().tied_to(4).is_empty(), "untied at its exit"); |
| 397 | uninstall(); |
| 398 | } |
| 399 | UNIT |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn remote_operations_reach_the_transport() { |
| 404 | for threads in THREADS { |
| 405 | assert_eq!( |
| 406 | run_root(threads, reaches_the_transport), |
| 407 | Exit::Normal, |
| 408 | "{threads} threads" |
| 409 | ); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // ---- inbound ----------------------------------------------------------------------- |
| 414 | |
| 415 | /// Waits for a message the transport delivers once every actor waits: |
| 416 | /// with a transport running, that is no deadlock. |
| 417 | unsafe extern "C" fn receives_from_the_transport(ctx: *mut Ctx) -> Value { |
| 418 | // SAFETY: the root actor's entry; it joins the thread before returning. |
| 419 | unsafe { |
| 420 | start_fake(); |
| 421 | spawn(ctx, waits_forever); |
| 422 | let me = this(ctx).0.pid; |
| 423 | let subject = perform(ctx, "subject", &[]); |
| 424 | let tag = tag_of(subject); |
| 425 | // The root joins it before it returns. |
| 426 | let transport = outside(ctx, move |sched| { |
| 427 | while !sched.idling.load(Ordering::SeqCst) { |
| 428 | std::thread::sleep(std::time::Duration::from_micros(100)); |
| 429 | } |
| 430 | inbound::deliver(sched, me, tag, int(42)); |
| 431 | }); |
| 432 | let message = receive(ctx, subject, -1).expect("a message"); |
| 433 | assert_eq!(int_get(message), 42); |
| 434 | drop_value(ctx, subject); |
| 435 | transport.join().expect("the transport's thread"); |
| 436 | uninstall(); |
| 437 | } |
| 438 | UNIT |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn a_running_transport_is_no_deadlock_and_delivers_from_its_thread() { |
| 443 | for threads in THREADS { |
| 444 | assert_eq!( |
| 445 | run_root(threads, receives_from_the_transport), |
| 446 | Exit::Normal, |
| 447 | "{threads} threads" |
| 448 | ); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static LINKED_TO: AtomicI64 = AtomicI64::new(0); |
| 453 | |
| 454 | /// Links the pid in `LINKED_TO`, then waits until stopped. |
| 455 | unsafe extern "C" fn links_and_waits(ctx: *mut Ctx) -> Value { |
| 456 | // SAFETY: an actor's entry. |
| 457 | unsafe { |
| 458 | perform(ctx, "link", &[int(LINKED_TO.load(Ordering::SeqCst))]); |
| 459 | wait_forever(ctx); |
| 460 | } |
| 461 | UNIT |
| 462 | } |
| 463 | |
| 464 | /// Whether [`links_when_told_and_waits`] may link. |
| 465 | static LINK_NOW: AtomicBool = AtomicBool::new(false); |
| 466 | |
| 467 | /// As [`links_and_waits`], once `LINK_NOW` is set. |
| 468 | unsafe extern "C" fn links_when_told_and_waits(ctx: *mut Ctx) -> Value { |
| 469 | // SAFETY: an actor's entry. |
| 470 | unsafe { |
| 471 | yield_until(ctx, "the go to link", |_| LINK_NOW.load(Ordering::SeqCst)); |
| 472 | links_and_waits(ctx) |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /// Spawn an actor running `entry` and monitor it: its `Down` subject. |
| 477 | /// |
| 478 | /// # Safety |
| 479 | /// `ctx` must be the running actor's context. |
| 480 | unsafe fn spawn_watched(ctx: *mut Ctx, entry: super::EntryFn) -> (i64, Value) { |
| 481 | // SAFETY: forwarded caller guarantee. |
| 482 | unsafe { |
| 483 | let child = spawn(ctx, entry); |
| 484 | (child, perform(ctx, "monitor", &[int(child)])) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Exit signals from a remote pid: through links (trapped or not), and |
| 489 | /// `stop` and `kill`. |
| 490 | unsafe extern "C" fn takes_remote_signals(ctx: *mut Ctx) -> Value { |
| 491 | // SAFETY: the root actor's entry. |
| 492 | unsafe { |
| 493 | start_fake(); |
| 494 | let me = this(ctx).0.pid; |
| 495 | let other = pid(3, 42); |
| 496 | LINKED_TO.store(other, Ordering::SeqCst); |
| 497 | perform(ctx, "trap_exits", &[int(1)]); |
| 498 | let exits = perform(ctx, "exits", &[]); |
| 499 | perform(ctx, "link", &[int(other)]); |
| 500 | let (child, child_down) = spawn_watched(ctx, links_and_waits); |
| 501 | yield_until(ctx, "the child to link", |_| { |
| 502 | calls().contains(&Call::Link(child, other)) |
| 503 | }); |
| 504 | let (victim, victim_down) = spawn_watched(ctx, waits_forever); |
| 505 | from_outside(ctx, move |sched| { |
| 506 | let boom = Signal::Linked(crashed("boom")); |
| 507 | inbound::signal(sched, other, me, &boom); |
| 508 | inbound::signal(sched, other, child, &boom); |
| 509 | // The link is gone: a second exit through it changes nothing. |
| 510 | inbound::signal(sched, other, me, &boom); |
| 511 | inbound::signal(sched, other, me, &Signal::Stop); |
| 512 | inbound::signal(sched, other, victim, &Signal::Kill); |
| 513 | }); |
| 514 | let trapped = receive(ctx, exits, -1).expect("an exit signal"); |
| 515 | assert_eq!(signal_of(ctx, trapped), (other, crashed("boom"))); |
| 516 | let stopped = receive(ctx, exits, -1).expect("an exit signal"); |
| 517 | assert_eq!(signal_of(ctx, stopped), (other, Exit::Shutdown)); |
| 518 | assert_eq!(receive(ctx, exits, 0), None, "one exit through the link"); |
| 519 | let down = receive(ctx, child_down, -1).expect("the child's Down"); |
| 520 | assert_eq!(signal_of(ctx, down), (child, crashed("boom"))); |
| 521 | // The child is unlisted at its exit, although the remote exit had |
| 522 | // already taken its only tie to the node. |
| 523 | assert_eq!(dist().tied_to(3), [me], "the child unlisted"); |
| 524 | let down = receive(ctx, victim_down, -1).expect("the victim's Down"); |
| 525 | assert_eq!(signal_of(ctx, down), (victim, Exit::Killed)); |
| 526 | for subject in [exits, child_down, victim_down] { |
| 527 | drop_value(ctx, subject); |
| 528 | } |
| 529 | uninstall(); |
| 530 | } |
| 531 | UNIT |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn remote_exit_signals_reach_local_ties() { |
| 536 | for threads in THREADS { |
| 537 | assert_eq!( |
| 538 | run_root(threads, takes_remote_signals), |
| 539 | Exit::Normal, |
| 540 | "{threads} threads" |
| 541 | ); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static TRAPPER_LINKS: Mutex<Vec<i64>> = Mutex::new(Vec::new()); |
| 546 | static TRAPPED: Mutex<Option<(i64, Exit)>> = Mutex::new(None); |
| 547 | |
| 548 | /// Traps exits, links the pids in `TRAPPER_LINKS`, and records the first |
| 549 | /// exit signal it gets. |
| 550 | unsafe extern "C" fn traps_one_exit(ctx: *mut Ctx) -> Value { |
| 551 | // SAFETY: an actor's entry. |
| 552 | unsafe { |
| 553 | perform(ctx, "trap_exits", &[int(1)]); |
| 554 | let exits = perform(ctx, "exits", &[]); |
| 555 | for other in TRAPPER_LINKS.lock().expect("links").clone() { |
| 556 | perform(ctx, "link", &[int(other)]); |
| 557 | } |
| 558 | let signal = receive(ctx, exits, -1).expect("an exit signal"); |
| 559 | *TRAPPED.lock().expect("trapped") = Some(signal_of(ctx, signal)); |
| 560 | drop_value(ctx, exits); |
| 561 | } |
| 562 | UNIT |
| 563 | } |
| 564 | |
| 565 | /// Node 5 is lost: its links and monitors end with `noconnection`; node |
| 566 | /// 6's stay. |
| 567 | unsafe extern "C" fn loses_a_node(ctx: *mut Ctx) -> Value { |
| 568 | // SAFETY: the root actor's entry. |
| 569 | unsafe { |
| 570 | start_fake(); |
| 571 | let (lost, kept) = (5, 6); |
| 572 | let (linked, watched, elsewhere) = (pid(lost, 1), pid(lost, 2), pid(kept, 1)); |
| 573 | LINKED_TO.store(linked, Ordering::SeqCst); |
| 574 | *TRAPPER_LINKS.lock().expect("links") = vec![linked, elsewhere]; |
| 575 | *TRAPPED.lock().expect("trapped") = None; |
| 576 | let (plain, plain_down) = spawn_watched(ctx, links_and_waits); |
| 577 | let trapper = spawn(ctx, traps_one_exit); |
| 578 | let watched_down = perform(ctx, "monitor", &[int(watched)]); |
| 579 | let elsewhere_down = perform(ctx, "monitor", &[int(elsewhere)]); |
| 580 | yield_until(ctx, "the children to link", |_| { |
| 581 | let calls = calls(); |
| 582 | calls.contains(&Call::Link(plain, linked)) |
| 583 | && calls.contains(&Call::Link(trapper, elsewhere)) |
| 584 | }); |
| 585 | let me = this(ctx).0.pid; |
| 586 | assert_eq!(dist().tied_to(lost), [me, plain, trapper]); |
| 587 | let watched_tag = tag_of(watched_down); |
| 588 | let elsewhere_tag = tag_of(elsewhere_down); |
| 589 | from_outside(ctx, move |sched| inbound::node_down(sched, &dist(), lost)); |
| 590 | let noconnection = crashed("noconnection"); |
| 591 | let down = receive(ctx, plain_down, -1).expect("the plain child's Down"); |
| 592 | assert_eq!(signal_of(ctx, down), (plain, noconnection.clone())); |
| 593 | let down = receive(ctx, watched_down, -1).expect("the remote Down"); |
| 594 | assert_eq!(signal_of(ctx, down), (watched, noconnection.clone())); |
| 595 | yield_until(ctx, "the trapper to finish", |s| s.actors.all().len() == 1); |
| 596 | assert_eq!( |
| 597 | *TRAPPED.lock().expect("trapped"), |
| 598 | Some((linked, noconnection.clone())) |
| 599 | ); |
| 600 | // The trapper's exit reached its link on node 6 only. |
| 601 | let told = Call::Signal(trapper, elsewhere, Signal::Linked(Exit::Normal)); |
| 602 | assert!(calls().contains(&told)); |
| 603 | assert!( |
| 604 | !calls() |
| 605 | .iter() |
| 606 | .any(|c| matches!(c, Call::Signal(_, to, _) if *to == linked)) |
| 607 | ); |
| 608 | assert!(dist().tied_to(lost).is_empty()); |
| 609 | assert_eq!(dist().tied_to(kept), [me]); |
| 610 | // Node 6 is untouched; a `Down` the fan-out already sent comes once. |
| 611 | assert_eq!(receive(ctx, elsewhere_down, 0), None); |
| 612 | from_outside(ctx, move |sched| { |
| 613 | inbound::down(sched, me, watched_tag, watched, &Exit::Normal); |
| 614 | inbound::down(sched, me, elsewhere_tag, elsewhere, &Exit::Normal); |
| 615 | }); |
| 616 | assert_eq!(receive(ctx, watched_down, 0), None, "one Down"); |
| 617 | let down = receive(ctx, elsewhere_down, -1).expect("node 6's Down"); |
| 618 | assert_eq!(signal_of(ctx, down), (elsewhere, Exit::Normal)); |
| 619 | for subject in [plain_down, watched_down, elsewhere_down] { |
| 620 | drop_value(ctx, subject); |
| 621 | } |
| 622 | uninstall(); |
| 623 | } |
| 624 | UNIT |
| 625 | } |
| 626 | |
| 627 | #[test] |
| 628 | fn a_lost_node_ends_every_tie_to_it_with_noconnection() { |
| 629 | for threads in THREADS { |
| 630 | assert_eq!( |
| 631 | run_root(threads, loses_a_node), |
| 632 | Exit::Normal, |
| 633 | "{threads} threads" |
| 634 | ); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // ---- ties a remote node makes ------------------------------------------------------ |
| 639 | |
| 640 | /// Remote pids link to and monitor local actors: the ties are kept and |
| 641 | /// listed, the actor's exit goes out through the transport, and a pid that |
| 642 | /// is gone answers `noproc`. |
| 643 | unsafe extern "C" fn takes_remote_ties(ctx: *mut Ctx) -> Value { |
| 644 | // SAFETY: the root actor's entry. |
| 645 | unsafe { |
| 646 | start_fake(); |
| 647 | let (linker, watcher) = (pid(3, 1), pid(3, 2)); |
| 648 | let (child, child_down) = spawn_watched(ctx, waits_forever); |
| 649 | from_outside(ctx, move |sched| { |
| 650 | inbound::link(sched, &dist(), linker, child); |
| 651 | inbound::monitor(sched, &dist(), watcher, 9, child); |
| 652 | // Twice: one link, two monitors (as locally). |
| 653 | inbound::link(sched, &dist(), linker, child); |
| 654 | inbound::monitor(sched, &dist(), watcher, 10, child); |
| 655 | }); |
| 656 | assert_eq!(dist().tied_to(3), [child]); |
| 657 | assert!(calls().is_empty(), "nothing sent yet"); |
| 658 | perform(ctx, "kill", &[int(child)]); |
| 659 | let down = receive(ctx, child_down, -1).expect("the child's Down"); |
| 660 | assert_eq!(signal_of(ctx, down), (child, Exit::Killed)); |
| 661 | assert_eq!( |
| 662 | calls(), |
| 663 | [ |
| 664 | Call::Down(child, watcher, 9, Exit::Killed), |
| 665 | Call::Down(child, watcher, 10, Exit::Killed), |
| 666 | Call::Signal(child, linker, Signal::Linked(Exit::Killed)), |
| 667 | ] |
| 668 | ); |
| 669 | assert!(dist().tied_to(3).is_empty(), "unlisted at its exit"); |
| 670 | // Now gone, as a pid that never was: `noproc`, and no listing. |
| 671 | let never = child + 1_000_000; |
| 672 | CALLS.lock().expect("calls").clear(); |
| 673 | from_outside(ctx, move |sched| { |
| 674 | inbound::link(sched, &dist(), linker, child); |
| 675 | inbound::monitor(sched, &dist(), watcher, 11, never); |
| 676 | }); |
| 677 | let noproc = crashed("noproc"); |
| 678 | assert_eq!( |
| 679 | calls(), |
| 680 | [ |
| 681 | Call::Signal(child, linker, Signal::Linked(noproc.clone())), |
| 682 | Call::Down(never, watcher, 11, noproc), |
| 683 | ] |
| 684 | ); |
| 685 | assert!(dist().tied_to(3).is_empty()); |
| 686 | drop_value(ctx, child_down); |
| 687 | uninstall(); |
| 688 | } |
| 689 | UNIT |
| 690 | } |
| 691 | |
| 692 | #[test] |
| 693 | fn remote_pids_link_to_and_monitor_local_actors() { |
| 694 | for threads in THREADS { |
| 695 | assert_eq!( |
| 696 | run_root(threads, takes_remote_ties), |
| 697 | Exit::Normal, |
| 698 | "{threads} threads" |
| 699 | ); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /// A lost node ends the ties its pids made, once: a trapping actor gets |
| 704 | /// one exit signal, whether the node's loss or the peer's own exit comes |
| 705 | /// first, and the monitors those pids held are forgotten. |
| 706 | unsafe extern "C" fn loses_a_node_tied_to_it(ctx: *mut Ctx) -> Value { |
| 707 | // SAFETY: the root actor's entry. |
| 708 | unsafe { |
| 709 | start_fake(); |
| 710 | let (me, sched) = (this(ctx).0.pid, this(ctx).1); |
| 711 | let (lost, kept) = (5, 6); |
| 712 | let (linker, watcher, elsewhere) = (pid(lost, 1), pid(lost, 2), pid(kept, 1)); |
| 713 | perform(ctx, "trap_exits", &[int(1)]); |
| 714 | let exits = perform(ctx, "exits", &[]); |
| 715 | from_outside(ctx, move |sched| { |
| 716 | inbound::link(sched, &dist(), linker, me); |
| 717 | inbound::monitor(sched, &dist(), watcher, 9, me); |
| 718 | inbound::monitor(sched, &dist(), elsewhere, 9, me); |
| 719 | }); |
| 720 | assert_eq!(dist().tied_to(lost), [me]); |
| 721 | from_outside(ctx, move |sched| { |
| 722 | inbound::node_down(sched, &dist(), lost); |
| 723 | // The peer's own exit, late: the link is gone. |
| 724 | let late = Signal::Linked(crashed("boom")); |
| 725 | inbound::signal(sched, linker, me, &late); |
| 726 | }); |
| 727 | let trapped = receive(ctx, exits, -1).expect("an exit signal"); |
| 728 | assert_eq!(signal_of(ctx, trapped), (linker, crashed("noconnection"))); |
| 729 | assert_eq!(receive(ctx, exits, 0), None, "one exit signal"); |
| 730 | assert!(dist().tied_to(lost).is_empty()); |
| 731 | assert_eq!(dist().tied_to(kept), [me]); |
| 732 | // Only node 6's watcher is left to tell, when this actor exits. |
| 733 | let monitors = sched.with_alive(me, |p| p.ties().monitors.clone()); |
| 734 | assert_eq!(monitors, Some(vec![(elsewhere, 9)])); |
| 735 | assert!(calls().is_empty(), "nothing sent to a lost node"); |
| 736 | drop_value(ctx, exits); |
| 737 | uninstall(); |
| 738 | } |
| 739 | UNIT |
| 740 | } |
| 741 | |
| 742 | #[test] |
| 743 | fn a_lost_node_ends_the_ties_its_pids_made_once() { |
| 744 | for threads in THREADS { |
| 745 | assert_eq!( |
| 746 | run_root(threads, loses_a_node_tied_to_it), |
| 747 | Exit::Normal, |
| 748 | "{threads} threads" |
| 749 | ); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /// A remote link racing the local actor's exit: either the link is made |
| 754 | /// and the exit goes out through it, or the actor was gone and `noproc` |
| 755 | /// does; never both, never neither, and no actor stays listed. |
| 756 | unsafe extern "C" fn races_remote_links_with_exits(ctx: *mut Ctx) -> Value { |
| 757 | // SAFETY: the root actor's entry; it joins each thread it starts. |
| 758 | unsafe { |
| 759 | start_fake(); |
| 760 | let linker = pid(7, 1); |
| 761 | for _ in 0..200 { |
| 762 | let child = spawn(ctx, waits_forever); |
| 763 | let transport = outside(ctx, move |sched| { |
| 764 | inbound::link(sched, &dist(), linker, child) |
| 765 | }); |
| 766 | perform(ctx, "kill", &[int(child)]); |
| 767 | transport.join().expect("the transport's thread"); |
| 768 | yield_until(ctx, "the child to finish", |s| s.actors.all().len() == 1); |
| 769 | let told: Vec<Call> = calls() |
| 770 | .into_iter() |
| 771 | .filter(|c| matches!(c, Call::Signal(from, ..) if *from == child)) |
| 772 | .collect(); |
| 773 | let killed = Call::Signal(child, linker, Signal::Linked(Exit::Killed)); |
| 774 | let noproc = Call::Signal(child, linker, Signal::Linked(crashed("noproc"))); |
| 775 | assert!(told == [killed] || told == [noproc], "{told:?}"); |
| 776 | assert!(dist().tied_to(7).is_empty(), "listed after its exit"); |
| 777 | } |
| 778 | uninstall(); |
| 779 | } |
| 780 | UNIT |
| 781 | } |
| 782 | |
| 783 | #[test] |
| 784 | fn a_remote_link_racing_an_exit_is_answered_once() { |
| 785 | for threads in THREADS { |
| 786 | assert_eq!( |
| 787 | run_root(threads, races_remote_links_with_exits), |
| 788 | Exit::Normal, |
| 789 | "{threads} threads" |
| 790 | ); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /// Links the pid in `LINKED_TO`, and returns (killed meanwhile). |
| 795 | unsafe extern "C" fn links_once(ctx: *mut Ctx) -> Value { |
| 796 | // SAFETY: an actor's entry. |
| 797 | unsafe { |
| 798 | perform(ctx, "link", &[int(LINKED_TO.load(Ordering::SeqCst))]); |
| 799 | } |
| 800 | UNIT |
| 801 | } |
| 802 | |
| 803 | /// An actor killed while it links to a remote pid: its exit goes out after |
| 804 | /// the link frame, whose queuing the kill waits for, so the other side has |
| 805 | /// the link the exit ends. |
| 806 | unsafe extern "C" fn killed_while_linking(ctx: *mut Ctx) -> Value { |
| 807 | // SAFETY: the root actor's entry. |
| 808 | unsafe { |
| 809 | start_fake(); |
| 810 | let other = pid(5, 9); |
| 811 | LINKED_TO.store(other, Ordering::SeqCst); |
| 812 | KILL_WHILE_LINKING.store(true, Ordering::SeqCst); |
| 813 | let (child, down) = spawn_watched(ctx, links_once); |
| 814 | let message = receive(ctx, down, -1).expect("the child's Down"); |
| 815 | assert_eq!(signal_of(ctx, message), (child, Exit::Killed)); |
| 816 | assert_eq!( |
| 817 | calls(), |
| 818 | [ |
| 819 | Call::Link(child, other), |
| 820 | Call::Signal(child, other, Signal::Linked(Exit::Killed)), |
| 821 | ] |
| 822 | ); |
| 823 | assert!(dist().tied_to(5).is_empty(), "untied at its exit"); |
| 824 | drop_value(ctx, down); |
| 825 | uninstall(); |
| 826 | } |
| 827 | UNIT |
| 828 | } |
| 829 | |
| 830 | #[test] |
| 831 | fn a_link_frame_goes_out_before_its_actors_exit() { |
| 832 | for threads in THREADS { |
| 833 | assert_eq!( |
| 834 | run_root(threads, killed_while_linking), |
| 835 | Exit::Normal, |
| 836 | "{threads} threads" |
| 837 | ); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /// A node lost once the link frame is queued, under the actor's lock: its |
| 842 | /// fan-out must find the actor and end the link with `noconnection`, as |
| 843 | /// when the loss comes before the frame (`send` fails). Listed after the |
| 844 | /// lock (as #415 first had it), the actor escaped the fan-out, listed |
| 845 | /// itself under the lost node, and kept its link to a pid of it. |
| 846 | unsafe extern "C" fn loses_the_node_while_linking(ctx: *mut Ctx) -> Value { |
| 847 | // SAFETY: the root actor's entry. |
| 848 | unsafe { |
| 849 | start_fake(); |
| 850 | let other = pid(5, 9); |
| 851 | LINKED_TO.store(other, Ordering::SeqCst); |
| 852 | LOSE_NODE_WHILE_LINKING.store(true, Ordering::SeqCst); |
| 853 | LINK_NOW.store(false, Ordering::SeqCst); |
| 854 | let (child, down) = spawn_watched(ctx, links_when_told_and_waits); |
| 855 | // Monitored first: a child that died before would be `noproc`. |
| 856 | LINK_NOW.store(true, Ordering::SeqCst); |
| 857 | yield_until(ctx, "the child to link", |_| { |
| 858 | calls().contains(&Call::Link(child, other)) |
| 859 | }); |
| 860 | let message = receive(ctx, down, 1000); |
| 861 | let tied = dist().tied_to(5); |
| 862 | let outcome = message.map(|m| signal_of(ctx, m)); |
| 863 | if outcome.is_none() { |
| 864 | // Not to leave it waiting forever. |
| 865 | from_outside(ctx, move |sched| exit_actor(sched, child, Exit::Killed)); |
| 866 | let _ = receive(ctx, down, -1); |
| 867 | } |
| 868 | drop_value(ctx, down); |
| 869 | uninstall(); |
| 870 | let ended = outcome == Some((child, crashed("noconnection"))); |
| 871 | *LOST_WHILE_LINKING.lock().expect("outcome") = Some((ended, child, outcome, tied)); |
| 872 | } |
| 873 | UNIT |
| 874 | } |
| 875 | |
| 876 | /// What [`loses_the_node_while_linking`] saw: whether the child died of |
| 877 | /// `noconnection`, its `Down` if any, and who was listed under the lost |
| 878 | /// node afterwards. |
| 879 | #[allow(clippy::type_complexity)] |
| 880 | static LOST_WHILE_LINKING: Mutex<Option<(bool, i64, Option<(i64, Exit)>, Vec<i64>)>> = |
| 881 | Mutex::new(None); |
| 882 | |
| 883 | #[test] |
| 884 | fn a_node_lost_while_linking_still_ends_the_link() { |
| 885 | for threads in THREADS { |
| 886 | assert_eq!( |
| 887 | run_root(threads, loses_the_node_while_linking), |
| 888 | Exit::Normal, |
| 889 | "{threads} threads" |
| 890 | ); |
| 891 | let seen = LOST_WHILE_LINKING.lock().expect("outcome").take(); |
| 892 | let (ended, child, down, tied) = seen.expect("the root ran"); |
| 893 | assert!( |
| 894 | ended, |
| 895 | "{threads} threads: child {child} outlived its link's lost node (its Down: \ |
| 896 | {down:?}); listed under the lost node afterwards: {tied:?}" |
| 897 | ); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /// As [`loses_the_node_while_linking`], for a new actor linked to the |
| 902 | /// remote asker of its spawn (`spawn_link_on`, [`outbound::link_spawned`]): |
| 903 | /// the asker's node lost once the link frame is queued. |
| 904 | unsafe extern "C" fn loses_the_askers_node_while_linking(ctx: *mut Ctx) -> Value { |
| 905 | // SAFETY: the root actor's entry. |
| 906 | unsafe { |
| 907 | start_fake(); |
| 908 | let (_, sched) = this(ctx); |
| 909 | let asker = pid(5, 9); |
| 910 | let (child, down) = spawn_watched(ctx, waits_forever); |
| 911 | let mut loser = None; |
| 912 | let sent = outbound::link_spawned(sched, &dist(), child, asker, || { |
| 913 | loser = Some(lose_node_meanwhile(sched, child, asker)); |
| 914 | true |
| 915 | }); |
| 916 | loser.expect("the frame queued").join().expect("the loss"); |
| 917 | assert_eq!(sent, Some(true), "the child was alive"); |
| 918 | let message = receive(ctx, down, 1000); |
| 919 | let tied = dist().tied_to(5); |
| 920 | let outcome = message.map(|m| signal_of(ctx, m)); |
| 921 | if outcome.is_none() { |
| 922 | from_outside(ctx, move |sched| exit_actor(sched, child, Exit::Killed)); |
| 923 | let _ = receive(ctx, down, -1); |
| 924 | } |
| 925 | drop_value(ctx, down); |
| 926 | uninstall(); |
| 927 | let ended = outcome == Some((child, crashed("noconnection"))); |
| 928 | *LOST_WHILE_SPAWNING.lock().expect("outcome") = Some((ended, child, outcome, tied)); |
| 929 | } |
| 930 | UNIT |
| 931 | } |
| 932 | |
| 933 | /// What [`loses_the_askers_node_while_linking`] saw, as |
| 934 | /// [`LOST_WHILE_LINKING`]. |
| 935 | #[allow(clippy::type_complexity)] |
| 936 | static LOST_WHILE_SPAWNING: Mutex<Option<(bool, i64, Option<(i64, Exit)>, Vec<i64>)>> = |
| 937 | Mutex::new(None); |
| 938 | |
| 939 | #[test] |
| 940 | fn an_askers_node_lost_while_its_spawn_links_still_ends_the_link() { |
| 941 | for threads in THREADS { |
| 942 | assert_eq!( |
| 943 | run_root(threads, loses_the_askers_node_while_linking), |
| 944 | Exit::Normal, |
| 945 | "{threads} threads" |
| 946 | ); |
| 947 | let seen = LOST_WHILE_SPAWNING.lock().expect("outcome").take(); |
| 948 | let (ended, child, down, tied) = seen.expect("the root ran"); |
| 949 | assert!( |
| 950 | ended, |
| 951 | "{threads} threads: child {child} outlived its asker's lost node (its Down: \ |
| 952 | {down:?}); listed under the lost node afterwards: {tied:?}" |
| 953 | ); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // ---- spawn_monitor_on -------------------------------------------------------------- |
| 958 | |
| 959 | /// Return at once. |
| 960 | unsafe extern "C" fn ends(_ctx: *mut Ctx) -> Value { |
| 961 | UNIT |
| 962 | } |
| 963 | |
| 964 | /// The node of a new actor monitored by a remote asker |
| 965 | /// ([`outbound::monitor_spawned`]): its answer is queued under the actor's |
| 966 | /// lock, so a kill from another thread meanwhile sends its `Down` after |
| 967 | /// the answer, which the asker's node needs first. An answer that cannot |
| 968 | /// be queued forgets the monitor; an actor already gone sends nothing. |
| 969 | unsafe extern "C" fn killed_while_answering(ctx: *mut Ctx) -> Value { |
| 970 | // SAFETY: the root actor's entry. |
| 971 | unsafe { |
| 972 | start_fake(); |
| 973 | let (me, sched) = this(ctx); |
| 974 | let me = me.pid; |
| 975 | let asker = pid(5, 9); |
| 976 | let (child, down) = spawn_watched(ctx, waits_forever); |
| 977 | let mut killer = None; |
| 978 | let made = outbound::monitor_spawned(sched, &dist(), child, asker, 77, || { |
| 979 | killer = Some(kill_meanwhile(sched, child)); |
| 980 | called(Call::Answered(child)); |
| 981 | true |
| 982 | }); |
| 983 | killer.expect("the answer queued").join().expect("the kill"); |
| 984 | assert_eq!(made, Some(true)); |
| 985 | let message = receive(ctx, down, -1).expect("the child's Down"); |
| 986 | assert_eq!(signal_of(ctx, message), (child, Exit::Killed)); |
| 987 | assert_eq!( |
| 988 | calls(), |
| 989 | [ |
| 990 | Call::Answered(child), |
| 991 | Call::Down(child, asker, 77, Exit::Killed), |
| 992 | ] |
| 993 | ); |
| 994 | assert!(dist().tied_to(5).is_empty(), "untied at its exit"); |
| 995 | // Gone: nothing is made or sent. |
| 996 | let gone = outbound::monitor_spawned(sched, &dist(), child, asker, 78, || { |
| 997 | called(Call::Answered(child)); |
| 998 | true |
| 999 | }); |
| 1000 | assert_eq!(gone, None); |
| 1001 | assert_eq!(calls().len(), 2); |
| 1002 | // The asker's node is not up: the monitor is forgotten at once. |
| 1003 | let (other, other_down) = spawn_watched(ctx, waits_forever); |
| 1004 | let made = outbound::monitor_spawned(sched, &dist(), other, asker, 79, || false); |
| 1005 | assert_eq!(made, Some(false)); |
| 1006 | let monitors = sched.with_alive(other, |p| p.ties().monitors.clone()); |
| 1007 | assert_eq!(monitors, Some(vec![(me, tag_of(other_down))])); |
| 1008 | perform(ctx, "kill", &[int(other)]); |
| 1009 | let message = receive(ctx, other_down, -1).expect("its Down"); |
| 1010 | assert_eq!(signal_of(ctx, message), (other, Exit::Killed)); |
| 1011 | assert_eq!(calls().len(), 2, "no Down for the forgotten monitor"); |
| 1012 | drop_value(ctx, down); |
| 1013 | drop_value(ctx, other_down); |
| 1014 | uninstall(); |
| 1015 | } |
| 1016 | UNIT |
| 1017 | } |
| 1018 | |
| 1019 | #[test] |
| 1020 | fn a_monitored_spawns_answer_goes_before_its_down() { |
| 1021 | for threads in THREADS { |
| 1022 | assert_eq!( |
| 1023 | run_root(threads, killed_while_answering), |
| 1024 | Exit::Normal, |
| 1025 | "{threads} threads" |
| 1026 | ); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /// The asker's node, as a monitored spawn's answer arrives |
| 1031 | /// ([`inbound::watch_spawned`]): the asker's half of the monitor and the |
| 1032 | /// answer are made in one step, on the transport's thread, so the `Down` |
| 1033 | /// that follows on the stream is delivered (once), and a lost node ends |
| 1034 | /// the monitor with `noconnection`. An asker that is gone gets nothing, |
| 1035 | /// and its answer is not made. |
| 1036 | unsafe extern "C" fn watches_what_it_spawned(ctx: *mut Ctx) -> Value { |
| 1037 | // SAFETY: the root actor's entry. |
| 1038 | unsafe { |
| 1039 | start_fake(); |
| 1040 | let me = this(ctx).0.pid; |
| 1041 | let answers = perform(ctx, "subject", &[]); |
| 1042 | let downs = perform(ctx, "subject", &[]); |
| 1043 | let lost = perform(ctx, "subject", &[]); |
| 1044 | let (reply_tag, down_tag, lost_tag) = (tag_of(answers), tag_of(downs), tag_of(lost)); |
| 1045 | let (watched, other) = (pid(4, 12), pid(4, 13)); |
| 1046 | from_outside(ctx, move |sched| { |
| 1047 | let watch = |pid, tag| { |
| 1048 | inbound::watch_spawned(sched, &dist(), me, pid, tag, reply_tag, || { |
| 1049 | int(remote_serial(pid)) |
| 1050 | }) |
| 1051 | }; |
| 1052 | assert!(watch(watched, down_tag)); |
| 1053 | // The stream's next frame: the new actor's `Down`, twice. |
| 1054 | inbound::down(sched, me, down_tag, watched, &Exit::Killed); |
| 1055 | inbound::down(sched, me, down_tag, watched, &Exit::Killed); |
| 1056 | assert!(watch(other, lost_tag)); |
| 1057 | }); |
| 1058 | assert_eq!(dist().tied_to(4), [me]); |
| 1059 | for serial in [12, 13] { |
| 1060 | let answer = receive(ctx, answers, 0).expect("the answer"); |
| 1061 | assert_eq!(int_get(answer), serial); |
| 1062 | } |
| 1063 | let message = receive(ctx, downs, 0).expect("the Down"); |
| 1064 | assert_eq!(signal_of(ctx, message), (watched, Exit::Killed)); |
| 1065 | assert!(receive(ctx, downs, 0).is_none(), "one Down"); |
| 1066 | from_outside(ctx, |sched| inbound::node_down(sched, &dist(), 4)); |
| 1067 | let message = receive(ctx, lost, 0).expect("the lost node's Down"); |
| 1068 | assert_eq!(signal_of(ctx, message), (other, crashed("noconnection"))); |
| 1069 | // An asker that is gone. |
| 1070 | let (gone, gone_down) = spawn_watched(ctx, ends); |
| 1071 | let message = receive(ctx, gone_down, -1).expect("its Down"); |
| 1072 | assert_eq!(signal_of(ctx, message), (gone, Exit::Normal)); |
| 1073 | from_outside(ctx, move |sched| { |
| 1074 | let watched = inbound::watch_spawned(sched, &dist(), gone, other, 1, 2, || { |
| 1075 | panic!("an answer made for an asker that is gone") |
| 1076 | }); |
| 1077 | assert!(!watched); |
| 1078 | }); |
| 1079 | assert!(dist().tied_to(4).is_empty()); |
| 1080 | for subject in [answers, downs, lost, gone_down] { |
| 1081 | drop_value(ctx, subject); |
| 1082 | } |
| 1083 | uninstall(); |
| 1084 | } |
| 1085 | UNIT |
| 1086 | } |
| 1087 | |
| 1088 | /// A remote pid's serial. |
| 1089 | fn remote_serial(pid: i64) -> i64 { |
| 1090 | crate::dist::parts(pid).1 |
| 1091 | } |
| 1092 | |
| 1093 | #[test] |
| 1094 | fn a_monitored_spawns_watch_comes_with_its_answer() { |
| 1095 | for threads in THREADS { |
| 1096 | assert_eq!( |
| 1097 | run_root(threads, watches_what_it_spawned), |
| 1098 | Exit::Normal, |
| 1099 | "{threads} threads" |
| 1100 | ); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | // ---- demonitor --------------------------------------------------------------------- |
| 1105 | |
| 1106 | /// `demonitor(pid, down)` (owned `down`). |
| 1107 | /// |
| 1108 | /// # Safety |
| 1109 | /// `ctx` must be the running actor's context. |
| 1110 | unsafe fn demonitor(ctx: *mut Ctx, pid: i64, down: Value) { |
| 1111 | // SAFETY: forwarded caller guarantee. |
| 1112 | unsafe { |
| 1113 | assert_eq!(perform(ctx, "demonitor", &[int(pid), down]), UNIT); |
| 1114 | assert_eq!((*ctx).yielding, YIELD_NONE, "the demonitor returned"); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /// A remote monitor's own half goes first, then the frame: a `Down` that |
| 1119 | /// arrives after is not delivered, and a monitor ends once. |
| 1120 | unsafe extern "C" fn demonitors_a_remote_pid(ctx: *mut Ctx) -> Value { |
| 1121 | // SAFETY: the root actor's entry. |
| 1122 | unsafe { |
| 1123 | start_fake(); |
| 1124 | let me = this(ctx).0.pid; |
| 1125 | let (watched, never) = (pid(3, 42), pid(3, 43)); |
| 1126 | let down = perform(ctx, "monitor", &[int(watched)]); |
| 1127 | let kept = perform(ctx, "monitor", &[int(watched)]); |
| 1128 | let (tag, kept_tag) = (tag_of(down), tag_of(kept)); |
| 1129 | demonitor(ctx, watched, dup_value(down)); |
| 1130 | // Gone already, or never there: nothing to tell the node. |
| 1131 | demonitor(ctx, watched, dup_value(down)); |
| 1132 | demonitor(ctx, never, dup_value(kept)); |
| 1133 | assert_eq!( |
| 1134 | calls(), |
| 1135 | [ |
| 1136 | Call::Monitor(me, tag, watched), |
| 1137 | Call::Monitor(me, kept_tag, watched), |
| 1138 | Call::Demonitor(me, tag, watched), |
| 1139 | ] |
| 1140 | ); |
| 1141 | from_outside(ctx, move |sched| { |
| 1142 | inbound::down(sched, me, tag, watched, &Exit::Normal); |
| 1143 | inbound::down(sched, me, kept_tag, watched, &Exit::Normal); |
| 1144 | }); |
| 1145 | assert_eq!(receive(ctx, down, 0), None, "no Down after demonitor"); |
| 1146 | let message = receive(ctx, kept, 0).expect("the other monitor's Down"); |
| 1147 | assert_eq!(signal_of(ctx, message), (watched, Exit::Normal)); |
| 1148 | // Still listed under the node until it exits. |
| 1149 | assert_eq!(dist().tied_to(3), [me]); |
| 1150 | drop_value(ctx, down); |
| 1151 | drop_value(ctx, kept); |
| 1152 | uninstall(); |
| 1153 | } |
| 1154 | UNIT |
| 1155 | } |
| 1156 | |
| 1157 | #[test] |
| 1158 | fn a_remote_demonitor_stops_its_down() { |
| 1159 | for threads in THREADS { |
| 1160 | assert_eq!( |
| 1161 | run_root(threads, demonitors_a_remote_pid), |
| 1162 | Exit::Normal, |
| 1163 | "{threads} threads" |
| 1164 | ); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /// A remote watcher's demonitor removes its monitor from the local actor: |
| 1169 | /// the actor's exit sends no `Down` for it. |
| 1170 | unsafe extern "C" fn forgets_a_remote_watcher(ctx: *mut Ctx) -> Value { |
| 1171 | // SAFETY: the root actor's entry. |
| 1172 | unsafe { |
| 1173 | start_fake(); |
| 1174 | let sched = this(ctx).1; |
| 1175 | let watcher = pid(3, 2); |
| 1176 | let (child, child_down) = spawn_watched(ctx, waits_forever); |
| 1177 | from_outside(ctx, move |sched| { |
| 1178 | inbound::monitor(sched, &dist(), watcher, 9, child); |
| 1179 | inbound::monitor(sched, &dist(), watcher, 10, child); |
| 1180 | inbound::demonitor(sched, watcher, 9, child); |
| 1181 | // Another tag, another watcher: nothing to forget. |
| 1182 | inbound::demonitor(sched, watcher, 11, child); |
| 1183 | inbound::demonitor(sched, pid(3, 3), 10, child); |
| 1184 | }); |
| 1185 | let monitors = sched.with_alive(child, |p| p.ties().monitors.clone()); |
| 1186 | let me = this(ctx).0.pid; |
| 1187 | assert_eq!( |
| 1188 | monitors, |
| 1189 | Some(vec![(me, tag_of(child_down)), (watcher, 10)]) |
| 1190 | ); |
| 1191 | perform(ctx, "kill", &[int(child)]); |
| 1192 | let down = receive(ctx, child_down, -1).expect("the child's Down"); |
| 1193 | assert_eq!(signal_of(ctx, down), (child, Exit::Killed)); |
| 1194 | assert_eq!(calls(), [Call::Down(child, watcher, 10, Exit::Killed)]); |
| 1195 | // Gone: a late demonitor finds nothing, and answers nothing. |
| 1196 | from_outside(ctx, move |sched| { |
| 1197 | inbound::demonitor(sched, watcher, 10, child) |
| 1198 | }); |
| 1199 | assert_eq!(calls().len(), 1); |
| 1200 | drop_value(ctx, child_down); |
| 1201 | uninstall(); |
| 1202 | } |
| 1203 | UNIT |
| 1204 | } |
| 1205 | |
| 1206 | #[test] |
| 1207 | fn a_remote_watchers_demonitor_removes_its_monitor() { |
| 1208 | for threads in THREADS { |
| 1209 | assert_eq!( |
| 1210 | run_root(threads, forgets_a_remote_watcher), |
| 1211 | Exit::Normal, |
| 1212 | "{threads} threads" |
| 1213 | ); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /// `demonitor` racing the `Down` it stops, from a local actor's exit and |
| 1218 | /// from a remote one: once it returns, a `Down` is in the mailbox already |
| 1219 | /// or never comes. A remote one is sent, or the demonitor frame is, never |
| 1220 | /// both. |
| 1221 | unsafe extern "C" fn races_demonitors_with_downs(ctx: *mut Ctx) -> Value { |
| 1222 | // SAFETY: the root actor's entry; it joins each thread it starts. |
| 1223 | unsafe { |
| 1224 | start_fake(); |
| 1225 | let me = this(ctx).0.pid; |
| 1226 | for i in 0..200 { |
| 1227 | // Local: the child's exit, on another thread. |
| 1228 | let (child, down) = spawn_watched(ctx, waits_forever); |
| 1229 | let kept = perform(ctx, "monitor", &[int(child)]); |
| 1230 | let killer = outside(ctx, move |sched| exit_actor(sched, child, Exit::Killed)); |
| 1231 | demonitor(ctx, child, dup_value(down)); |
| 1232 | let early = receive(ctx, down, 0); |
| 1233 | killer.join().expect("the killer's thread"); |
| 1234 | // Pushed under the same lock as the first monitor's `Down`. |
| 1235 | let message = receive(ctx, kept, -1).expect("the other monitor's Down"); |
| 1236 | drop_value(ctx, message); |
| 1237 | assert_eq!(receive(ctx, down, 0), None, "a local Down after demonitor"); |
| 1238 | if let Some(message) = early { |
| 1239 | drop_value(ctx, message); |
| 1240 | } |
| 1241 | for subject in [down, kept] { |
| 1242 | drop_value(ctx, subject); |
| 1243 | } |
| 1244 | // Remote: the watched pid's `Down`, from the transport's thread. |
| 1245 | let watched = pid(3, i); |
| 1246 | let down = perform(ctx, "monitor", &[int(watched)]); |
| 1247 | let tag = tag_of(down); |
| 1248 | let transport = outside(ctx, move |sched| { |
| 1249 | inbound::down(sched, me, tag, watched, &Exit::Normal) |
| 1250 | }); |
| 1251 | demonitor(ctx, watched, dup_value(down)); |
| 1252 | let early = receive(ctx, down, 0); |
| 1253 | transport.join().expect("the transport's thread"); |
| 1254 | assert_eq!(receive(ctx, down, 0), None, "a remote Down after demonitor"); |
| 1255 | let sent = calls().contains(&Call::Demonitor(me, tag, watched)); |
| 1256 | assert_ne!(sent, early.is_some(), "the Down or the demonitor frame"); |
| 1257 | if let Some(message) = early { |
| 1258 | drop_value(ctx, message); |
| 1259 | } |
| 1260 | drop_value(ctx, down); |
| 1261 | } |
| 1262 | uninstall(); |
| 1263 | } |
| 1264 | UNIT |
| 1265 | } |
| 1266 | |
| 1267 | #[test] |
| 1268 | fn a_demonitor_racing_a_down_leaves_none_to_come() { |
| 1269 | for threads in THREADS { |
| 1270 | assert_eq!( |
| 1271 | run_root(threads, races_demonitors_with_downs), |
| 1272 | Exit::Normal, |
| 1273 | "{threads} threads" |
| 1274 | ); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /// `demonitor` on another actor's subject panics, as `receive` does. |
| 1279 | unsafe extern "C" fn demonitors_on_another_subject(ctx: *mut Ctx) -> Value { |
| 1280 | // SAFETY: the root actor's entry. |
| 1281 | unsafe { |
| 1282 | let down = crate::values::cell(ctx, 0, &[int(this(ctx).0.pid + 1), int(7)]); |
| 1283 | perform(ctx, "demonitor", &[int(0), down]); |
| 1284 | assert_eq!((*ctx).yielding, YIELD_PANIC); |
| 1285 | } |
| 1286 | UNIT |
| 1287 | } |
| 1288 | |
| 1289 | #[test] |
| 1290 | fn demonitor_on_another_actors_subject_panics() { |
| 1291 | for threads in THREADS { |
| 1292 | assert_eq!( |
| 1293 | run_root(threads, demonitors_on_another_subject), |
| 1294 | crashed("demonitor on a subject owned by another actor"), |
| 1295 | "{threads} threads" |
| 1296 | ); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | // ---- unlink ------------------------------------------------------------------------ |
| 1301 | |
| 1302 | /// `unlink(pid)`. |
| 1303 | /// |
| 1304 | /// # Safety |
| 1305 | /// `ctx` must be the running actor's context. |
| 1306 | unsafe fn unlink(ctx: *mut Ctx, pid: i64) { |
| 1307 | // SAFETY: forwarded caller guarantee. |
| 1308 | unsafe { |
| 1309 | assert_eq!(perform(ctx, "unlink", &[int(pid)]), UNIT); |
| 1310 | assert_eq!((*ctx).yielding, YIELD_NONE, "the unlink returned"); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /// Whether `pid` has its half of a link to `other`, and its unlinks |
| 1315 | /// waiting for their acknowledgement. |
| 1316 | fn link_state(sched: &Shared, pid: i64, other: i64) -> (bool, Vec<(i64, u64)>) { |
| 1317 | let state = sched.with_proc(pid, |p| { |
| 1318 | let ties = p.ties(); |
| 1319 | (ties.links.contains(other), ties.unlinks()) |
| 1320 | }); |
| 1321 | state.expect("an actor") |
| 1322 | } |
| 1323 | |
| 1324 | /// A remote unlink: the actor's half waits for the acknowledgement of its |
| 1325 | /// id, the frame sent once. An exit or a link from the pid meanwhile is |
| 1326 | /// ignored, and so is the acknowledgement of another id; a link from the |
| 1327 | /// actor overrides the unlink. Without the node up, or with the node |
| 1328 | /// lost, nothing is left waiting, and no `noconnection` comes through it. |
| 1329 | unsafe extern "C" fn unlinks_a_remote_pid(ctx: *mut Ctx) -> Value { |
| 1330 | // SAFETY: the root actor's entry. |
| 1331 | unsafe { |
| 1332 | start_fake(); |
| 1333 | let (me, sched) = (this(ctx).0.pid, this(ctx).1); |
| 1334 | let (other, far) = (pid(3, 42), pid(5, 1)); |
| 1335 | perform(ctx, "trap_exits", &[int(1)]); |
| 1336 | let exits = perform(ctx, "exits", &[]); |
| 1337 | perform(ctx, "link", &[int(other)]); |
| 1338 | unlink(ctx, other); |
| 1339 | // Unlinking already: nothing more to send (id 2 is not sent). |
| 1340 | unlink(ctx, other); |
| 1341 | assert_eq!(link_state(sched, me, other), (false, vec![(other, 1)])); |
| 1342 | from_outside(ctx, move |sched| { |
| 1343 | inbound::signal(sched, other, me, &Signal::Linked(crashed("boom"))); |
| 1344 | inbound::link(sched, &dist(), other, me); |
| 1345 | inbound::unlink_acked(sched, other, me, 2); |
| 1346 | }); |
| 1347 | assert_eq!(receive(ctx, exits, 0), None, "an exit through the unlink"); |
| 1348 | assert_eq!(link_state(sched, me, other), (false, vec![(other, 1)])); |
| 1349 | from_outside(ctx, move |sched| inbound::unlink_acked(sched, other, me, 1)); |
| 1350 | assert_eq!(link_state(sched, me, other), (false, vec![])); |
| 1351 | // Linked again, by the remote pid: an exit through it counts. |
| 1352 | from_outside(ctx, move |sched| { |
| 1353 | inbound::link(sched, &dist(), other, me); |
| 1354 | inbound::signal(sched, other, me, &Signal::Linked(crashed("boom"))); |
| 1355 | }); |
| 1356 | let trapped = receive(ctx, exits, 0).expect("an exit signal"); |
| 1357 | assert_eq!(signal_of(ctx, trapped), (other, crashed("boom"))); |
| 1358 | // Linked again by this actor before the acknowledgement. |
| 1359 | perform(ctx, "link", &[int(other)]); |
| 1360 | unlink(ctx, other); |
| 1361 | perform(ctx, "link", &[int(other)]); |
| 1362 | from_outside(ctx, move |sched| inbound::unlink_acked(sched, other, me, 3)); |
| 1363 | assert_eq!(link_state(sched, me, other), (true, vec![])); |
| 1364 | UNLINK_NOT_UP.store(true, Ordering::SeqCst); |
| 1365 | unlink(ctx, other); |
| 1366 | assert_eq!(link_state(sched, me, other), (false, vec![]), "not up"); |
| 1367 | perform(ctx, "link", &[int(far)]); |
| 1368 | unlink(ctx, far); |
| 1369 | assert_eq!(link_state(sched, me, far), (false, vec![(far, 5)])); |
| 1370 | from_outside(ctx, move |sched| inbound::node_down(sched, &dist(), 5)); |
| 1371 | assert_eq!(receive(ctx, exits, 0), None, "noconnection through it"); |
| 1372 | assert_eq!(link_state(sched, me, far), (false, vec![]), "lost"); |
| 1373 | assert_eq!( |
| 1374 | calls(), |
| 1375 | [ |
| 1376 | Call::Link(me, other), |
| 1377 | Call::Unlink(me, other, 1), |
| 1378 | Call::Link(me, other), |
| 1379 | Call::Unlink(me, other, 3), |
| 1380 | Call::Link(me, other), |
| 1381 | Call::Link(me, far), |
| 1382 | Call::Unlink(me, far, 5), |
| 1383 | ] |
| 1384 | ); |
| 1385 | assert_eq!(dist().tied_to(3), [me], "listed until it exits"); |
| 1386 | drop_value(ctx, exits); |
| 1387 | uninstall(); |
| 1388 | } |
| 1389 | UNIT |
| 1390 | } |
| 1391 | |
| 1392 | #[test] |
| 1393 | fn a_remote_unlink_waits_for_its_acknowledgement() { |
| 1394 | for threads in THREADS { |
| 1395 | assert_eq!( |
| 1396 | run_root(threads, unlinks_a_remote_pid), |
| 1397 | Exit::Normal, |
| 1398 | "{threads} threads" |
| 1399 | ); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | /// A remote pid unlinks a local actor: the actor's half goes, so its exit |
| 1404 | /// sends nothing, and the unlink is acknowledged, for an actor that is |
| 1405 | /// gone too. An actor unlinking that pid itself keeps its own unlink |
| 1406 | /// waiting, and ignores a link the pid sent before it got that unlink. |
| 1407 | unsafe extern "C" fn takes_remote_unlinks(ctx: *mut Ctx) -> Value { |
| 1408 | // SAFETY: the root actor's entry. |
| 1409 | unsafe { |
| 1410 | start_fake(); |
| 1411 | let (me, sched) = (this(ctx).0.pid, this(ctx).1); |
| 1412 | let linker = pid(3, 1); |
| 1413 | let ack = move |local: i64, id: u64| move || called(Call::UnlinkAck(local, linker, id)); |
| 1414 | let (child, child_down) = spawn_watched(ctx, waits_forever); |
| 1415 | from_outside(ctx, move |sched| { |
| 1416 | inbound::link(sched, &dist(), linker, child); |
| 1417 | inbound::unlink(sched, linker, child, ack(child, 5)); |
| 1418 | }); |
| 1419 | perform(ctx, "kill", &[int(child)]); |
| 1420 | let down = receive(ctx, child_down, -1).expect("the child's Down"); |
| 1421 | assert_eq!(signal_of(ctx, down), (child, Exit::Killed)); |
| 1422 | from_outside(ctx, move |sched| { |
| 1423 | inbound::unlink(sched, linker, child, ack(child, 6)) |
| 1424 | }); |
| 1425 | let acks = [ |
| 1426 | Call::UnlinkAck(child, linker, 5), |
| 1427 | Call::UnlinkAck(child, linker, 6), |
| 1428 | ]; |
| 1429 | assert_eq!(calls(), acks, "no exit sent"); |
| 1430 | perform(ctx, "trap_exits", &[int(1)]); |
| 1431 | let exits = perform(ctx, "exits", &[]); |
| 1432 | from_outside(ctx, move |sched| inbound::link(sched, &dist(), linker, me)); |
| 1433 | unlink(ctx, linker); |
| 1434 | from_outside(ctx, move |sched| { |
| 1435 | inbound::unlink(sched, linker, me, ack(me, 7)); |
| 1436 | inbound::link(sched, &dist(), linker, me); |
| 1437 | inbound::signal(sched, linker, me, &Signal::Linked(crashed("boom"))); |
| 1438 | }); |
| 1439 | assert_eq!(receive(ctx, exits, 0), None); |
| 1440 | assert_eq!(link_state(sched, me, linker), (false, vec![(linker, 1)])); |
| 1441 | from_outside(ctx, move |sched| { |
| 1442 | inbound::unlink_acked(sched, linker, me, 1) |
| 1443 | }); |
| 1444 | assert_eq!(link_state(sched, me, linker), (false, vec![])); |
| 1445 | assert_eq!( |
| 1446 | calls()[2..], |
| 1447 | [Call::Unlink(me, linker, 1), Call::UnlinkAck(me, linker, 7)] |
| 1448 | ); |
| 1449 | drop_value(ctx, exits); |
| 1450 | drop_value(ctx, child_down); |
| 1451 | uninstall(); |
| 1452 | } |
| 1453 | UNIT |
| 1454 | } |
| 1455 | |
| 1456 | #[test] |
| 1457 | fn a_remote_pids_unlink_is_acknowledged() { |
| 1458 | for threads in THREADS { |
| 1459 | assert_eq!( |
| 1460 | run_root(threads, takes_remote_unlinks), |
| 1461 | Exit::Normal, |
| 1462 | "{threads} threads" |
| 1463 | ); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /// `unlink` racing an exit through the link, from a local actor's exit |
| 1468 | /// and from a remote one: once it returns, an `ExitSignal` is in the |
| 1469 | /// mailbox already, or never comes. A remote one is delivered, or the |
| 1470 | /// unlink frame is sent, never both. |
| 1471 | unsafe extern "C" fn races_unlinks_with_exits(ctx: *mut Ctx) -> Value { |
| 1472 | // SAFETY: the root actor's entry; it joins each thread it starts. |
| 1473 | unsafe { |
| 1474 | start_fake(); |
| 1475 | let me = this(ctx).0.pid; |
| 1476 | perform(ctx, "trap_exits", &[int(1)]); |
| 1477 | let exits = perform(ctx, "exits", &[]); |
| 1478 | for i in 0..200 { |
| 1479 | // Local: the child's exit, on another thread, signals the |
| 1480 | // root before it returns. |
| 1481 | let child = spawn(ctx, waits_forever); |
| 1482 | perform(ctx, "link", &[int(child)]); |
| 1483 | let killer = outside(ctx, move |sched| exit_actor(sched, child, Exit::Killed)); |
| 1484 | unlink(ctx, child); |
| 1485 | let early = receive(ctx, exits, 0); |
| 1486 | killer.join().expect("the killer's thread"); |
| 1487 | assert_eq!(receive(ctx, exits, 0), None, "a local exit after unlink"); |
| 1488 | if let Some(message) = early { |
| 1489 | assert_eq!(signal_of(ctx, message), (child, Exit::Killed)); |
| 1490 | } |
| 1491 | yield_until(ctx, "the child to finish", |s| s.actors.all().len() == 1); |
| 1492 | // Remote: the linked pid's exit, from the transport's thread. |
| 1493 | let other = pid(3, i); |
| 1494 | perform(ctx, "link", &[int(other)]); |
| 1495 | let boom = Signal::Linked(crashed("boom")); |
| 1496 | let transport = outside(ctx, move |sched| inbound::signal(sched, other, me, &boom)); |
| 1497 | unlink(ctx, other); |
| 1498 | let early = receive(ctx, exits, 0); |
| 1499 | transport.join().expect("the transport's thread"); |
| 1500 | assert_eq!(receive(ctx, exits, 0), None, "a remote exit after unlink"); |
| 1501 | let sent = calls() |
| 1502 | .iter() |
| 1503 | .any(|c| matches!(c, Call::Unlink(_, to, _) if *to == other)); |
| 1504 | assert_ne!(sent, early.is_some(), "the exit or the unlink frame"); |
| 1505 | if let Some(message) = early { |
| 1506 | assert_eq!(signal_of(ctx, message), (other, crashed("boom"))); |
| 1507 | } |
| 1508 | } |
| 1509 | drop_value(ctx, exits); |
| 1510 | uninstall(); |
| 1511 | } |
| 1512 | UNIT |
| 1513 | } |
| 1514 | |
| 1515 | #[test] |
| 1516 | fn an_unlink_racing_an_exit_leaves_none_to_come() { |
| 1517 | for threads in THREADS { |
| 1518 | assert_eq!( |
| 1519 | run_root(threads, races_unlinks_with_exits), |
| 1520 | Exit::Normal, |
| 1521 | "{threads} threads" |
| 1522 | ); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | // ---- third security review of #404 (dist/secreview3) ----------------------------- |
| 1527 | |
| 1528 | /// The ids of `pid`'s unlinks of `other` waiting for their acknowledgement. |
| 1529 | fn waiting_ids(sched: &Shared, pid: i64, other: i64) -> Vec<u64> { |
| 1530 | let (_, unlinking) = link_state(sched, pid, other); |
| 1531 | unlinking |
| 1532 | .into_iter() |
| 1533 | .filter(|(p, _)| *p == other) |
| 1534 | .map(|(_, id)| id) |
| 1535 | .collect() |
| 1536 | } |
| 1537 | |
| 1538 | /// Pinned (`dist/secreview3`): only the pid an unlink names acknowledges |
| 1539 | /// it, and only with its id; an unlink frame removes only the half of the |
| 1540 | /// pid that sent it. `node.rs` makes a frame's remote pid from its sender's |
| 1541 | /// node (`State::tie`), so a peer names only its own pids: it can neither |
| 1542 | /// end another node's unlink early nor remove a half linking another |
| 1543 | /// node's pid, nor another serial's. |
| 1544 | unsafe extern "C" fn acknowledges_only_its_own_unlinks(ctx: *mut Ctx) -> Value { |
| 1545 | // SAFETY: the root actor's entry. |
| 1546 | unsafe { |
| 1547 | start_fake(); |
| 1548 | let (me, sched) = (this(ctx).0.pid, this(ctx).1); |
| 1549 | // The same serial on nodes 3 and 5, and another serial of node 3. |
| 1550 | let (x, y, z) = (pid(3, 42), pid(5, 42), pid(3, 43)); |
| 1551 | for other in [x, y, z] { |
| 1552 | perform(ctx, "link", &[int(other)]); |
| 1553 | unlink(ctx, other); |
| 1554 | } |
| 1555 | let [ix, iy, iz] = [x, y, z].map(|other| { |
| 1556 | let ids = waiting_ids(sched, me, other); |
| 1557 | assert_eq!(ids.len(), 1, "{other}: one unlink waiting"); |
| 1558 | ids[0] |
| 1559 | }); |
| 1560 | from_outside(ctx, move |sched| { |
| 1561 | // Another node's pid, another serial, another id. |
| 1562 | inbound::unlink_acked(sched, y, me, ix); |
| 1563 | inbound::unlink_acked(sched, z, me, ix); |
| 1564 | inbound::unlink_acked(sched, x, me, iy); |
| 1565 | inbound::unlink_acked(sched, x, me, iz); |
| 1566 | }); |
| 1567 | for (other, id) in [(x, ix), (y, iy), (z, iz)] { |
| 1568 | assert_eq!(waiting_ids(sched, me, other), [id], "{other}: waiting"); |
| 1569 | } |
| 1570 | from_outside(ctx, move |sched| inbound::unlink_acked(sched, x, me, ix)); |
| 1571 | assert!( |
| 1572 | waiting_ids(sched, me, x).is_empty(), |
| 1573 | "x acknowledged its own" |
| 1574 | ); |
| 1575 | assert_eq!(waiting_ids(sched, me, y), [iy]); |
| 1576 | // Linked to x again: unlink frames from y and z leave its half. |
| 1577 | perform(ctx, "link", &[int(x)]); |
| 1578 | from_outside(ctx, move |sched| { |
| 1579 | inbound::unlink(sched, y, me, || {}); |
| 1580 | inbound::unlink(sched, z, me, || {}); |
| 1581 | }); |
| 1582 | assert!(link_state(sched, me, x).0, "x's half kept"); |
| 1583 | from_outside(ctx, move |sched| inbound::unlink(sched, x, me, || {})); |
| 1584 | assert!(!link_state(sched, me, x).0, "x's own unlink removes it"); |
| 1585 | uninstall(); |
| 1586 | } |
| 1587 | UNIT |
| 1588 | } |
| 1589 | |
| 1590 | #[test] |
| 1591 | fn secreview3_an_unlink_is_acknowledged_only_by_its_pid() { |
| 1592 | for threads in THREADS { |
| 1593 | assert_eq!( |
| 1594 | run_root(threads, acknowledges_only_its_own_unlinks), |
| 1595 | Exit::Normal, |
| 1596 | "{threads} threads" |
| 1597 | ); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | /// SR3-2 (minor), confirmed. A peer whose hello sets flag bit 4 but that |
| 1602 | /// never answers op `17` keeps each unlink of one of its pids waiting |
| 1603 | /// (`Ties::unlinking`) until its node is lost: one entry per pid the actor |
| 1604 | /// unlinked, with no cap and no timeout (Erlang's protocol has neither). |
| 1605 | /// Meanwhile those pids' links are ignored, as an unlink in flight |
| 1606 | /// requires. Only an acknowledgement, a new link from the actor, its exit |
| 1607 | /// or the node's loss ends one, and the loss sends no exit through them. |
| 1608 | /// Bounded by the actor's own unlinks of links that existed: a peer adds |
| 1609 | /// none by itself. The entries are scanned by every op `10` and `18` to |
| 1610 | /// the actor and every link it makes (`otp/secreview3.rs` measures it). |
| 1611 | unsafe extern "C" fn keeps_unlinks_a_peer_never_answers(ctx: *mut Ctx) -> Value { |
| 1612 | // SAFETY: the root actor's entry. |
| 1613 | unsafe { |
| 1614 | start_fake(); |
| 1615 | let (me, sched) = (this(ctx).0.pid, this(ctx).1); |
| 1616 | perform(ctx, "trap_exits", &[int(1)]); |
| 1617 | let exits = perform(ctx, "exits", &[]); |
| 1618 | let n = 1_000; |
| 1619 | for serial in 0..n { |
| 1620 | let other = pid(3, serial); |
| 1621 | perform(ctx, "link", &[int(other)]); |
| 1622 | unlink(ctx, other); |
| 1623 | } |
| 1624 | let waiting = |sched: &Shared| { |
| 1625 | sched |
| 1626 | .with_proc(me, |p| p.ties().unlinking.len()) |
| 1627 | .expect("the root") |
| 1628 | }; |
| 1629 | assert_eq!(waiting(sched), n as usize, "one per unlinked pid"); |
| 1630 | // A link from one of them meanwhile is ignored; a link from this |
| 1631 | // actor ends its unlink. |
| 1632 | from_outside(ctx, move |sched| { |
| 1633 | inbound::link(sched, &dist(), pid(3, 0), me) |
| 1634 | }); |
| 1635 | assert!(!link_state(sched, me, pid(3, 0)).0, "a link in flight"); |
| 1636 | perform(ctx, "link", &[int(pid(3, 1))]); |
| 1637 | assert_eq!(waiting(sched), n as usize - 1); |
| 1638 | // The node's loss forgets them, and ends the one link left. |
| 1639 | from_outside(ctx, |sched| inbound::node_down(sched, &dist(), 3)); |
| 1640 | assert_eq!(waiting(sched), 0, "forgotten with the node"); |
| 1641 | let message = receive(ctx, exits, 0).expect("the link's exit"); |
| 1642 | assert_eq!( |
| 1643 | signal_of(ctx, message), |
| 1644 | (pid(3, 1), crashed("noconnection")) |
| 1645 | ); |
| 1646 | assert_eq!(receive(ctx, exits, 0), None, "none through an unlink"); |
| 1647 | drop_value(ctx, exits); |
| 1648 | uninstall(); |
| 1649 | } |
| 1650 | UNIT |
| 1651 | } |
| 1652 | |
| 1653 | #[test] |
| 1654 | fn secreview3_unlinks_a_peer_never_answers_wait_until_its_loss() { |
| 1655 | for threads in THREADS { |
| 1656 | assert_eq!( |
| 1657 | run_root(threads, keeps_unlinks_a_peer_never_answers), |
| 1658 | Exit::Normal, |
| 1659 | "{threads} threads" |
| 1660 | ); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /// Pinned (`dist/secreview3`): the relay makes the asker's half of a |
| 1665 | /// monitored spawn's monitor for the pid spawned and the down tag only |
| 1666 | /// (`inbound::watch_spawned`; `node.rs` takes the tag from the pending |
| 1667 | /// spawn, never from the answer). So a peer's `Down` naming the reply tag |
| 1668 | /// (a subject of `std/dist.Answer`s) or another of its pids is dropped: a |
| 1669 | /// `Down` reaches only the `Subject<Down>` the asker made. |
| 1670 | unsafe extern "C" fn delivers_downs_only_on_the_down_subject(ctx: *mut Ctx) -> Value { |
| 1671 | // SAFETY: the root actor's entry. |
| 1672 | unsafe { |
| 1673 | start_fake(); |
| 1674 | let me = this(ctx).0.pid; |
| 1675 | let answers = perform(ctx, "subject", &[]); |
| 1676 | let downs = perform(ctx, "subject", &[]); |
| 1677 | let (reply_tag, down_tag) = (tag_of(answers), tag_of(downs)); |
| 1678 | let (spawned, other) = (pid(4, 12), pid(4, 13)); |
| 1679 | from_outside(ctx, move |sched| { |
| 1680 | let watched = |
| 1681 | inbound::watch_spawned(sched, &dist(), me, spawned, down_tag, reply_tag, || { |
| 1682 | int(12) |
| 1683 | }); |
| 1684 | assert!(watched); |
| 1685 | inbound::down(sched, me, reply_tag, spawned, &crashed("on the reply tag")); |
| 1686 | inbound::down(sched, me, down_tag, other, &crashed("another pid")); |
| 1687 | }); |
| 1688 | let answer = receive(ctx, answers, 0).expect("the answer"); |
| 1689 | assert_eq!(int_get(answer), 12); |
| 1690 | assert!( |
| 1691 | receive(ctx, answers, 0).is_none(), |
| 1692 | "a Down on the reply tag" |
| 1693 | ); |
| 1694 | assert!(receive(ctx, downs, 0).is_none(), "a Down of another pid"); |
| 1695 | from_outside(ctx, move |sched| { |
| 1696 | inbound::down(sched, me, down_tag, spawned, &Exit::Killed) |
| 1697 | }); |
| 1698 | let message = receive(ctx, downs, 0).expect("its Down"); |
| 1699 | assert_eq!(signal_of(ctx, message), (spawned, Exit::Killed)); |
| 1700 | drop_value(ctx, answers); |
| 1701 | drop_value(ctx, downs); |
| 1702 | uninstall(); |
| 1703 | } |
| 1704 | UNIT |
| 1705 | } |
| 1706 | |
| 1707 | #[test] |
| 1708 | fn secreview3_a_monitored_spawns_down_reaches_only_its_down_subject() { |
| 1709 | for threads in THREADS { |
| 1710 | assert_eq!( |
| 1711 | run_root(threads, delivers_downs_only_on_the_down_subject), |
| 1712 | Exit::Normal, |
| 1713 | "{threads} threads" |
| 1714 | ); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | /// Starting distribution switches the clock to real time, unless |
| 1719 | /// `LOUISE_VIRTUAL_TIME` keeps it virtual. |
| 1720 | unsafe extern "C" fn switches_to_real_time(ctx: *mut Ctx) -> Value { |
| 1721 | // SAFETY: the root actor's entry. |
| 1722 | let sched = unsafe { this(ctx) }.1; |
| 1723 | assert!(!sched.real_time()); |
| 1724 | sched.switch_to_real_time(); |
| 1725 | assert_eq!(sched.real_time(), !sched.virtual_time); |
| 1726 | UNIT |
| 1727 | } |
| 1728 | |
| 1729 | #[test] |
| 1730 | fn starting_distribution_switches_to_real_time() { |
| 1731 | for virtual_time in [false, true] { |
| 1732 | assert_eq!( |
| 1733 | super::run_root_clock(1, virtual_time, switches_to_real_time), |
| 1734 | Exit::Normal, |
| 1735 | "virtual time: {virtual_time}" |
| 1736 | ); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | // ---- back-pressure --------------------------------------------------------------- |
| 1741 | |
| 1742 | /// The fake peer's queue holds this many messages. |
| 1743 | const CAPACITY: usize = 2; |
| 1744 | |
| 1745 | /// The fake peer: its queue, the senders parked on it, and what it gave |
| 1746 | /// up (senders that exited while parked). |
| 1747 | struct Peer { |
| 1748 | queue: Vec<i64>, |
| 1749 | parked: Vec<Waiter>, |
| 1750 | abandoned: Vec<i64>, |
| 1751 | } |
| 1752 | |
| 1753 | static PEER: Mutex<Peer> = Mutex::new(Peer { |
| 1754 | queue: Vec::new(), |
| 1755 | parked: Vec::new(), |
| 1756 | abandoned: Vec::new(), |
| 1757 | }); |
| 1758 | |
| 1759 | fn peer() -> std::sync::MutexGuard<'static, Peer> { |
| 1760 | PEER.lock().expect("the peer") |
| 1761 | } |
| 1762 | |
| 1763 | /// Hooks whose sends queue an `Int` message on [`PEER`], parking while it |
| 1764 | /// is full, as the transport's do. |
| 1765 | static PARKING: Hooks = Hooks { |
| 1766 | send: |ctx, _, _, _, message| { |
| 1767 | let n = int_get(message); |
| 1768 | let attempt = |n: i64, waiter: Option<Waiter>| { |
| 1769 | let mut p = peer(); |
| 1770 | if p.queue.len() < CAPACITY { |
| 1771 | p.queue.push(n); |
| 1772 | return Try::Done(Ok(())); |
| 1773 | } |
| 1774 | p.parked.extend(waiter); |
| 1775 | Try::Full(n) |
| 1776 | }; |
| 1777 | let unwait = |w: Waiter| peer().parked.retain(|p| *p != w); |
| 1778 | // SAFETY: the hook runs on the running actor of `ctx`. |
| 1779 | let sent = unsafe { send_parking(ctx, n, attempt, unwait) }; |
| 1780 | let sent = sent.unwrap_or_else(|| { |
| 1781 | peer().abandoned.push(n); |
| 1782 | Ok(()) |
| 1783 | }); |
| 1784 | sent.map_err(|why| unsendable(&why)) |
| 1785 | }, |
| 1786 | link: |_, _, _| {}, |
| 1787 | monitor: |_, _, _, _| false, |
| 1788 | demonitor: |_, _, _| {}, |
| 1789 | unlink: |_, _| {}, |
| 1790 | signal: |_, _, _| {}, |
| 1791 | down: |_, _, _, _| {}, |
| 1792 | exit: |_, _| {}, |
| 1793 | }; |
| 1794 | |
| 1795 | /// Messages the peer took off its queue, in order; how many waiters it |
| 1796 | /// woke. |
| 1797 | static TAKEN: Mutex<(Vec<i64>, usize)> = Mutex::new((Vec::new(), 0)); |
| 1798 | |
| 1799 | /// The peer writes its queue out, then wakes its parked senders, as the |
| 1800 | /// transport's thread does. |
| 1801 | fn drain(sched: &Shared) { |
| 1802 | let (queue, parked) = { |
| 1803 | let mut p = peer(); |
| 1804 | (std::mem::take(&mut p.queue), std::mem::take(&mut p.parked)) |
| 1805 | }; |
| 1806 | let mut taken = TAKEN.lock().expect("taken"); |
| 1807 | taken.0.extend(queue); |
| 1808 | taken.1 += parked.len(); |
| 1809 | drop(taken); |
| 1810 | for w in parked { |
| 1811 | wake_sender(sched, w); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | fn start_parking() { |
| 1816 | *peer() = Peer { |
| 1817 | queue: Vec::new(), |
| 1818 | parked: Vec::new(), |
| 1819 | abandoned: Vec::new(), |
| 1820 | }; |
| 1821 | *TAKEN.lock().expect("taken") = (Vec::new(), 0); |
| 1822 | install(&PARKING); |
| 1823 | } |
| 1824 | |
| 1825 | /// How many messages [`sends_ten`] sends. |
| 1826 | const SENT: i64 = 10; |
| 1827 | |
| 1828 | /// Sends 1 to [`SENT`] to a remote subject, unless it is killed. |
| 1829 | unsafe extern "C" fn sends_ten(ctx: *mut Ctx) -> Value { |
| 1830 | // SAFETY: an actor's entry. |
| 1831 | unsafe { |
| 1832 | for n in 1..=SENT { |
| 1833 | let to = crate::values::cell(ctx, 1, &[int(pid(3, 42)), int(7), int(0)]); |
| 1834 | perform(ctx, "send", &[to, int(n)]); |
| 1835 | if (*ctx).yielding != YIELD_NONE { |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | UNIT |
| 1841 | } |
| 1842 | |
| 1843 | /// The root drains the peer's queue only between its own turns: a sender |
| 1844 | /// that blocked its thread instead of parking would hang one thread. |
| 1845 | unsafe extern "C" fn parks_senders(ctx: *mut Ctx) -> Value { |
| 1846 | // SAFETY: the root actor's entry. |
| 1847 | unsafe { |
| 1848 | start_parking(); |
| 1849 | let sender = spawn(ctx, sends_ten); |
| 1850 | yield_until(ctx, "the sender parked", |_| { |
| 1851 | peer().parked.iter().any(|w| w.pid == sender) |
| 1852 | }); |
| 1853 | assert_eq!(peer().queue, [1, 2], "the rest waits in its sender"); |
| 1854 | yield_until(ctx, "every message", |s| { |
| 1855 | drain(s); |
| 1856 | TAKEN.lock().expect("taken").0.len() == SENT as usize |
| 1857 | }); |
| 1858 | let (taken, woken) = TAKEN.lock().expect("taken").clone(); |
| 1859 | assert_eq!(taken, (1..=SENT).collect::<Vec<_>>(), "in send order"); |
| 1860 | // On one thread, after every second message; on several, the |
| 1861 | // root may drain as fast as the sender sends. |
| 1862 | assert!(woken >= 1, "woken where it parked"); |
| 1863 | yield_until(ctx, "the sender to finish", |s| s.actors.all().len() == 1); |
| 1864 | assert!(peer().abandoned.is_empty()); |
| 1865 | uninstall(); |
| 1866 | } |
| 1867 | UNIT |
| 1868 | } |
| 1869 | |
| 1870 | #[test] |
| 1871 | fn a_sender_parks_while_the_queue_is_full() { |
| 1872 | for threads in THREADS { |
| 1873 | assert_eq!( |
| 1874 | run_root(threads, parks_senders), |
| 1875 | Exit::Normal, |
| 1876 | "{threads} threads" |
| 1877 | ); |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | /// A sender killed while parked: its message is dropped, its waiter |
| 1882 | /// forgotten, and a late wake for it does nothing. |
| 1883 | unsafe extern "C" fn kills_a_parked_sender(ctx: *mut Ctx) -> Value { |
| 1884 | // SAFETY: the root actor's entry. |
| 1885 | unsafe { |
| 1886 | start_parking(); |
| 1887 | let sender = spawn(ctx, sends_ten); |
| 1888 | yield_until(ctx, "the sender parked", |_| !peer().parked.is_empty()); |
| 1889 | let stale = peer().parked[0]; |
| 1890 | let down = perform(ctx, "monitor", &[int(sender)]); |
| 1891 | perform(ctx, "kill", &[int(sender)]); |
| 1892 | let message = receive(ctx, down, -1).expect("its Down"); |
| 1893 | assert_eq!(signal_of(ctx, message), (sender, Exit::Killed)); |
| 1894 | drop_value(ctx, down); |
| 1895 | // Its `Down` comes at the kill; it unwinds when it runs next. |
| 1896 | yield_until(ctx, "the sender to unwind", |s| s.actors.all().len() == 1); |
| 1897 | { |
| 1898 | let p = peer(); |
| 1899 | assert_eq!(p.queue, [1, 2]); |
| 1900 | assert!(p.parked.is_empty(), "unregistered: {:?}", p.parked); |
| 1901 | assert_eq!(p.abandoned, [3], "dropped, at most once"); |
| 1902 | } |
| 1903 | from_outside(ctx, move |sched| wake_sender(sched, stale)); |
| 1904 | uninstall(); |
| 1905 | } |
| 1906 | UNIT |
| 1907 | } |
| 1908 | |
| 1909 | #[test] |
| 1910 | fn a_sender_killed_while_parked_drops_its_message() { |
| 1911 | for threads in THREADS { |
| 1912 | assert_eq!( |
| 1913 | run_root(threads, kills_a_parked_sender), |
| 1914 | Exit::Normal, |
| 1915 | "{threads} threads" |
| 1916 | ); |
| 1917 | } |
| 1918 | } |