// `unlink`: once it returns, neither side's exit reaches the other // through the link, until a new `link`; an exit signal already in the // mailbox stays. Each exit is followed by a witness, linked to the // exiting actor after the others: its exit signal comes after theirs. import std/io import std/process. { type ExitReason , type Pid , type Subject , type proc , Crashed , ExitSignal , Killed , Normal , Shutdown } fn reason :: r: ExitReason -> String { match r { Normal -> "normal" , Killed -> "killed" , Crashed(m) -> "crashed: " <> m , Shutdown -> "shutdown" } } /// An actor that waits until told, then crashes with `why`. fn doomed :: why: String -> (Pid, Subject<()>) / { let ready = process.subject () let pid = { use <- process.spawn let go = process.subject () process.send ready go process.receive_forever go panic why } (pid, process.receive_forever ready) } /// Link `linked`, if any, and unlink it at once. fn link_and_unlink :: linked: Option -> () / { match linked { Some(pid) -> { process.link pid process.unlink pid } , None -> () } } /// An actor that links and unlinks `linked`, then waits until told and /// crashes with `why`. fn unlinker :: linked: Option why: String -> (Pid, Subject<()>) / { let ready = process.subject () let pid = { use <- process.spawn link_and_unlink linked let go = process.subject () process.send ready go process.receive_forever go panic why } (pid, process.receive_forever ready) } /// An actor that does not trap exits, links and unlinks `linked`, then /// answers every question on the returned subject. fn bystander :: linked: Option -> (Pid, Subject>) / { let ready = process.subject () let pid = { use <- process.spawn link_and_unlink linked let questions = process.subject () process.send ready questions answer questions } (pid, process.receive_forever ready) } fn answer :: questions: Subject> -> () / { let reply = process.receive_forever questions process.send reply "alive" answer questions } /// Whether the actor of `questions` still answers. fn alive :: questions: Subject> -> String / { let reply = process.subject () process.send questions reply match process.receive reply 1000 { Some(s) -> s, None -> "gone" } } /// An actor that traps exits, linked to `pid` once this returns: the /// subject its first exit signal's reason goes to. fn witness :: pid: Pid -> Subject / { let told = process.subject () let linked = process.subject () let _ = { use <- process.spawn process.trap_exits True process.link pid process.send linked () let ExitSignal(_, why) = process.receive_forever (process.exits ()) process.send told (reason why) } process.receive_forever linked told } /// Tell `pid` to crash, and wait until its witness is told. fn crash :: pid: Pid go: Subject<()> -> () / { let seen = witness pid process.send go () let _ = process.receive_forever seen } /// What waits on `exits`. fn pending :: exits: Subject -> String / { match process.receive exits 0 { Some(ExitSignal(_, why)) -> "an exit signal, " <> reason why , None -> "nothing" } } pub fn main :: -> () / { process.trap_exits True let exits = process.exits () // The other's crash, after an unlink: no exit signal here. let (pid, go) = doomed "boom" process.link pid process.unlink pid crash pid go io.println ("the other's crash: " <> pending exits) // A caller that does not trap exits lives on. let (pid, go) = doomed "boom" let (_, questions) = bystander (Some(pid)) crash pid go io.println ("the other's crash, not trapping: " <> alive questions) // The caller's crash does not reach the other. let (other, questions) = bystander None let (pid, go) = unlinker (Some(other)) "the caller crashed" crash pid go io.println ("the caller's crash: the other is " <> alive questions) // Linked again, the link works. let (pid, go) = doomed "again" process.link pid process.unlink pid process.link pid crash pid go let ExitSignal(from, why) = process.receive_forever exits io.println ("linked again: " <> reason why <> " from it: {from == pid}") // No link, a dead pid, itself: nothing. let (live, go) = unlinker None "later" process.unlink live process.unlink pid process.unlink (process.self ()) crash live go io.println ("no link, a dead pid, itself: " <> pending exits) // An exit signal that arrived before the unlink stays. let (pid, go) = doomed "early" process.link pid crash pid go process.unlink pid io.println ("arrived before: " <> pending exits) } //> the other's crash: nothing //> the other's crash, not trapping: alive //> the caller's crash: the other is alive //> linked again: crashed: again from it: True //> no link, a dead pid, itself: nothing //> arrived before: an exit signal, crashed: early