<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>sunshowers</title><link>https://sunshowers.io/</link><description>Recent content on sunshowers</description><generator>Hugo</generator><language>en-us</language><copyright>© Rain 2020-present. Licensed under
&lt;a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0"&gt;CC BY-NC-SA 4.0&lt;/a&gt;
unless marked otherwise.
&lt;!--# Theme based on Terminal by panr, and used under the MIT license: https://github.com/panr/hugo-theme-terminal --&gt;</copyright><lastBuildDate>Sat, 14 Nov 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://sunshowers.io/index.xml" rel="self" type="application/rss+xml"/><item><title>Designing nextest's install scripts</title><link>https://sunshowers.io/posts/nextest-install-scripts/</link><pubDate>Sat, 14 Nov 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/nextest-install-scripts/</guid><description>&lt;figure class="floatright narrow" &gt;&#10; &#10; &lt;a href="./images/nextest-install-scripts-card.png"&gt;&#10; &#10; &lt;img src="./images/nextest-install-scripts-card.png" alt="The cargo-nextest home page, with the headline &amp;#39;A next-generation test runner for Rust&amp;#39; above tabs for &amp;#39;Linux, macOS, WSL&amp;#39; and &amp;#39;Windows (PowerShell)&amp;#39;. The selected tab shows the command &amp;#39;curl -LsSf https://get.nexte.st/install.sh | sh&amp;#39;." /&gt;&#10; &#10; &lt;/a&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Now leading with install scripts!&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Four and a half years after I released the first version of &lt;a href="https://nexte.st/"&gt;cargo-nextest&lt;/a&gt;, it now has install scripts! This has been a long time coming, and I&amp;rsquo;m really happy to finally have support for them.&lt;/p&gt;</description></item><item><title>Contra "Glue work considered harmful"</title><link>https://sunshowers.io/posts/glue-work/</link><pubDate>Thu, 12 Nov 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/glue-work/</guid><description>&lt;p&gt;Sean Goedecke, &lt;a href="https://www.seangoedecke.com/glue-work-considered-harmful/"&gt;&lt;em&gt;Glue work considered harmful&lt;/em&gt;&lt;/a&gt; (emphasis here, and in the rest of the quotes, in original):&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;So why doesn’t glue work get you promoted, if it’s so crucial to shipping projects? Are companies stupid? Are they deliberately leaving value on the table? No, I don’t think so. Companies don’t reward glue work because &lt;strong&gt;they don’t want you prioritizing it.&lt;/strong&gt; And they don’t want you prioritizing it because they want you shipping features. Glue work is hard. If you’re capable of doing glue work well, they want you to use that ability to deliver projects instead of improving general efficiency.&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>*sshole, or when are we finally going to get rid of C?</title><link>https://sunshowers.io/posts/sshole/</link><pubDate>Fri, 06 Nov 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/sshole/</guid><description>&lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/4-aaaaaa.jpg" alt="The meme of a happy person and a sad person on a bus, the sad person captioned with &amp;#39;4&amp;#39; and the happy person with a string of 257 As." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Original art by Genildo Ronchi.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Yet another day, &lt;a href="https://sshhole.com/"&gt;yet another vulnerability&lt;/a&gt; in core C code. This one happened due to two different conditions using the same sentinel value:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;On Unix, a password hash of &lt;code&gt;&amp;quot;*&amp;quot;&lt;/code&gt; has meant a locked account since the very early days.&lt;/li&gt;&#10;&lt;li&gt;Since 2012, musl libc&amp;rsquo;s &lt;code&gt;crypt&lt;/code&gt; function has returned the string &lt;code&gt;&amp;quot;*&amp;quot;&lt;/code&gt; for a password that&amp;rsquo;s too long for it to hash.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The two points combined, along with sshd hashing login attempts for locked accounts to avoid timing leaks, mean that on musl systems without PAM (like, oh, &lt;em&gt;the official Alpine AWS image&lt;/em&gt;), an unauthenticated user now gets a (possibly root!) shell on your computer. See &lt;a href="https://www.alpinelinux.org/posts/2026-11-05-openssh-password-authentication-bypass/"&gt;this Alpine advisory&lt;/a&gt; for more details.&lt;/p&gt;</description></item><item><title>More on Anthropic's Scion ripping off Mononoke</title><link>https://sunshowers.io/posts/anthropic-mononoke-2/</link><pubDate>Thu, 15 Oct 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/anthropic-mononoke-2/</guid><description>&lt;p&gt;&lt;em&gt;Update: Nick G. from Anthropic has &lt;a href="https://news.ycombinator.com/item?id=50107559"&gt;responded on Hacker News&lt;/a&gt;, and &lt;a href="https://www.anthropic.com/engineering/redesigning-source-control#footnote-2"&gt;sufficient attribution&lt;/a&gt; has been added to Anthropic&amp;rsquo;s blog post. I stand by my posts as of the time I wrote them.&lt;/em&gt;&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;Discuss on &lt;a href="https://news.ycombinator.com/item?id=50106738"&gt;Hacker News&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;In response to &lt;a href="./posts/anthropic-mononoke/"&gt;yesterday&amp;rsquo;s post&lt;/a&gt; about Anthropic&amp;rsquo;s Scion ripping off Mononoke, I received a number of questions and concerns. Here&amp;rsquo;s a followup to answer the big ones.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;This was a ripoff.&lt;/strong&gt; My post yesterday was trying to be as charitable as possible given what I knew, and in particular that the similarity to Mononoke was mostly unintentional and the lack of credit was the result of not doing due diligence. However, after publishing the post I privately received confirmation that Scion intentionally and blatantly ripped off Mononoke, and in particular that there was overlap in personnel. There is no chance that this was an unintentional homage. This is something I had a hunch about but was not comfortable saying yesterday, given what I knew back then.&lt;/p&gt;</description></item><item><title>Anthropic's Scion is heavily inspired by Mononoke</title><link>https://sunshowers.io/posts/anthropic-mononoke/</link><pubDate>Wed, 14 Oct 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/anthropic-mononoke/</guid><description>&lt;p&gt;&lt;em&gt;Update 2026-10-15: I wrote &lt;a href="./posts/anthropic-mononoke-2/"&gt;a followup&lt;/a&gt; to answer questions and concerns.&lt;/em&gt;&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;Discuss on &lt;a href="https://news.ycombinator.com/item?id=50094694"&gt;Hacker News&lt;/a&gt; or &lt;a href="https://lobste.rs/s/plcgps/anthropic_s_scion_is_heavily_inspired_by"&gt;Lobsters&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Here is Anthropic &lt;a href="https://www.anthropic.com/engineering/redesigning-source-control"&gt;describing&lt;/a&gt; (&lt;a href="https://ghostarchive.org/archive/hCFVA"&gt;archive link&lt;/a&gt;) yesterday how their new version control system, Scion, uses a concept called &amp;ldquo;derived data&amp;rdquo; to make operations like blames more efficient:&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;a href="./images/scion-derived-data.png"&gt;&#10; &#10; &lt;img src="./images/scion-derived-data.png" alt="A changeset records only what’s changed, so on its own, it can’t quickly answer questions like who last edited a line or which commits affected a file. To answer those, Scion builds additional data structures from the changesets, including dedicated structures for file blame and path history and skip lists for the commit graph. We call everything that isn’t directly part of the changeset “derived data,” and we compute it all through one generic mechanism. Each type of derived data is defined recursively, so its value for a changeset is computed from its values for that changeset’s parents. This mechanism has three benefits. First, we can easily add a new type of derived data when a new data structure would make some operation faster. Second, for each type, we can choose whether to compute it eagerly or defer the work, so we avoid materializing data we don’t need. And third, because the client never sees the internals of these data structures, we can optimize them without changing the client. We’ve added new types of derived data many times as the system has grown, such as new indices for performing copy detection or path-based file search." /&gt;&#10; &#10; &lt;/a&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;How Anthropic describes Scion&amp;rsquo;s derived data.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Here is me, back in 2022, describing in my Oxide candidate materials how the &lt;a href="https://github.com/facebook/sapling/tree/main/eden/mononoke"&gt;Mononoke&lt;/a&gt; source control server I helped build uses a concept called &amp;ldquo;derived data&amp;rdquo; to make operations like blames more efficient:&lt;/p&gt;</description></item><item><title>iddqd, or the hardest kind of unsafe Rust</title><link>https://sunshowers.io/posts/iddqd-unsafe/</link><pubDate>Tue, 02 Jun 2026 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/iddqd-unsafe/</guid><description>&lt;p&gt;&lt;em&gt;Mirrored from the canonical version on the &lt;a href="https://oxide.computer/blog/iddqd-unsafe"&gt;Oxide blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Update 2026-06-16&lt;/em&gt;: I talked about this post on &lt;em&gt;Oxide and Friends&lt;/em&gt;, in an episode titled &lt;em&gt;The Hardest Kind of Unsafe Rust&lt;/em&gt;. Check it out on &lt;a href="https://www.youtube.com/watch?v=mdcu3dKMjyg"&gt;YouTube&lt;/a&gt;, &lt;a href="https://oxide-and-friends.transistor.fm/episodes/the-hardest-kind-of-unsafe-rust"&gt;Transistor&lt;/a&gt;, or wherever else you get your podcasts.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;I&amp;rsquo;m the main author of &lt;a href="https://docs.rs/iddqd"&gt;&lt;code&gt;iddqd&lt;/code&gt;&lt;/a&gt;, a Rust library for maps (named after the &lt;a href="https://doomwiki.org/wiki/Doom_cheat_codes#All_Doom_engine_versions"&gt;Doom cheat code&lt;/a&gt;) where keys are borrowed from values. At &lt;a href="https://oxide.computer/"&gt;Oxide&lt;/a&gt; we use it extensively in &lt;a href="https://github.com/oxidecomputer/omicron"&gt;Omicron&lt;/a&gt;, our control plane—the software that &lt;a href="https://rfd.shared.oxide.computer/rfd/0048"&gt;sits&lt;/a&gt; at the heart of every Oxide rack, provisions resources like compute and storage for our customers, and ensures the rack stays up and running over time. &lt;code&gt;iddqd&lt;/code&gt; maintains in-memory indexes of the kinds of large records that show up everywhere in a system like that, such as &lt;a href="https://github.com/oxidecomputer/omicron/blob/2e319eb3b9b9fcffb8cee71cffb9cd13397cb316/sled-agent/config-reconciler/src/internal_disks.rs#L602-L637"&gt;disks&lt;/a&gt; or &lt;a href="https://github.com/oxidecomputer/omicron/blob/2e319eb3b9b9fcffb8cee71cffb9cd13397cb316/nexus/types/src/inventory.rs#L682-L717"&gt;sled inventories&lt;/a&gt;. As a result, it must be correct: if it misbehaves, our control plane can malfunction in ways that are unpredictable and hard to diagnose.&lt;/p&gt;</description></item><item><title>In defense of lock poisoning in Rust</title><link>https://sunshowers.io/posts/on-poisoning/</link><pubDate>Tue, 02 Dec 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/on-poisoning/</guid><description>&lt;p&gt;There&amp;rsquo;s recently been some discussion about the benefits and downsides of lock (mutex) poisoning in Rust, spurred by &lt;a href="https://github.com/rust-lang/rust/issues/149359"&gt;a recent proposal&lt;/a&gt; to make the default mutex non-poisoned, i.e. &lt;strong&gt;silently unlock on panic&lt;/strong&gt; (see also, &lt;a href="https://news.ycombinator.com/item?id=46036883"&gt;recent discussion on Hacker News&lt;/a&gt;). As a passionate defender of lock poisoning, I thought I&amp;rsquo;d gather and write about my thoughts on this matter.&lt;/p&gt;&#10;&lt;p&gt;To summarize, I believe:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Unexpected cancellations in critical sections cause real harm to system correctness.&lt;/li&gt;&#10;&lt;li&gt;Lock poisoning is an important part of ensuring the correctness of critical sections in Rust.&lt;/li&gt;&#10;&lt;li&gt;Poisoning applies more generally than mutexes, and providing an easy way to track that (via e.g. a &lt;code&gt;Poison&amp;lt;T&amp;gt;&lt;/code&gt; wrapper) is valuable.&lt;/li&gt;&#10;&lt;li&gt;While there is conceptual elegance in separating out locking from poisoning on panic, the importance of lock poisoning overrides these concerns.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="what-is-poisoning"&gt;What is poisoning?&lt;/h2&gt;&#10;&lt;p&gt;Rust, like most multithreaded languages, has &lt;a href="https://doc.rust-lang.org/std/sync/struct.Mutex.html"&gt;mutexes&lt;/a&gt;: a construct to ensure that a particular piece of data can only be accessed by one thread at a time. The way mutexes work in Rust is particularly well-considered:&lt;/p&gt;</description></item><item><title>`SocketAddrV6` is not roundtrip serializable</title><link>https://sunshowers.io/posts/socketaddrv6-not-roundtrip/</link><pubDate>Tue, 04 Nov 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/socketaddrv6-not-roundtrip/</guid><description>&lt;p&gt;A few weeks ago at &lt;a href="https://oxide.computer/"&gt;Oxide&lt;/a&gt;, we encountered a bug where a particular, somewhat large, data structure was erroring on serialization to JSON via &lt;a href="https://serde.rs/"&gt;&lt;code&gt;serde&lt;/code&gt;&lt;/a&gt;. The problem was that JSON only supports map keys that are strings&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, and the data structure had an infrequently-populated map with keys that were more complex than that&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;We fixed the bug, but a concern still remained: what if some other map that was empty most of the time had a complex key in it? The easiest way to guard against this is by generating random instances of the data structure and attempting to serialize them, checking that this operation doesn&amp;rsquo;t panic. The most straightforward way to do this is with &lt;em&gt;property-based testing&lt;/em&gt;, where you define:&lt;/p&gt;</description></item><item><title>Cancelling async Rust</title><link>https://sunshowers.io/posts/cancelling-async-rust/</link><pubDate>Fri, 03 Oct 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/cancelling-async-rust/</guid><description>&lt;div class="sectioned-post"&gt;&#10;&lt;p&gt;&lt;em&gt;This is an edited, written version of my RustConf 2025 talk about cancellations in async Rust. Like &lt;a href="./posts/beyond-ctrl-c-signals/"&gt;the written version of my RustConf 2023 talk&lt;/a&gt;, I&amp;rsquo;ve tried to retain the feel of a talk while making it readable as a standalone blog entry. Some links:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://www.youtube.com/watch?v=zrv5Cy1R7r4"&gt;Video of the talk&lt;/a&gt; on YouTube.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://docs.google.com/presentation/u/1/d/e/2PACX-1vTMc4EdHRf6ulz-xaAhZFGZwxJ7jPQgYWczT6pEIvwfXILV4ZEgMdLuoRh70bgh9SP7mxblEnyXuZD0/pub?start=false&amp;amp;loop=false&amp;amp;delayms=60000"&gt;Slides&lt;/a&gt; on Google Slides.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://github.com/sunshowers/cancelling-async-rust"&gt;Repository with links and notes&lt;/a&gt; on GitHub.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://lwn.net/Articles/1036924/"&gt;Coverage on Linux Weekly News&lt;/a&gt;.&lt;/em&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"&gt;&#10;&#9;&#9;&#9;&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/zrv5Cy1R7r4?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"&gt;&lt;/iframe&gt;&#10;&#9;&#9;&lt;/div&gt;&#10;&#10;&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;&#10;&lt;p&gt;Let’s start with a simple example – you decide to read from a channel in a loop and gather a bunch of messages:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;rx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;All good, nothing wrong with this, but you realize sometimes the channel is empty for long periods of time, so you add a timeout and print a message:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;::&lt;span class="n"&gt;from_secs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;rx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;no messages for 5 seconds&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There&amp;rsquo;s nothing wrong with this code—it behaves as expected.&lt;/p&gt;&#10;&lt;p&gt;Now you realize you need to write a bunch of messages out to a channel in a loop:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;next_message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;sent successfully&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But sometimes the channel gets too full and blocks, so you add a timeout and print a message:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;next_message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;::&lt;span class="n"&gt;from_secs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;sent successfully&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;no space for 5 seconds&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It turns out that &lt;em&gt;this&lt;/em&gt; code is often incorrect, because not all messages make their way to the channel.&lt;/p&gt;&#10;&lt;p&gt;Hi, I’m Rain, and this post is about cancelling async Rust. This post is split into three parts:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="./posts/cancelling-async-rust/#1-what-is-cancellation"&gt;&lt;em&gt;What is cancellation?&lt;/em&gt;&lt;/a&gt; It&amp;rsquo;s an extremely powerful part of async Rust but also one that is very hard to reason thoroughly about.&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/cancelling-async-rust/#2-analyzing-cancellations"&gt;&lt;em&gt;Analyzing cancellations:&lt;/em&gt;&lt;/a&gt; Going deep into their mechanics and providing some helpful ways to think about them.&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/cancelling-async-rust/#3-what-can-be-done"&gt;&lt;em&gt;What can be done?&lt;/em&gt;&lt;/a&gt; Solutions, including practical guidance, and real bugs we’ve found and fixed in production codebases.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Before we begin, I want to lay my cards on the table – I really love async Rust!&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/beyond-ctrl-c.jpg" alt="Me speaking at RustConf 2023. Beyond Ctrl-C: The dark corners of Unix signal handling." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Me speaking at RustConf 2023.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;I gave &lt;a href="./posts/beyond-ctrl-c-signals/"&gt;a talk&lt;/a&gt; at RustConf a couple years ago talking about how async Rust is a great fit for signal handling in complex applications.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;I’m also the author of &lt;a href="https://nexte.st/"&gt;cargo-nextest&lt;/a&gt;, a next-generation test runner for Rust, where async Rust is the best way I know of to express some really complex algorithms that I wouldn’t know how to express otherwise. I wrote &lt;a href="./posts/nextest-and-tokio/"&gt;a blog post&lt;/a&gt; about this a few years ago.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Now, I work at &lt;a href="https://oxide.computer/"&gt;Oxide Computer Company&lt;/a&gt;, where we make &lt;em&gt;cloud-in-a-box computers&lt;/em&gt;. We make vertically integrated systems where you provide power and networking on one end, and the software you want to run on the other end, and we take care of everything in between.&lt;/p&gt;&#10;&lt;p&gt;Of course, we use Rust everywhere, and in particular we use async Rust extensively for our higher-level software, such as &lt;a href="https://github.com/oxidecomputer/crucible"&gt;storage&lt;/a&gt;, &lt;a href="https://github.com/oxidecomputer/maghemite"&gt;networking&lt;/a&gt; and the &lt;a href="https://github.com/oxidecomputer/omicron/"&gt;customer-facing management API&lt;/a&gt;. But along the way we’ve encountered a number of issues around async cancellation, and a lot of this post is about what we learned along the way.&lt;/p&gt;&#10;&lt;h2 id="1-what-is-cancellation"&gt;1. What is cancellation?&lt;/h2&gt;&#10;&lt;p&gt;What does cancellation mean? Logically, a cancellation is exactly what it sounds like: you start some work, and then change your mind and decide to stop doing that work.&lt;/p&gt;&#10;&lt;p&gt;As you might imagine this is a useful thing to do:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;You may have started a large download or a long network request&lt;/li&gt;&#10;&lt;li&gt;Maybe you’ve started reading a file, similar to the &lt;code&gt;head&lt;/code&gt; command.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;But then you change your mind: you want to cancel it rather than continue it to completion.&lt;/p&gt;&#10;&lt;h3 id="cancellations-in-synchronous-rust"&gt;Cancellations in synchronous Rust&lt;/h3&gt;&#10;&lt;p&gt;Before we talk about async Rust, it’s worth thinking about how you’d do cancellations in synchronous Rust.&lt;/p&gt;&#10;&lt;p&gt;One option is to have some kind of &lt;em&gt;flag&lt;/em&gt; you periodically check, maybe stored in an atomic:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;should_cancel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ordering&lt;/span&gt;::&lt;span class="n"&gt;Relaxed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;expensive_operation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#10;&lt;li&gt;The code that wishes to perform the cancellation can set that flag.&lt;/li&gt;&#10;&lt;li&gt;Then, the code which checks that flag can exit early.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This approach is fine for smaller bits of code but doesn’t really scale well to large chunks of code since you’d have to sprinkle these checks everywhere.&lt;/p&gt;&#10;&lt;p&gt;A related option, if you’re working with a framework as part of your work, is to &lt;em&gt;panic with a special payload&lt;/em&gt; of some kind.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;If that feels strange to you, you’re not alone! But the &lt;a href="https://github.com/salsa-rs/salsa"&gt;Salsa&lt;/a&gt; framework for incremental computation, used by—among other things—&lt;a href="https://github.com/rust-lang/rust-analyzer"&gt;rust-analyzer&lt;/a&gt;, uses this approach.&lt;/li&gt;&#10;&lt;li&gt;Something I learned recently was that this only works on build targets which have a notion of &lt;a href="https://doc.rust-lang.org/std/panic/fn.catch_unwind.html"&gt;panic unwinding&lt;/a&gt;, or being able to bubble up the panic. Not all platforms support this, and in particular, &lt;a href="https://webassembly.org/"&gt;Wasm&lt;/a&gt; doesn&amp;rsquo;t. This means that Salsa cancellations don’t work if you build rust-analyzer for Wasm.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;A third option is to &lt;em&gt;kill the whole process&lt;/em&gt;. This is a &lt;em&gt;very heavyweight&lt;/em&gt; approach, but an effective one in case you spawn processes to do your work.&lt;/p&gt;&#10;&lt;p&gt;Rather than kill the whole process, can you kill a single thread?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;While &lt;a href="https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread"&gt;some OSes have APIs&lt;/a&gt; to perform this action, they tend to warn very strongly against it. That’s because in general, most code is &lt;a href="./posts/nextest-process-per-test/#appendix"&gt;just not ready&lt;/a&gt; for a thread disappearing from underneath.&lt;/li&gt;&#10;&lt;li&gt;In particular, thread killing is not permitted by safe Rust, since it can cause serious corruption. For example, Rust mutexes would likely stay locked forever.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;All of these options are suboptimal or of limited use in some way. In general, the way I think about it is that there isn’t a &lt;em&gt;universal protocol&lt;/em&gt; for cancellation in synchronous Rust.&lt;/p&gt;&#10;&lt;p&gt;In contrast, there &lt;em&gt;is&lt;/em&gt; such a protocol in async Rust, and in fact cancellations are extraordinarily easy to perform in async Rust.&lt;/p&gt;&#10;&lt;p&gt;Why is that so? To understand that, let’s look at what a future is.&lt;/p&gt;&#10;&lt;h3 id="what-is-a-future"&gt;What is a future?&lt;/h3&gt;&#10;&lt;p&gt;Here’s a simple example of a future:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// This creates a state machine.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Nothing executes yet. `future` is just a struct in memory.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this future, you first perform a network request which returns some data, and then you process it.&lt;/p&gt;&#10;&lt;p&gt;The Rust compiler looks at this future and generates a state machine, which is just a struct or enum in memory:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// The compiler generates something like:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;MyFuture&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WaitingForNetwork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NetworkFuture&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WaitingForProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ProcessFuture&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// It&amp;#39;s just data, no running code!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you&amp;rsquo;ve written async Rust before the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords, you’ve probably written code like it by hand. It’s basically just an enum describing all the possible states the future can be in.&lt;/p&gt;&#10;&lt;p&gt;The compiler also generates an implementation of &lt;a href="https://doc.rust-lang.org/std/future/trait.Future.html"&gt;the &lt;code&gt;Future&lt;/code&gt; trait&lt;/a&gt; for this future:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MyFuture&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;Poll&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="bp"&gt;Self&lt;/span&gt;::&lt;span class="n"&gt;Output&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WaitingForNetwork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// etc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and when you call &lt;code&gt;.await&lt;/code&gt; on the future, it gets translated down to this underlying &lt;code&gt;poll&lt;/code&gt; function. It is only when &lt;code&gt;await&lt;/code&gt; or this &lt;code&gt;poll&lt;/code&gt; function is called that something actually happens.&lt;/p&gt;&#10;&lt;p&gt;Note that this is diametrically opposed to how async works in other languages like Go, JavaScript, or C#. In those languages, when you create a future to await on, it starts doing its thing, immediately, in the background:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// JavaScript: starts running immediately&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/api/data&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That’s regardless of whether you await it or not.&lt;/p&gt;&#10;&lt;p&gt;In Rust, this &lt;code&gt;get&lt;/code&gt; call does nothing until you actually call &lt;code&gt;.await&lt;/code&gt; on it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Rust: just data, does nothing!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;reqwest&lt;/span&gt;::&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;/api/data&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I know I sound a bit like a broken record here, but if you can take away one thing from this post, it would be that futures are passive, and completely inert until awaited or polled.&lt;/p&gt;&#10;&lt;h3 id="the-universal-protocol"&gt;The universal protocol&lt;/h3&gt;&#10;&lt;p&gt;So what does the universal protocol to cancel futures look like? It is simply to &lt;em&gt;drop&lt;/em&gt; the future, or to not await it, or poll it any more. Since a future is just a state machine, you can throw it away at any time the poll function isn’t actively being called.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;some_async_work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// cancelled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The upshot of all this is that any Rust future can be cancelled at any await point.&lt;/p&gt;&#10;&lt;p&gt;Given how hard cancellation tends to be in synchronous environments, the ability to easily cancel futures in async Rust is extraordinarily powerful—in many ways its greatest strength!&lt;/p&gt;&#10;&lt;p&gt;But there is a flip side, which is that cancelling futures is far, far too easy. This is for two reasons.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;First, it’s just way too easy to quietly drop a future. As we’re going to see, there are all kinds of code patterns that lead to silently dropping futures.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Now this wouldn’t be so bad, if not for the second reason: that cancellation of parent futures propagates down to child futures.&lt;/p&gt;&#10;&lt;p&gt;Because of Rust’s &lt;a href="https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html"&gt;single ownership model&lt;/a&gt;, child futures are owned by parent ones. If a parent future is dropped or cancelled, the same happens to the child.&lt;/p&gt;&#10;&lt;p&gt;To figure out whether a child future’s cancellation can cause issues, you have to look at its parent, and grandparent, and so on. Reasoning about cancellation becomes a very complicated &lt;em&gt;non-local operation&lt;/em&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="2-analyzing-cancellations"&gt;2. Analyzing cancellations&lt;/h2&gt;&#10;&lt;p&gt;I’m going to cover some examples in a bit, but before we do that I want to talk about a couple terms, some of which you might have seen references to already.&lt;/p&gt;&#10;&lt;h3 id="cancel-safety-and-cancel-correctness"&gt;Cancel safety and cancel correctness&lt;/h3&gt;&#10;&lt;p&gt;The first term is &lt;strong&gt;cancel safety&lt;/strong&gt;. You might have seen mentions of this in the Tokio documentation. Cancel safety, as generally defined, means the property of a future that can be cancelled (i.e. dropped) without any side effects.&lt;/p&gt;&#10;&lt;p&gt;For example, a &lt;a href="https://docs.rs/tokio/latest/tokio/time/fn.sleep.html"&gt;Tokio sleep future&lt;/a&gt; is cancel safe: you can just stop waiting on the sleep and it’s completely fine.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;time&lt;/span&gt;::&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// this has no side effects&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;An example of a future that is not cancel safe is &lt;a href="https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.Sender.html#method.send"&gt;Tokio’s MPSC send&lt;/a&gt;, which sends a message over a channel:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// message is lost!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If this future is dropped, the message is lost forever.&lt;/p&gt;&#10;&lt;p&gt;The important thing is that cancel safety is a &lt;em&gt;local property&lt;/em&gt; of an &lt;em&gt;individual future&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;But cancel safety is not all that one needs to care about. What actually matters is the context the cancellation happens in, or in other words whether the cancellation actually causes some kind of larger property in the system to be violated.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;For example, if you drop a future which sends a message, but for whatever reason you don’t care about the message any more, it’s not really a bug!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;To capture this I tend to use a different term called &lt;strong&gt;cancel correctness&lt;/strong&gt;, which I define as a &lt;em&gt;global property&lt;/em&gt; of &lt;em&gt;system correctness&lt;/em&gt; in the face of cancellations. (This isn’t a standard term, but it’s a framing I’ve found really helpful in understanding cancellations.)&lt;/p&gt;&#10;&lt;p&gt;When is cancel correctness violated? It requires three things:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;em&gt;The system has a cancel-unsafe future somewhere within it.&lt;/em&gt; As we’ll see, many APIs that are cancel-unsafe can be reworked to be cancel-safe. If there aren’t any cancel-unsafe futures in the system, then the system is cancel correct.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;em&gt;A cancel-unsafe future is actually cancelled.&lt;/em&gt; This may sound a bit trivial, but if cancel-unsafe futures are always run to completion, then the system can’t have cancel correctness bugs.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;em&gt;Cancelling the future violates some property of a system.&lt;/em&gt; This could be data loss as with &lt;code&gt;Sender::send&lt;/code&gt;, some kind of invariant violation, or some kind of cleanup that must be performed but isn&amp;rsquo;t.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;So a lot of making Rust async robust is about trying to tackle one of these three things.&lt;/p&gt;&#10;&lt;p&gt;I want to zoom in for a second on invariant violations and talk about an example of a Tokio API that is very prone to cancel correctness issues: &lt;a href="https://docs.rs/tokio/latest/tokio/sync/struct.Mutex.html"&gt;Tokio mutexes&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="the-pain-of-tokio-mutexes"&gt;The pain of Tokio mutexes&lt;/h3&gt;&#10;&lt;p&gt;The way Tokio mutexes work is: you create a mutex, you lock it which gives you mutable access to the data underneath, and then you unlock it by releasing the mutex.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Access guard.data, protected by the mutex...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you look at the &lt;a href="https://docs.rs/tokio/latest/tokio/sync/struct.Mutex.html#method.lock"&gt;&lt;code&gt;lock&lt;/code&gt; function&amp;rsquo;s documentation&lt;/a&gt;, in the “cancel safety” section it says:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to lock makes you lose your place in the queue.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Okay, so not totally cancel safe, but the only kind of unsafety is fairness, which doesn’t sound too bad.&lt;/p&gt;&#10;&lt;p&gt;But the problems lie in what you actually do with the mutex. In practice, most uses of mutexes are in order to &lt;em&gt;temporarily violate invariants&lt;/em&gt; that are otherwise upheld when a lock isn’t held.&lt;/p&gt;&#10;&lt;p&gt;I’ll use a real world example of a cancel correctness bug that we found at my job at Oxide: we had code to manage a bunch of data sent over by our computers, which we call sleds. The shared state was guarded by a mutex, and a typical operation was:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Obtain a lock on the mutex.&lt;/li&gt;&#10;&lt;li&gt;Obtain the sled-specific data by value, moving it to an invalid &lt;code&gt;None&lt;/code&gt; state.&lt;/li&gt;&#10;&lt;li&gt;Perform an action.&lt;/li&gt;&#10;&lt;li&gt;Set the sled-specific data back to the next valid state.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Here&amp;rsquo;s a rough sketch of what that looks like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// guard.data is Option&amp;lt;T&amp;gt;: Some to begin with&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// guard.data is now None&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;new_data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;process_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// guard.data is Some again&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is all well and good, but the problem is that the action being performed actually had an await point within it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// guard.data is Option&amp;lt;T&amp;gt;: Some to begin with&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// guard.data is now None&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// DANGER: cancellation here leaves data in None state!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;new_data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;process_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// guard.data is Some again&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If the code that operated on the mutex got cancelled at that await point, then the data would be stuck in the invalid &lt;code&gt;None&lt;/code&gt; state. Not great!&lt;/p&gt;&#10;&lt;p&gt;And keep in mind the non-local reasoning aspect: when doing this analysis, you need to look at the whole chain of callers.&lt;/p&gt;&#10;&lt;h3 id="cancellation-patterns"&gt;Cancellation patterns&lt;/h3&gt;&#10;&lt;p&gt;Now that we’ve talked about some of the bad things that can happen during cancellations, it’s worth asking what kinds of code patterns lead to futures being cancelled.&lt;/p&gt;&#10;&lt;p&gt;The most straightforward example, and maybe a bit of a silly one, is that you create a future but simply forget to call &lt;code&gt;.await&lt;/code&gt; on it.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;some_async_work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// missing .await&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now Rust actually warns you if you don&amp;rsquo;t call &lt;code&gt;.await&lt;/code&gt; on the future:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;warning: unused implementer of `Future` that must be used&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; |&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;11 | some_async_work();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; | ^^^^^^^^^^^^^^^^^&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; |&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; = note: futures do nothing unless you `.await` or poll them&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But a code pattern I’ve sometimes made mistakes with is that the future returns a &lt;code&gt;Result&lt;/code&gt;, and you want to ignore the result so you assign it to an underscore like so:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;some_async_work&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// future returns Result&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If I forget to call &lt;code&gt;.await&lt;/code&gt; on the future, Rust doesn’t warn me about it at all, and then I’m left scratching my head about why this code didn’t run. I know this sounds really silly and basic, but I’ve made this mistake a bunch of times.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;(After my talk, it was pointed out to me that Clippy 1.67 and above have a &lt;a href="https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_future"&gt;&lt;code&gt;let_underscore_future&lt;/code&gt;&lt;/a&gt; warn-by-default lint for this. Hooray!)&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Another example of futures being cancelled is &lt;code&gt;try&lt;/code&gt; operations, such as Tokio’s &lt;a href="https://docs.rs/tokio/latest/tokio/macro.try_join.html"&gt;&lt;code&gt;try_join&lt;/code&gt; macro&lt;/a&gt;. For example:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;do_stuff_async&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;&amp;#39;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// async work&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;more_async_work&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;&amp;#39;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// more here&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;try_join!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;do_stuff_async&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;more_async_work&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you call &lt;code&gt;try_join&lt;/code&gt; with a bunch of futures, and all of them succeed, it’s all good. But if one of them fails, the rest simply get cancelled.&lt;/p&gt;&#10;&lt;p&gt;In fact, at Oxide we had a pretty bad bug around this: we had code to stop a bunch of services, all expressed as futures. We used &lt;code&gt;try_join&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="fm"&gt;try_join!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stop_service_a&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stop_service_b&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stop_service_c&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If one of these operations failed for whatever reason, we would stop running the code to wait for the other services to exit. Oops!&lt;/p&gt;&#10;&lt;p&gt;But perhaps the most well-known source of cancellations is Tokio&amp;rsquo;s &lt;a href="https://docs.rs/tokio/latest/tokio/macro.select.html"&gt;&lt;code&gt;select&lt;/code&gt; macro&lt;/a&gt;. Select is this incredibly beautiful operation. It is called with a set of futures, and it drives all of them forward concurrently:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;result1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle_result1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle_result2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Each future has a code block associated with it (above, &lt;code&gt;handle_result1&lt;/code&gt; and &lt;code&gt;handle_result2&lt;/code&gt;). If one of the futures completes, the corresponding code block is called. But also, all of the other futures are always cancelled!&lt;/p&gt;&#10;&lt;p&gt;For a variety of reasons, select statements in general, and &lt;em&gt;select loops&lt;/em&gt; in particular, are particularly prone to cancel correctness issues. So a lot of the documentation about cancel safety talks about select loops. But I want to emphasize here that select is not the &lt;em&gt;only&lt;/em&gt; source of cancellations, just a particularly &lt;em&gt;notable&lt;/em&gt; one.&lt;/p&gt;&#10;&lt;h2 id="3-what-can-be-done"&gt;3. What can be done?&lt;/h2&gt;&#10;&lt;p&gt;So, now that we’ve looked at all of these issues with cancellations, what can be done about it?&lt;/p&gt;&#10;&lt;p&gt;First, I want to break the bad news to you – there is &lt;em&gt;no general, fully reliable solution for this&lt;/em&gt; in Rust today. But in our experience there are a few patterns that have been successful at reducing the likelihood of cancellation bugs.&lt;/p&gt;&#10;&lt;p&gt;Going back to our definition of cancel correctness, there are three prongs all of which come together to produce a bug:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;A cancel-unsafe future exists&lt;/li&gt;&#10;&lt;li&gt;This cancel-unsafe future is cancelled&lt;/li&gt;&#10;&lt;li&gt;The cancellation violates a system property&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Most solutions we’ve come up with try and tackle one of these prongs.&lt;/p&gt;&#10;&lt;h3 id="making-futures-cancel-safe"&gt;Making futures cancel-safe&lt;/h3&gt;&#10;&lt;p&gt;Let’s look at the first prong: the system has a cancel-unsafe future somewhere in it. Can we use code patterns to make futures be cancel-safe? It turns out we can! I’ll give you two examples here.&lt;/p&gt;&#10;&lt;p&gt;The first is MPSC sends. Let’s come back to the example from earlier where we would lose messages entirely:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;next_message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;::&lt;span class="n"&gt;from_secs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;sent successfully&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;no space for 5 seconds&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Can we find a way to make this cancel safe?&lt;/p&gt;&#10;&lt;p&gt;In this case, yes, and we do so by breaking up the operation into two parts:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;::&lt;span class="n"&gt;from_secs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserve&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_message&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;sent successfully&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;no space for 5 seconds&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#10;&lt;li&gt;The first component is the operation to &lt;a href="https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.Sender.html#method.reserve"&gt;reserve a permit&lt;/a&gt; or slot in the channel. This is an initial async operation that’s cancel-safe.&lt;/li&gt;&#10;&lt;li&gt;The second is to generate, then &lt;a href="https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.Permit.html#method.send"&gt;send the message&lt;/a&gt;, which is an operation that becomes infallible.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;(I want to put an asterisk here that reserve is not entirely cancel-safe, since Tokio’s MPSC follows a first-in-first-out pattern and dropping the future means losing your place in line. Keep this in mind for now.)&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Update 2025-10-24: The code sample now calls &lt;code&gt;next_message&lt;/code&gt; after a permit has been reserved. Thanks to &lt;a href="https://lobste.rs/s/d4batn/cancelling_async_rust#c_9pm7e4"&gt;quad on Lobsters&lt;/a&gt; for the correction.&lt;/em&gt;&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;The second is with &lt;a href="https://docs.rs/tokio/latest/tokio/io/trait.AsyncWrite.html"&gt;Tokio’s &lt;code&gt;AsyncWrite&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;If you’ve written synchronous Rust you’re probably familiar with &lt;a href="https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all"&gt;the &lt;code&gt;write_all&lt;/code&gt; method&lt;/a&gt;, which writes an entire buffer out:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;::&lt;span class="n"&gt;io&lt;/span&gt;::&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In synchronous Rust, this is a great API. But within async Rust, the &lt;code&gt;write_all&lt;/code&gt; pattern is absolutely not cancel safe! If the future is dropped before completion, you have no idea how much of this buffer was written out.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;io&lt;/span&gt;::&lt;span class="n"&gt;AsyncWriteExt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Not cancel-safe!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But there’s an alternative API that is cancel-safe, called &lt;a href="https://docs.rs/tokio/latest/tokio/io/trait.AsyncWriteExt.html#method.write_all_buf"&gt;&lt;code&gt;write_all_buf&lt;/code&gt;&lt;/a&gt;. This API is carefully designed to enable the reporting of partial progress, and it doesn’t just accept a &lt;em&gt;buffer&lt;/em&gt;, but rather something that looks like a cursor on top of it:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;io&lt;/span&gt;::&lt;span class="n"&gt;AsyncWriteExt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;: &lt;span class="nc"&gt;io&lt;/span&gt;::&lt;span class="n"&gt;Cursor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write_all_buf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When part of the buffer is written out, the cursor is advanced by that number of bytes. So if you call &lt;code&gt;write_all_buf&lt;/code&gt; in a loop, you’ll be resuming from this partial progress, which works great.&lt;/p&gt;&#10;&lt;h3 id="not-cancelling-futures"&gt;Not cancelling futures&lt;/h3&gt;&#10;&lt;p&gt;Going back to the three prongs: the second prong is about actually cancelling futures. What code patterns can be used to not cancel futures? Here are a couple of examples.&lt;/p&gt;&#10;&lt;p&gt;The first one is, in a place like a select loop, resume futures rather than cancelling them each time. You’d typically achieve this by &lt;a href="https://doc.rust-lang.org/std/pin/"&gt;pinning a future&lt;/a&gt;, and then polling a mutable reference to that future. For example:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;::&lt;span class="n"&gt;pin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserve&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line hl"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;permit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;other_condition&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Coming back to our example of MPSC sends, the one asterisk with &lt;code&gt;reserve&lt;/code&gt; is that cancelling it makes you lose your place in line. Instead, if you pin the &lt;code&gt;reserve&lt;/code&gt; future and poll a mutable reference to it, you don’t lose your place in line.&lt;/p&gt;&#10;&lt;p&gt;(Does the difference here matter? It depends, but you can now have this strategy available to you.)&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;The second example is to use tasks. I mentioned earlier that futures are Rust are diametrically opposed to similar notions in languages like JavaScript. Well, there’s an alternative in async Rust that&amp;rsquo;s much closer to the JavaScript idea, and that’s &lt;a href="https://docs.rs/tokio/latest/tokio/task/"&gt;tasks&lt;/a&gt;.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Unlike futures which are driven by the caller, tasks are driven by the runtime (such as Tokio).&lt;/li&gt;&#10;&lt;li&gt;With Tokio, dropping a handle to a task does not cause it to be cancelled, which means they’re a good place to run cancel-unsafe code.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;A fun example is that at Oxide, we have an HTTP server called &lt;a href="https://docs.rs/dropshot"&gt;Dropshot&lt;/a&gt;. Previously, whenever an HTTP request came in, we’d use a future for it, and drop the future if the TCP connection was closed.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Before: Future cancelled on TCP close&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;handle_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This was really bad because future cancellations could happen due to the behavior of not just the parent future, but of a process that was running across a network! This is a rather extreme form of non-local reasoning.&lt;/p&gt;&#10;&lt;p&gt;We addressed this by spinning up a task for each HTTP request, and by running the code to completion even if the connection is closed:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// After: Task runs to completion&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="systematic-solutions"&gt;Systematic solutions?&lt;/h3&gt;&#10;&lt;p&gt;The last thing I want to say is that &lt;strong&gt;this sucks&lt;/strong&gt;!&lt;/p&gt;&#10;&lt;p&gt;The promise of Rust is that you don’t need to do this kind of non-local reasoning—that you can understand important behavior by looking at code directly around the behavior, then use the type system to scale that up to global correctness. Almost everything in Rust, from &lt;code&gt;&amp;amp;&lt;/code&gt; and &lt;code&gt;&amp;amp;mut&lt;/code&gt; to &lt;code&gt;unsafe&lt;/code&gt;, is geared towards making that possible. However, future cancellations fly directly in the face of that, and I think they’re probably the &lt;strong&gt;least Rusty part of Rust&lt;/strong&gt;. This is all really unfortunate.&lt;/p&gt;&#10;&lt;p&gt;Can we come up with something more systematic than this kind of ad-hoc reasoning?&lt;/p&gt;&#10;&lt;p&gt;There doesn’t exist anything in safe Rust today, but there are a few different ideas people have come up with. I wanted to give a nod to those ideas:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;em&gt;Async drop&lt;/em&gt; would let you run async code when a future is cancelled. This would handle some, though not all, of the cases we discussed today.&lt;/li&gt;&#10;&lt;li&gt;There’s also a couple different proposals for what are called &lt;em&gt;linear types&lt;/em&gt;, where you could force some code to be run on drop, or mark a particular future as non-cancellable (once it’s been created it must be driven to completion).&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;All of these options have really significant implementation challenges, though. This &lt;a href="https://without.boats/blog/asynchronous-clean-up/"&gt;blog post from boats&lt;/a&gt; covers some of these solutions, and the implementation challenges with them.&lt;/p&gt;&#10;&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;&#10;&lt;p&gt;In this post, we:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Saw that futures are passive&lt;/li&gt;&#10;&lt;li&gt;Introduced cancel safety and cancel correctness as concepts&lt;/li&gt;&#10;&lt;li&gt;Examined some bugs that can occur with cancellation&lt;/li&gt;&#10;&lt;li&gt;Looked at some recommendations you can use to mitigate the downsides of cancellation&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Some of the recommendations are:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Avoid Tokio mutexes&lt;/li&gt;&#10;&lt;li&gt;Rewrite APIs to make futures cancel-safe&lt;/li&gt;&#10;&lt;li&gt;Find ways to ensure that cancel-unsafe futures are driven to completion&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;There&amp;rsquo;s a very deep well of complexity here, a lot more than I can cover in one blog post:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Why are futures passive, anyway?&lt;/li&gt;&#10;&lt;li&gt;Cooperative cancellation: cancellation tokens&lt;/li&gt;&#10;&lt;li&gt;Actor model as an alternative to Tokio mutexes&lt;/li&gt;&#10;&lt;li&gt;Task aborts&lt;/li&gt;&#10;&lt;li&gt;Structured concurrency&lt;/li&gt;&#10;&lt;li&gt;Relationship to panic safety and mutex poisoning&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;If you’re curious about any of these, check out &lt;a href="https://github.com/sunshowers/cancelling-async-rust"&gt;this link&lt;/a&gt; where I’ve put together a collection of documents and blog posts about these concepts. In particular, I&amp;rsquo;d recommend reading these two Oxide RFDs:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://rfd.shared.oxide.computer/rfd/397"&gt;RFD 397 Challenges with async/await in the control plane&lt;/a&gt; by David Pacheco&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://rfd.shared.oxide.computer/rfd/400"&gt;RFD 400 Dealing with cancel safety in async Rust&lt;/a&gt; by myself&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;em&gt;Thank you for reading this post to the end! And thanks to many of my coworkers at Oxide for reviewing the talk and the RFDs linked above, and for suggestions and constructive feedback.&lt;/em&gt;&lt;/p&gt;&#10;&lt;/div&gt;</description></item><item><title>I didn't transition for the metaphysics</title><link>https://sunshowers.io/posts/transition-metaphysics/</link><pubDate>Wed, 14 May 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/transition-metaphysics/</guid><description>&lt;p&gt;&lt;em&gt;Content note for anti-trans, scientifically illiterate nonsense.&lt;/em&gt;&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/ryan-metaphysics.jpg" alt="A Twitter/X post by Benjamin Ryan (@benryanwriter) stating: &amp;#34;If they&amp;#39;re wondering why a philosophy professor would have any place authoring an HHS report about pediatric gender medicine, it&amp;#39;s because they haven&amp;#39;t read it. This field of medicine is grounded in metaphysics. So it is only fitting that it should be analyzed by a philosopher.&amp;#34; The post quotes a tweet from Florence Ashley (@floralashes.bsky.social) from May 1, 2025 that reads: &amp;#34;If you open the anti-trans HHS report&amp;#39;s appendix in NotePad, you&amp;#39;ll see that it was compiled by Alex Byrne. The appendix is a rigorous, scientific overview of systematic reviews. Alex Byrne is an MIT philosophy professor with no apparent background in science, let alone expertise in it.&amp;#34; Ashley&amp;#39;s tweet includes a screenshot of Notepad showing PDF metadata with &amp;#34;Alex Byrne&amp;#34; listed as creator. The post shows 2 retweets, 16 likes, and 423 views." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Ignorance on display.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;A couple weeks ago, I came across this rather remarkable Twitter post from Benjamin Ryan, a so-called writer about trans issues, commenting on a recent, &lt;a href="https://www.npr.org/sections/shots-health-news/2025/05/01/nx-s1-5383599/transgender-gender-affirming-care-trump-hhs"&gt;anonymously-authored&lt;/a&gt; HHS report.&lt;/p&gt;</description></item><item><title>Demystifying monads in Rust through property-based testing</title><link>https://sunshowers.io/posts/monads-through-pbt/</link><pubDate>Wed, 19 Feb 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/monads-through-pbt/</guid><description>&lt;div class="sectioned-post"&gt;&#10;&lt;p&gt;In programming pedagogy, &lt;a href="https://en.wikipedia.org/wiki/Monad_%28functional_programming%29"&gt;monads&lt;/a&gt; have a place as a mystical object from the functional programming world that&amp;rsquo;s hard to understand and even harder to explain. The stereotype about monad explanations is that they fall into two buckets: either comparisons to some kind of &lt;a href="https://blog.plover.com/prog/burritos.html"&gt;food item&lt;/a&gt;, or throwing complex mathematical jargon at you, &lt;a href="https://stackoverflow.com/questions/3870088/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem"&gt;&lt;em&gt;what&amp;rsquo;s the problem?&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;But monads aren&amp;rsquo;t esoteric or magical at all, nor do they only occur in functional programming. In essence, a monad is a &lt;em&gt;design pattern&lt;/em&gt; that allows you to chain together operations within a framework. Noticing monadic design can be quite helpful for programmers in any environment, particularly because it&amp;rsquo;s often &lt;strong&gt;undesirable&lt;/strong&gt;! In many situations, monads have observable tradeoffs, and sometimes (as here) we can even collect concrete data to back this up.&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m going to try and explain monads in a way that is:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Geared towards Rust developers&lt;/strong&gt;, with code samples in Rust, though I hope any sufficiently curious programmer can follow along&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Free of jargon:&lt;/strong&gt; no mathematical formalism whatsoever&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Without analogies&lt;/strong&gt; of any kind, and grounded in real programming problems&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Non-trivial:&lt;/strong&gt; focusing on a production-worthy example with objectively measurable implications&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Practical:&lt;/strong&gt; with advice all of us coders can use&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In other words, I&amp;rsquo;m going to try and write the monad tutorial that I personally would have appreciated when I was younger. And I&amp;rsquo;m going to start at a place that&amp;rsquo;s unconventional: through &lt;em&gt;property-based testing&lt;/em&gt;, where monads have profound performance characteristics.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Note: While this article&amp;rsquo;s primary goal is to explain monads, it also serves as a practical introduction to property-based testing and fault injection techniques. If you&amp;rsquo;re new to these, you&amp;rsquo;ll find an introduction to both alongside the monad explanation.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;This post consists of five sections:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="./posts/monads-through-pbt/#1-property-based-testing"&gt;&lt;em&gt;Property-based testing&lt;/em&gt;&lt;/a&gt; goes over the basics&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/monads-through-pbt/#2-drawing-the-rest-of-the-owl"&gt;&lt;em&gt;Drawing the rest of the owl&lt;/em&gt;&lt;/a&gt; talks about a complex scenario: using property-based testing to inject faults&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/monads-through-pbt/#3-integrated-shrinking"&gt;&lt;em&gt;Integrated shrinking&lt;/em&gt;&lt;/a&gt; shows how to reduce inputs of challenging complexity to smaller sizes&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/monads-through-pbt/#4-monads-finally"&gt;&lt;em&gt;Monads, finally&lt;/em&gt;&lt;/a&gt; is where we introduce monads in this context, and provide data for how costly they can be&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="./posts/monads-through-pbt/#5-rediscovering-structure"&gt;&lt;em&gt;Rediscovering structure&lt;/em&gt;&lt;/a&gt; discusses some ways to mitigate the tradeoffs of monads in property-based testing&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="1-property-based-testing"&gt;1. Property-based testing&lt;/h2&gt;&#10;&lt;p&gt;Testing is fundamentally about building models for how your code should behave, at just the right level of complexity: they should match the scope of what you&amp;rsquo;re testing, without going overboard and reinventing the whole system a second time.&lt;/p&gt;&#10;&lt;p&gt;The best explication of this general idea I&amp;rsquo;ve seen is in &lt;a href="https://kwarc.info/teaching/TDM/Borges.pdf"&gt;this piece&lt;/a&gt; by the great Argentinian writer &lt;a href="https://en.wikipedia.org/wiki/Jorge_Luis_Borges"&gt;Jorge Luis Borges&lt;/a&gt;:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;…In that Empire, the Art of Cartography attained such Perfection that the map of a single Province occupied the entirety of a City, and the map of the Empire, the entirety of a Province. In time, those Unconscionable Maps no longer satisfied, and the Cartographers Guilds struck a Map of the Empire whose size was that of the Empire, and which coincided point for point with it…”&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;—On Exactitude in Science&lt;/em&gt;, Jorge Luis Borges&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Nothing quite exemplifies testing-as-modeling like property-based testing—an approach where instead of specifying exact examples, you define models in terms of &lt;strong&gt;properties&lt;/strong&gt;, or invariants, that your code should satisfy. Then, you test your models against randomly generated inputs.&lt;/p&gt;&#10;&lt;p&gt;Let&amp;rsquo;s take a simple example of a sort function, say &lt;code&gt;my_sort&lt;/code&gt;, defined over a slice of integers:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;How should we go about testing it?&lt;/p&gt;&#10;&lt;p&gt;The most common way to do this is to list out a bunch of inputs and ensure they are correctly sorted, through &lt;em&gt;example-based tests&lt;/em&gt;.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[test]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;test_my_sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// More examples listed out.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Example-based tests are quite valuable, because they are easy to write and quite direct about what happens. But even in a simple example like sorting, it&amp;rsquo;s easy to imagine cases where your examples don&amp;rsquo;t quite cover every edge case.&lt;/p&gt;&#10;&lt;p&gt;How can more edge cases be covered? Well, one way to do so is to step back and ask, what is the sort trying to do? The goal of a sort function is to ensure that all the elements are in ascending order. Can we test that directly?&lt;/p&gt;&#10;&lt;p&gt;The first thing we&amp;rsquo;d need is to get some inputs to test with. All we care about is a list of numbers here, which &lt;em&gt;seems&lt;/em&gt; like it should be easy to generate using a random number generator.&lt;/p&gt;&#10;&lt;p&gt;So maybe we write something like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[test]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;test_my_sort_2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Run the test 1024 times.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate a random list of, say, 0 to 512 elements, with values&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// between 0 and 10000.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Call the sort function on it.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Check that all values are in ascending order.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;input {input:?} failed at index {i}, output {output:?}&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We now have a model of sorting that we&amp;rsquo;ve written down in code form: any pair of values must be in ascending order. (In this view, example-based tests are also simple models: for input X, the output should be Y.)&lt;/p&gt;&#10;&lt;p&gt;Now, we run the test, and…&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;thread &amp;#39;test_my_sort_2&amp;#39; panicked at tests/tests.rs:33:13:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;input [7496, 2087, 6900, 7927, 3840, 3065, 6472, 1186, 6464, 4512, 251, 5591, 3410, 2033, 5367, 2202, 5544, 2434, 6491, 8999, 9818, 2885, 8683, 1201, 6115, 2584, 2473, 6817, 5765, 5196, 9389, 5799, 9012, 293, 38, 1024, 9569, 4654, 7449, 7389, 8088, 5074, 3110, 938, 4944, 3859, 7368, 8978, 7524, 9503, 7406, 7591, 8213, 6445, 7000, 7354, 8967, 5549, 7935, 1866, 4048, 4043, 8905, 3154, 4771, 2364, 3982, 5088, 7317, 233, 3396, 1810, 3022, 9065, 454, 6181, 8257, 9598, 3982, 920, 5880, 4165, 4164, 930, 560, 9062, 5587, 6271, 5878, 2495, 9055, 3877, 4352, 1228, 8287, 8901, 3442, 373, 3635, 5316, 4423, 7688, 7919, 4465, 8991, 7043, 7696, 6875, 1478, 2428, 5127, 6809, 6175, 1415, 7263, 5145, 4153, 876, 1528, 6781, 5627, 6750, 3665, 2567, 6855, 141, 2144, 4491, 9121, 7982, 4131, 6337, 1926, 8797, 9382, 1702, 9559, 3910, 1715, 6661, 269, 4366, 6185, 5616, 365, 808, 4864, 3657, 9574, 3057, 7760, 6375, 2326, 7273, 6303, 7018, 8988, 6271, 988, 7796, 2390, 1689, 4279, 9586, 151, 9738, 3659, 7064, 1529, 8237, 4211, 2272, 8909, 7638] failed at index 173, output [38, 141, 151, 233, 251, 269, 293, 365, 373, 454, 560, 808, 876, 920, 930, 938, 988, 1024, 1186, 1201, 1228, 1415, 1478, 1528, 1529, 1689, 1702, 1715, 1810, 1866, 1926, 2033, 2087, 2144, 2202, 2272, 2326, 2364, 2390, 2428, 2434, 2473, 2495, 2567, 2584, 2885, 3022, 3057, 3065, 3110, 3154, 3396, 3410, 3442, 3635, 3657, 3659, 3665, 3840, 3859, 3877, 3910, 3982, 3982, 4043, 4048, 4131, 4153, 4164, 4165, 4211, 4279, 4352, 4366, 4423, 4465, 4491, 4512, 4654, 4771, 4864, 4944, 5074, 5088, 5127, 5145, 5196, 5316, 5367, 5544, 5549, 5587, 5591, 5616, 5627, 5765, 5799, 5878, 5880, 6115, 6175, 6181, 6185, 6271, 6271, 6303, 6337, 6375, 6445, 6464, 6472, 6491, 6661, 6750, 6781, 6809, 6817, 6855, 6875, 6900, 7000, 7018, 7043, 7064, 7263, 7273, 7317, 7354, 7368, 7389, 7406, 7449, 7496, 7524, 7591, 7638, 7688, 7696, 7760, 7796, 7919, 7927, 7935, 7982, 8088, 8213, 8237, 8257, 8287, 8683, 8797, 8901, 8905, 8909, 8967, 8978, 8988, 8991, 8999, 9012, 9055, 9062, 9065, 9121, 9382, 9389, 9503, 9559, 9569, 9574, 9586, 9598, 9818, 9738]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Whoops, looks like the function has a bug. (Scroll the above example to the right!)&lt;/p&gt;&#10;&lt;p&gt;This example is quite unhelpful and hard to understand! It is possible to use this as an input to debug with, but it is quite painful. If we could use automation to turn this test case into a much smaller one that can still reproduce the bug, debugging becomes significantly easier. The process of doing so is called test case &lt;strong&gt;shrinking&lt;/strong&gt; or &lt;strong&gt;reduction&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;p&gt;To recap—property-based testing consists of two components:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Test case &lt;strong&gt;generation&lt;/strong&gt; using a source of randomness.&lt;/li&gt;&#10;&lt;li&gt;On failing a test, &lt;strong&gt;shrinking&lt;/strong&gt; it down to a smaller, more understandable size.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="implementing-a-manual-shrinker"&gt;Implementing a manual shrinker&lt;/h3&gt;&#10;&lt;p&gt;What counts as &amp;ldquo;smaller&amp;rdquo;? For a list of numbers, ideally we&amp;rsquo;d be able to minimize both the number of items in the list and the integers themselves. This suggests an algorithm for how to write a shrinker by hand:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;First, try and minimize the size of the list using a binary search algorithm. For example:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Try the first half of the list against the function.&lt;/li&gt;&#10;&lt;li&gt;If that exhibits an error, attempt to recursively shrink this half.&lt;/li&gt;&#10;&lt;li&gt;If that doesn&amp;rsquo;t work, try it with the last half of the list.&lt;/li&gt;&#10;&lt;li&gt;If neither work or if the list has 1 or fewer elements, move on to the next step.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Once the list has been shrunk, start shrinking the elements within the list, applying binary search to each element.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;After you&amp;rsquo;re done writing this algorithm, you&amp;rsquo;re well on your way towards creating the original property-based testing library, &lt;a href="https://en.wikipedia.org/wiki/QuickCheck"&gt;QuickCheck&lt;/a&gt;. This approach has you write two functions: a &lt;em&gt;generator&lt;/em&gt; and a &lt;em&gt;shrinker&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;With this, you can get much more reasonable-looking output:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;input [58, 33] failed at index 1, output [58, 33]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And for relatively simple cases like lists of integers, this kind of shrinking works quite well!&lt;/p&gt;&#10;&lt;p&gt;But we&amp;rsquo;re not here to test simple cases. We&amp;rsquo;re here for the difficult ones.&lt;/p&gt;&#10;&lt;h2 id="2-drawing-the-rest-of-the-owl"&gt;2. Drawing the rest of the owl&lt;/h2&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/how-to-really-draw-an-owl.jpg" alt="A drawing guide with instructions for a front-facing owl face, a side/45° profile view, and a front-facing owl with body. Each series has 6 steps. The instructions are quite helpful, and a subversion of the &amp;#34;draw the rest of the owl&amp;#34; meme." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Subverting the &lt;a href="https://www.reddit.com/r/funny/comments/eccj2/how_to_draw_an_owl/"&gt;meme&lt;/a&gt;, we&amp;rsquo;re going to deal with real-world complexity in this section.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Most real-world sort implementations don&amp;rsquo;t just work over a list of integers. They&amp;rsquo;re written to be polymorphic over anything which can be sorted. In Rust parlance, this means anything that implements the &lt;a href="https://doc.rust-lang.org/std/cmp/trait.Ord.html"&gt;&lt;code&gt;Ord&lt;/code&gt;&lt;/a&gt; trait; and even if not, a &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by"&gt;custom comparator function&lt;/a&gt; can be provided.&lt;/p&gt;&#10;&lt;p&gt;But &lt;code&gt;Ord&lt;/code&gt; can be written by hand, and custom comparators are virtually always written by hand.&lt;/p&gt;&#10;&lt;p&gt;One immediate consequence is that it&amp;rsquo;s possible that the comparator function says two elements are equal, but they are actually different. In that case, should the order of elements be preserved?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;A sort implementation which preserves the order is called a &lt;strong&gt;stable sort&lt;/strong&gt;.&lt;/li&gt;&#10;&lt;li&gt;An implementation which does not is called an &lt;strong&gt;unstable sort&lt;/strong&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Unstable sorts tend to be faster than stable ones, and there are valid reasons for preferring each at different times. (The Rust standard library has separate implementations for &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html#method.sort"&gt;stable&lt;/a&gt; and &lt;a href="https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable"&gt;unstable&lt;/a&gt; sorts.)&lt;/p&gt;&#10;&lt;p&gt;Additionally, hand-written implementations mean &lt;strong&gt;users can make mistakes&lt;/strong&gt;! A production-grade sort algorithm must behave reasonably in the face of arbitrary user input, not just in the actual elements being sorted but also in the comparator function (full credit to &lt;a href="https://github.com/Voultapher"&gt;Lukas Bergdoll&lt;/a&gt; for his &lt;a href="https://github.com/Voultapher/sort-research-rs/blob/main/writeup/sort_safety/text.md"&gt;extensive research&lt;/a&gt; here):&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Ord safety:&lt;/strong&gt; Users can write a comparator that&amp;rsquo;s simply incorrect. An easy way is to introduce a difference between ordering and equality, for example by &lt;a href="https://doc.rust-lang.org/std/cmp/enum.Ordering.html#variant.Less"&gt;returning &lt;code&gt;Ordering::Less&lt;/code&gt;&lt;/a&gt; for two elements that are actually equal. Users could also return different answers for the same comparison when called at different times&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Panic safety:&lt;/strong&gt; The comparator can panic in the middle of execution. Since panics &lt;a href="https://doc.rust-lang.org/std/panic/fn.catch_unwind.html"&gt;can be caught&lt;/a&gt;, the input should be in some kind of valid state afterwards.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Observation safety:&lt;/strong&gt; If any of the inputs are mutated by the comparator, those mutations must be carried through to the final output. (With Rust, mutation through shared references is possible via &lt;a href="https://doc.rust-lang.org/book/ch15-05-interior-mutability.html"&gt;interior mutability&lt;/a&gt;, as seen in &lt;a href="https://doc.rust-lang.org/std/cell/struct.RefCell.html"&gt;&lt;code&gt;RefCell&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://doc.rust-lang.org/std/sync/struct.Mutex.html"&gt;&lt;code&gt;Mutex&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In these cases, completing the sort successfully becomes impossible. But it&amp;rsquo;s important that we leave the input in a reasonable state.&lt;/p&gt;&#10;&lt;p&gt;How do we go about testing this? Trying to think of all the different failure modes seems really hard! But property-based testing can address this need through randomized &lt;strong&gt;fault injection&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;p&gt;Let&amp;rsquo;s focus on Ord safety for now, with a comparator that flips around the result 20% of the time:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[derive(Clone, Copy, Debug)]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;OrdBehavior&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Regular&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Flipped&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;: &lt;span class="kt"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;: &lt;span class="nc"&gt;RefCell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ord&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;Ordering&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Get the next behavior from the list.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;borrow_mut&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;::&lt;span class="n"&gt;Regular&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;::&lt;span class="n"&gt;Flipped&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Flip the behavior.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To generate a &lt;code&gt;BadType&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;generate_bad_type&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate a value between 0 and 10000;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate a list of behaviors of length 0..128, where the elements are&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Regular 4/5 times and Flipped 1/5 times.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;: &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;: &lt;span class="nc"&gt;RefCell&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And to test this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[test]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;test_my_sort_3&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Run the test 1024 times.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate a list of BadTypes using generate_bad_type.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;: &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Call sort as before.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Sorting isn&amp;#39;t really well-defined in this case, but we can&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ensure two properties:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;//&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 1. my_sort doesn&amp;#39;t panic (implicitly checked by getting here)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 2. all the input values are still present in the output&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input_values&lt;/span&gt;: &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output_values&lt;/span&gt;: &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Sort the input and output values, and assert that they match.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input_values&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output_values&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_values&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output_values&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Our original approach continues to work well—that is, right until the test finds a bug and we need to shrink a failing input.&lt;/p&gt;&#10;&lt;h2 id="3-integrated-shrinking"&gt;3. Integrated shrinking&lt;/h2&gt;&#10;&lt;p&gt;How does one go about writing a shrinker for &lt;code&gt;Vec&amp;lt;BadType&amp;gt;&lt;/code&gt;? Doing so seemed relatively straightforward for a list of integers. But this is a list where the elements are pairs of an integer &lt;em&gt;and another list&lt;/em&gt;. Also:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;We&amp;rsquo;ve just tested Ord safety so far—once we&amp;rsquo;ve added fault injection for other kinds of safety, the complexity seems endless.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;And even more importantly, there isn&amp;rsquo;t a great way to compose smaller shrinkers together to form larger ones. Writing shrinkers is a lot of work already, and what&amp;rsquo;s the point if you have to keep doing all of it over and over?&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The practical result is that most of the time, writing a shrinker for types like &lt;code&gt;Vec&amp;lt;BadType&amp;gt;&lt;/code&gt; is quite difficult. And writing one is also technically optional, since:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;If the test passes, shrinkers are never invoked. Simply write correct code, and shrinking just isn&amp;rsquo;t an issue!&lt;/li&gt;&#10;&lt;li&gt;If the test fails, developers can debug using the original input. It&amp;rsquo;s painful but possible.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;All in all, given the choice of writing a shrinker by hand or just moving on with their lives, most developers tend to choose the latter&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;. Because of this, most modern property-based testing frameworks, like &lt;a href="https://docs.rs/proptest"&gt;&lt;strong&gt;proptest&lt;/strong&gt;&lt;/a&gt; in Rust, try and take care of shrinking for you through some notion of &lt;em&gt;integrated shrinking&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;The idea behind integrated shrinking is: When you generate a random input, you don&amp;rsquo;t just generate the value itself. You also generate some context that is helpful for reducing the size of the input.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;In proptest, this combined value and context is called a &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.ValueTree.html"&gt;&lt;strong&gt;value tree&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;Any implementation that accepts a random number generator and turns it into a value tree is called a &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.Strategy.html"&gt;&lt;strong&gt;strategy&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The proptest library comes with many different kinds of strategies that can be composed together to create more complex ones. To generate an instance of &lt;code&gt;OrdBehavior&lt;/code&gt;, we&amp;rsquo;re going to use two strategies:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/struct.Just.html"&gt;&lt;strong&gt;&lt;code&gt;Just&lt;/code&gt; strategy&lt;/strong&gt;&lt;/a&gt;, which &amp;ldquo;just&amp;rdquo; returns a single value.&lt;/li&gt;&#10;&lt;li&gt;The &lt;a href="https://docs.rs/proptest/latest/proptest/macro.prop_oneof.html"&gt;&lt;strong&gt;&lt;code&gt;prop_oneof&lt;/code&gt; strategy&lt;/strong&gt;&lt;/a&gt;, which generates values from one of a possible list of strategies, where each choice has a given probability. (A function that takes one or more strategies as input, and produces a strategy as its output, is called a &lt;strong&gt;combinator&lt;/strong&gt;.)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;generate_ord_behavior&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Strategy&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;prop_oneof!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 4/5 chance that the Regular implementation is generated.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Just&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;::&lt;span class="n"&gt;Regular&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 1/5 chance that it&amp;#39;s flipped.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Just&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrdBehavior&lt;/span&gt;::&lt;span class="n"&gt;Flipped&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To generate &lt;code&gt;BadType&lt;/code&gt;, we&amp;rsquo;re going to use &lt;code&gt;generate_ord_behavior&lt;/code&gt;, as well as:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;A &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.Strategy.html#impl-Strategy-for-Range%3Cu64%3E"&gt;&lt;strong&gt;range strategy&lt;/strong&gt;&lt;/a&gt; such as &lt;code&gt;0..10000_u64&lt;/code&gt;, which generates values within the range uniformly at random.&lt;/li&gt;&#10;&lt;li&gt;The &lt;a href="https://docs.rs/proptest/latest/proptest/collection/fn.vec.html"&gt;&lt;strong&gt;&lt;code&gt;vec&lt;/code&gt; combinator&lt;/strong&gt;&lt;/a&gt;, which accepts a strategy for elements and a size parameter.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;generate_bad_type&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Strategy&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Use the range strategy to generate values uniformly at random.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value_strategy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10000_&lt;/span&gt;&lt;span class="k"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Use the vec strategy to generate a list of behaviors: 0..128 items.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior_strategy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;generate_ord_behavior&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Now what? We need to compose these strategies together. With proptest,&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// the way to do this is to first create a tuple of strategies.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tuple_strategy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value_strategy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior_strategy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// A tuple of strategies is also a strategy! Generated values are a tuple&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// of constituents.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;//&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// With this in hand, we can use a function called `prop_map` to turn the&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// tuple into a BadType.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tuple_strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prop_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;: &lt;span class="nc"&gt;RefCell&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/proptest-composition.png" alt="A flowchart to show how smaller strategies flow into larger ones. See code comments in sample above for a full explanation." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Composing smaller strategies into a larger one for &lt;code&gt;BadType&lt;/code&gt;.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;You might be wondering where all the shrinking code is. It&amp;rsquo;s actually implemented on the corresponding value trees for each strategy:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Range strategies use binary search to make values smaller.&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;Just&lt;/code&gt; strategy doesn&amp;rsquo;t do any shrinking, since it just returns a single value.&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;prop_oneof&lt;/code&gt; combinator shrinks towards the beginning of the choices: in this case, &lt;code&gt;Flipped&lt;/code&gt; is shrunk into &lt;code&gt;Regular&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;vec&lt;/code&gt; combinator implements roughly the algorithm in &lt;a href="./posts/monads-through-pbt/#implementing-a-manual-shrinker"&gt;&lt;em&gt;Implementing a manual shrinker&lt;/em&gt;&lt;/a&gt; above.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;You can see how the base strategies get turned into successively larger ones through combinators like &lt;code&gt;prop_oneof&lt;/code&gt;. It&amp;rsquo;s very similar to &lt;a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html"&gt;&lt;code&gt;Iterator&lt;/code&gt;&lt;/a&gt;, where you can keep calling &lt;code&gt;.map&lt;/code&gt;, &lt;code&gt;.filter&lt;/code&gt;, &lt;code&gt;.enumerate&lt;/code&gt; and so on over and over&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;In my experience, composability across different scales is where this model shines. You can build bigger strategies out of smaller ones, up to a surprising amount of complexity. This means that your team can invest in a library of ever-more-complex strategies, and continue to derive value out of that library across everything from the smallest of unit tests to large integration tests.&lt;/p&gt;&#10;&lt;p&gt;But there is one &lt;em&gt;massive&lt;/em&gt; wrinkle with integrated shrinking. And that wrinkle is &lt;em&gt;exactly&lt;/em&gt; what monads are about.&lt;/p&gt;&#10;&lt;h2 id="4-monads-finally"&gt;4. Monads, finally&lt;/h2&gt;&#10;&lt;p&gt;In the previous few sections, we&amp;rsquo;ve built up all the context we need. We&amp;rsquo;re now going to look at the fundamental operation that introduces monadic composition to proptest: &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.Strategy.html#method.prop_flat_map"&gt;&lt;code&gt;prop_flat_map&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/map-shrink.png" alt="Diagram comparing prop_map behavior before and after shrinking. Left panel labeled &amp;#39;original&amp;#39; shows value=100 passing through prop_map(x²) to produce value=10000. Right panel labeled &amp;#39;shrink&amp;#39; shows value=50 passing through the same prop_map(x²) function to produce value=2500. An arrow connects the two panels, illustrating how prop_map preserves the same transformation during shrinking, just with smaller values." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;&lt;code&gt;prop_map&lt;/code&gt; from &lt;em&gt;x&lt;/em&gt; to &lt;em&gt;x²&lt;/em&gt;. When values are shrunk, they transparently pass through the map function.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;In the example above, there&amp;rsquo;s a function called &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.Strategy.html#method.prop_map"&gt;&lt;code&gt;prop_map&lt;/code&gt;&lt;/a&gt;, which we use to turn a tuple of components into a &lt;code&gt;BadType&lt;/code&gt; value. What happens when you try and shrink a value through a &lt;code&gt;prop_map&lt;/code&gt;? It&amp;rsquo;s very simple:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Attempt to get a smaller value from the underlying value tree.&lt;/li&gt;&#10;&lt;li&gt;Call the map function on the value.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;So &lt;code&gt;prop_map&lt;/code&gt; is just a conduit that values pass through: it simply maps a value to another value, and does not change the structure of the value tree in any way.&lt;/p&gt;&#10;&lt;p&gt;Now let&amp;rsquo;s say we want to test out pairs of &lt;code&gt;BadType&lt;/code&gt; instances, where the way the second &lt;code&gt;BadType&lt;/code&gt; is generated depends on the first. This is a situation where we don&amp;rsquo;t just want to map a value to another value—we need to generate a whole new &lt;em&gt;strategy&lt;/em&gt; based on a value.&lt;/p&gt;&#10;&lt;p&gt;This is a &lt;strong&gt;fundamental shift&lt;/strong&gt;:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;As above, &lt;code&gt;prop_map&lt;/code&gt; transforms a value into another, but preserves the original structure.&lt;/li&gt;&#10;&lt;li&gt;This new method, &lt;code&gt;prop_flat_map&lt;/code&gt;, goes well beyond that. Based on the value generated, it creates a brand new &lt;em&gt;strategy&lt;/em&gt; with a structure all of its own.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This is monadic composition in action. The result of one operation controls, at runtime, the entire shape of the next operation.&lt;/p&gt;&#10;&lt;p&gt;For example, here&amp;rsquo;s one way to go about generating pairs of &lt;code&gt;BadType&lt;/code&gt;s, where the second value is always greater than the first:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;generate_bad_type_pair&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Strategy&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// First generate a BadType.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;generate_bad_type&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;prop_flat_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;first_bad_type&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Now generate a second BadType with a value greater than the first.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_bad_type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;20000_&lt;/span&gt;&lt;span class="k"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;generate_ord_behavior&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prop_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second_value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate the second value.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;second_bad_type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;: &lt;span class="nc"&gt;second_value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;: &lt;span class="nc"&gt;RefCell&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ord_behavior&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Return the pair.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_bad_type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;second_bad_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Your first reaction might be: &lt;em&gt;wow, this seems really powerful&lt;/em&gt;. And you would be right! You can write &lt;em&gt;whatever you like&lt;/em&gt; in the body of a &lt;code&gt;prop_flat_map&lt;/code&gt;:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;You can conditionally return one strategy or another, reimplementing &lt;a href="https://docs.rs/proptest/latest/proptest/macro.prop_oneof.html"&gt;&lt;code&gt;prop_oneof&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;You can first generate a size and then return a vector with those many elements, reimplementing the &lt;code&gt;vec&lt;/code&gt; combinator.&lt;/li&gt;&#10;&lt;li&gt;You can call &lt;code&gt;prop_flat_map&lt;/code&gt; again, as many times as you like.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In a real and quite rigorous sense, &lt;code&gt;prop_flat_map&lt;/code&gt; is maximally powerful. Every combinator we&amp;rsquo;ve talked about, and in fact most of the proptest library, can be written in terms of &lt;code&gt;prop_flat_map&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;So why do all these combinators exist? Why don&amp;rsquo;t we just use &lt;code&gt;prop_flat_map&lt;/code&gt; everywhere?&lt;/p&gt;&#10;&lt;p&gt;The function actually works reasonably well in practice. It generates random values with the right shape, and shrinks them correctly on a failing input.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;But.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Shrinking is &lt;em&gt;really, really slow.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Exponentially&lt;/em&gt; slow.&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/flat-map-shrink.png" alt="Diagram comparing prop_flat_map behavior before and after shrinking. Left panel labeled &amp;#39;original&amp;#39; shows value=100 passing through prop_flat_map(0..x²) to create strategy:0..10000, which produces value=8634. Right panel labeled &amp;#39;shrink&amp;#39; shows value=50 passing through the same function to create a completely new strategy:0..2500 (highlighted in pink), which generates multiple different outputs (value=389, value=1653, value=...). An arrow connects the original strategy to the new one, illustrating how shrinking creates an entirely new structure rather than preserving the original one." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;&lt;code&gt;prop_flat_map&lt;/code&gt; with a map from &lt;em&gt;x&lt;/em&gt; to the strategy &lt;em&gt;0..x²&lt;/em&gt;. Each time the original value is shrunk, a brand-new strategy is created (highlighted in red) and the shrinker has to start over.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Why is that the case? Consider what happens when we want to shrink a value through a &lt;code&gt;prop_flat_map&lt;/code&gt;. As before:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;We would attempt to get a smaller value from the underlying value tree.&lt;/li&gt;&#10;&lt;li&gt;And then, because we would call the &lt;code&gt;prop_flat_map&lt;/code&gt; callback to generate a new strategy, we would &lt;em&gt;throw away the previously-generated value tree entirely&lt;/em&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Because &lt;code&gt;prop_flat_map&lt;/code&gt; generates brand new value trees each time it&amp;rsquo;s called, shrinking has to be started again, from scratch, each time! This is the essence of monadic composition: powerful, unconstrained, and fundamentally &lt;strong&gt;unpredictable&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;h3 id="measuring-the-impact"&gt;Measuring the impact&lt;/h3&gt;&#10;&lt;p&gt;We can measure the impact of monadic composition quite directly, along two related axes: the amount of time it takes to complete iterations, and the number of iterations the shrink completes in.&lt;/p&gt;&#10;&lt;p&gt;For this post I wrote &lt;a href="https://github.com/sunshowers-code/proptest-shrink-tests"&gt;a small Rust program&lt;/a&gt; which collects metrics about shrinking for:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The &lt;code&gt;prop_flat_map&lt;/code&gt; implementations for &lt;code&gt;BadType&lt;/code&gt; pairs above, and a non-monadic implementation with &lt;code&gt;prop_map&lt;/code&gt; (see below)&lt;/li&gt;&#10;&lt;li&gt;The same for &lt;code&gt;(BadType, BadType, BadType)&lt;/code&gt; triples: a non-monadic implementation with &lt;code&gt;prop_map&lt;/code&gt;, and a monadic one with two levels of &lt;code&gt;prop_flat_map&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;With this program, I collected 512 samples on my workstation and analyzed the data. (I ran the program with &lt;a href="https://doc.rust-lang.org/cargo/reference/profiles.html#opt-level"&gt;&lt;code&gt;opt-level&lt;/code&gt; set to 1&lt;/a&gt;, to mimic typical dev builds in larger Rust projects&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;).&lt;/p&gt;&#10;&lt;p&gt;First, the amount of time it took to shrink values down, by key &lt;a href="https://en.wikipedia.org/wiki/Percentile"&gt;percentile&lt;/a&gt;:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: center"&gt;Metric&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Pairs (&lt;code&gt;prop_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Triples (&lt;code&gt;prop_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Pairs (&lt;code&gt;prop_flat_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Triples (&lt;code&gt;prop_flat_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;min&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;11 µs&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;48 µs&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;3.85 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;8.95 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p50&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;1.70 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;2.52 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;8.52 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;181 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p75&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;3.74 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;5.77 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;10.04 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;307 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p90&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;5.25 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;8.41 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;11.76 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;435 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;max&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;7.00 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;10.55 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;15.53 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;1808 ms&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;In this table, &lt;strong&gt;p50&lt;/strong&gt; represents the median completion time, while &lt;strong&gt;p75&lt;/strong&gt; and &lt;strong&gt;p90&lt;/strong&gt; show the times that 75% and 90% of the samples completed within. With &lt;code&gt;prop_map&lt;/code&gt;, the amount of time scales somewhat linearly as we go from pairs to triples. But with just one additional level of &lt;code&gt;prop_flat_map&lt;/code&gt;, the performance degrades dramatically, going from under 20 milliseconds to almost 2 seconds! That&amp;rsquo;s over 100x slower.&lt;/p&gt;&#10;&lt;p&gt;The difference in the number of iterations is even more striking:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: center"&gt;Metric&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Pairs (&lt;code&gt;prop_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Triples (&lt;code&gt;prop_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Pairs (&lt;code&gt;prop_flat_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: right"&gt;Triples (&lt;code&gt;prop_flat_map&lt;/code&gt;)&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;min&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;48&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;93&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;1228&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;11223&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p50&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;215&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;306&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;6722&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;281016&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p75&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;270&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;354&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;9315&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;481996&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;p90&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;310&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;410&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;10722&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;693358&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;strong&gt;max&lt;/strong&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;387&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;530&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;12242&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: right"&gt;884729&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;From hundreds of iterations to almost a million! And we&amp;rsquo;re working with pretty simple structures here, too. Just one more level of &lt;code&gt;prop_flat_map&lt;/code&gt; would make shrinking quite noticeably slow, and another one after that would be disastrous.&lt;/p&gt;&#10;&lt;p&gt;The data here spans several orders of magnitude. A good way to visualize this kind of data is via a &lt;a href="https://en.wikipedia.org/wiki/Cumulative_distribution_function"&gt;CDF&lt;/a&gt; plotted on a logarithmic scale. In these graphs, the x-axis shows the time or iterations, and the y-axis shows the cumulative probability. Curves that are further to the right are worse, and the logarithmic scale reveals that the differences are in orders of magnitude.&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/proptest-performance-cdf.png" alt="There are two log‐log scale CDF (cumulative distribution function) plots, each showing four lines labeled “pair map,” “triple map,” “pair flat_map,” and “triple flat_map.” For the top plot (cdf of shrink execution time), the x‐axis ranges roughly from 10 µs to 1 × 10^6 µs (log scale) and the y‐axis shows cumulative probability from 0.01 to 1.0 (also log scale). The “pair map” (green) and “triple map” (purple) curves overlap around 100 µs to about 1,000 µs, reaching 100% probability before the “pair flat_map” (blue) and “triple flat_map” (orange) lines. The blue line peaks around tens of thousands of microseconds, while the orange line extends further toward 1 × 10^6 µs before leveling off. For the bottom plot (cdf of number of shrink iterations), the x‐axis is the number of iterations (10 to 1 × 10^6 on a log scale) and the y‐axis is cumulative probability (0.01 to 1.0 on a log scale). Again, “pair map” (green) and “triple map” (purple) are at lower iteration counts (roughly tens to hundreds) and reach 100% probability faster. “Pair flat_map” (blue) extends to thousands of iterations, and “triple flat_map” (orange) continues to tens or hundreds of thousands of iterations before reaching 100%. A legend in the top‐right corner identifies each line’s label. In the bottom right is system information (Ryzen 7950X, Linux 6.12, Rust 1.84.1, opt‐level 1)." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Cumulative distribution functions for &lt;code&gt;prop_map&lt;/code&gt; and &lt;code&gt;prop_flat_map&lt;/code&gt; pairs and triples. This is a logarithmic scale, so the differences are in orders of magnitude.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;h2 id="5-rediscovering-structure"&gt;5. Rediscovering structure&lt;/h2&gt;&#10;&lt;p&gt;What makes monadic composition so difficult to deal with? It has to do with the fact, mentioned above, that you can write &lt;em&gt;whatever you like&lt;/em&gt; inside the &lt;code&gt;prop_flat_map&lt;/code&gt;. Because a &lt;code&gt;prop_flat_map&lt;/code&gt; can contain arbitrary computation inside of it, and that computation returns brand-new strategies, determining how a value will shrink through it is fundamentally unpredictable without actually executing it.&lt;/p&gt;&#10;&lt;p&gt;In other words, the &lt;code&gt;prop_flat_map&lt;/code&gt; callback is quite opaque. Why is that? It&amp;rsquo;s because the &lt;code&gt;prop_flat_map&lt;/code&gt; callback is written in Rust, which is a powerful, &lt;a href="https://en.wikipedia.org/wiki/Turing_completeness"&gt;Turing-complete&lt;/a&gt; language. It is impossible to fully analyze the semantics of Turing-complete languages&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;. (You might know this as the &lt;a href="https://en.wikipedia.org/wiki/Halting_problem"&gt;halting problem&lt;/a&gt;, or as &lt;a href="https://en.wikipedia.org/wiki/Rice%27s_theorem"&gt;Rice&amp;rsquo;s theorem&lt;/a&gt;.)&lt;/p&gt;&#10;&lt;p&gt;But the fact that some analysis requires solving the halting problem is merely the start of the discussion, not the end of it! There is a rich literature on how to find approximate solutions for problems that are otherwise insoluble due to Rice&amp;rsquo;s theorem. For shrinking, here are a few approaches that are known to work.&lt;/p&gt;&#10;&lt;p&gt;One option is to &lt;strong&gt;place limits&lt;/strong&gt; on how long shrinking is done for. Note that &lt;code&gt;prop_flat_map&lt;/code&gt; has no issues while generating values, just while shrinking them&lt;sup id="fnref:6"&gt;&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref"&gt;6&lt;/a&gt;&lt;/sup&gt;. The proptest library itself sets limits on shrink iterations, particularly &lt;a href="https://docs.rs/proptest/latest/proptest/test_runner/struct.Config.html#structfield.max_flat_map_regens"&gt;across &lt;code&gt;prop_flat_map&lt;/code&gt; instances&lt;/a&gt;. This ensures that shrinking operations finish in a reasonable amount of time, even if they don&amp;rsquo;t produce minimal values.&lt;/p&gt;&#10;&lt;p&gt;A better option is to &lt;strong&gt;rewrite generators to not use monadic composition&lt;/strong&gt;. For the example above, it&amp;rsquo;s not hugely difficult&lt;sup id="fnref:7"&gt;&lt;a href="#fn:7" class="footnote-ref" role="doc-noteref"&gt;7&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;better_generate_bad_type_pair&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;impl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Strategy&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BadType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Generate two BadType instances.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;generate_bad_type&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;generate_bad_type&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Look, no prop_flat_map! This is non-monadic composition.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prop_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bad1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bad2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Add bad1.value to bad2.value. Because the two are non-negative&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// (unsigned integers), this ensures that bad2.value is always&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// bigger than bad1.value.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bad2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bad1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bad1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bad2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But this can be quite challenging as complexity goes up! The proptest library comes with a number of helpers to write non-monadic strategies, particularly &lt;a href="https://docs.rs/proptest/latest/proptest/strategy/trait.Strategy.html#method.prop_recursive"&gt;&lt;code&gt;prop_recursive&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://docs.rs/proptest/latest/proptest/sample/struct.Index.html"&gt;&lt;code&gt;sample::Index&lt;/code&gt;&lt;/a&gt;. But there are real situations, particularly with large and complex data structures (for example, randomly-generated programming language syntax trees), where none of those options suffice and you have to use the full power of &lt;code&gt;prop_flat_map&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;Last but not least, there&amp;rsquo;s a set of approaches that I&amp;rsquo;m going to put into the bucket of &lt;em&gt;rediscovering structure&lt;/em&gt; across flat maps. Key to these is understanding that when you generate a random value, you&amp;rsquo;re turning an RNG, which is a random stream of bits, into concrete, structured values. Can we somehow be clever about looking at the RNG bitstream?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;One option is to &lt;strong&gt;instrument the test&lt;/strong&gt;, for example by using a &lt;a href="https://github.com/rust-fuzz/cargo-fuzz"&gt;fuzzer&lt;/a&gt;. Fuzzing is all about generating random data, looking at the branches explored, and tweaking the random data to ensure other branches are taken. It&amp;rsquo;s a great fit for peering past the black box of monadic composition.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Another option is to &lt;strong&gt;be clever with random number generator&lt;/strong&gt;. A random number ultimately generates a sequence of ones and zeroes. Can we poke at this sequence, possibly with the help of some hints from the strategies themselves? This is implemented by the &lt;a href="https://github.com/HypothesisWorks/hypothesis"&gt;Hypothesis framework for Python&lt;/a&gt;; see &lt;a href="https://www.doc.ic.ac.uk/~afd/papers/2020/ECOOP_Hypothesis.pdf"&gt;this excellent paper about it&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Both of these approaches are heuristic and quite complex. But that&amp;rsquo;s what you need to put together some structure again, after it&amp;rsquo;s been through the blender of monadic composition.&lt;/p&gt;&#10;&lt;/div&gt;</description></item><item><title>Free will quite clearly doesn't exist</title><link>https://sunshowers.io/posts/there-is-no-free-will/</link><pubDate>Wed, 29 Jan 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/there-is-no-free-will/</guid><description>&lt;p&gt;&lt;em&gt;There&amp;rsquo;s absolutely no brand-new insight in this post. A huge amount of credit goes to various philosophers and thinkers, especially folks like &lt;a href="https://www.voidpod.com/"&gt;Aaron Rabinowitz&lt;/a&gt;, for shaping my views. Any errors in it are my own.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;In the natural world, everything that occurs is the result of a chain of causation all the way to the big bang. The chain of causation is:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;almost entirely deterministic&lt;/li&gt;&#10;&lt;li&gt;with some chaotic effects that appear to be random on the outside but in reality are deterministic&lt;/li&gt;&#10;&lt;li&gt;and also with a small amount of true (quantum) randomness, that very occasionally turns into something big&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;(We&amp;rsquo;ll summarize all of these causes into a single word, &amp;ldquo;deterministic.&amp;rdquo;)&lt;/p&gt;</description></item><item><title>Why nextest is process-per-test</title><link>https://sunshowers.io/posts/nextest-process-per-test/</link><pubDate>Tue, 07 Jan 2025 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/nextest-process-per-test/</guid><description>&lt;p&gt;&lt;em&gt;I&amp;rsquo;m often asked why the Rust test runner I maintain, &lt;a href="https://nexte.st/"&gt;cargo-nextest&lt;/a&gt;, runs every test in a separate process. Here&amp;rsquo;s my best attempt at explaining the rationale behind it.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;This document is cross-posted from the canonical copy at &lt;a href="https://nexte.st/docs/design/why-process-per-test/"&gt;the nextest site&lt;/a&gt;. Benchmarks &lt;a href="https://nexte.st/docs/benchmarks/"&gt;are available&lt;/a&gt; there as well, though a large part of this document talks about non-benchmarkable reliability improvements.&lt;/em&gt;&lt;/p&gt;&#10;&lt;h1 id="why-process-per-test"&gt;Why process-per-test?&lt;/h1&gt;&#10;&lt;p&gt;A key factor distinguishing nextest from &lt;code&gt;cargo test&lt;/code&gt; is that nextest runs &lt;strong&gt;each test in a separate process&lt;/strong&gt;. With nextest, the default execution model is now, and will always be, process-per-test. This document explains why.&lt;/p&gt;</description></item><item><title>Beyond Ctrl-C: The dark corners of Unix signal handling</title><link>https://sunshowers.io/posts/beyond-ctrl-c-signals/</link><pubDate>Tue, 03 Sep 2024 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/beyond-ctrl-c-signals/</guid><description>&lt;div class="sectioned-post"&gt;&#10;&lt;p&gt;&lt;em&gt;&lt;a href="https://rustconf.com/"&gt;RustConf 2024&lt;/a&gt; is next week, so I thought I&amp;rsquo;d put up a written version of my RustConf 2023 talk about signals in time for that. I&amp;rsquo;ve tried to retain the feel of it being a talk, while editing it heavily to make it readable as a blog entry. Some links:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://www.youtube.com/watch?v=zhbkp_Fzqoo"&gt;Video of the talk&lt;/a&gt; on YouTube.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://docs.google.com/presentation/d/e/2PACX-1vQuV0pYyWQulFbnRHfgvtHqQ_ZVyrnkmfQxT0eQfjEpVjnptUIG8uMx9FuUDz3wxtWqVN5QA9C4biZT/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000#slide=id.p"&gt;Slides&lt;/a&gt; on Google Slides.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;&lt;a href="https://github.com/sunshowers/rustconf-2023-signals"&gt;Demo repository&lt;/a&gt; on GitHub.&lt;/em&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"&gt;&#10;&#9;&#9;&#9;&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/zhbkp_Fzqoo?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"&gt;&lt;/iframe&gt;&#10;&#9;&#9;&lt;/div&gt;&#10;&#10;&lt;h2 id="1-introduction"&gt;1. Introduction&lt;/h2&gt;&#10;&lt;p&gt;Let&amp;rsquo;s start with a couple of questions:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Have you ever hit Ctrl-C while running a command-line program?&lt;/li&gt;&#10;&lt;li&gt;Have you ever encountered data corruption because you hit Ctrl-C at the wrong time?&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;If you&amp;rsquo;ve answered yes to the second question, then there&amp;rsquo;s a chance your program isn&amp;rsquo;t handling signals correctly. In this post, we&amp;rsquo;ll explore the world of Unix signals, delving into their history and their surprising complexity. We&amp;rsquo;ll learn how to handle these interruptions gracefully. And we&amp;rsquo;ll discover how async Rust can help us tame the chaos of signal handling, making it easier than ever to write robust software.&lt;/p&gt;&#10;&lt;p&gt;Most of this knowledge comes from my work on &lt;a href="https://nexte.st/"&gt;cargo-nextest&lt;/a&gt;, a next-generation test runner for Rust that is up to thrice as fast as &lt;code&gt;cargo test&lt;/code&gt;. One of the things that sets nextest apart is how it carefully, rigorously handles any signals that come its way. Be sure to try it out if you haven&amp;rsquo;t already!&lt;/p&gt;&#10;&lt;h3 id="why-signals"&gt;Why signals?&lt;/h3&gt;&#10;&lt;p&gt;If you&amp;rsquo;re an experienced engineer, you&amp;rsquo;re probably used to asking questions like &lt;em&gt;why&lt;/em&gt;, &lt;em&gt;when&lt;/em&gt;, and &lt;em&gt;how much&lt;/em&gt; you should care about something.&lt;/p&gt;&#10;&lt;p&gt;So: Why bother with signal handling at all? When should you care about signals?&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/kubernetes-sigterm.png" alt="A screenshot of Kubernetes documentation with: 3 - SIGTERM signal is sent to the pod. At this point, Kubernetes will send a SIGTERM signal to the containers in the pod. This signal lets the containers know that they are going to be shut down soon. Your code should listen for this event and start shutting down cleanly at this point. This may include stopping any long-lived connections (like a database connection or WebSocket stream), saving the current state, or anything like that..." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Kubernetes on SIGTERM (&lt;a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace"&gt;source&lt;/a&gt;).&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;You should care about signals if you&amp;rsquo;re developing a &lt;strong&gt;service&lt;/strong&gt;. You&amp;rsquo;re likely going to be running under a service manager like Kubernetes, and the lifecycle generally involves signals.&lt;/p&gt;&#10;&lt;p&gt;This screenshot shows Kubernetes documentation explaining that when your service is shutting down, it will receive a &lt;code&gt;SIGTERM&lt;/code&gt;. Docker works the same way with &lt;code&gt;docker stop&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;Note how the documentation advises that your code should &amp;ldquo;listen for this event and start shutting down cleanly at this point&amp;rdquo;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;You should also care about signals if you&amp;rsquo;re developing a &lt;strong&gt;command-line tool&lt;/strong&gt;. That&amp;rsquo;s because your users are impatient and if they perceive your operation as being slow for any reason, they will hit Ctrl-C and send a signal to your process.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;How much do you need to care about signals?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;If you&amp;rsquo;re a command like &lt;code&gt;ls&lt;/code&gt; or &lt;code&gt;cat&lt;/code&gt; and all you&amp;rsquo;re doing is &lt;strong&gt;reading data&lt;/strong&gt;, you probably don&amp;rsquo;t need to care much about signals. You&amp;rsquo;re not making any changes to the system so there&amp;rsquo;s little that can go wrong if your process dies.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;If you&amp;rsquo;re &lt;strong&gt;writing data&lt;/strong&gt;, it&amp;rsquo;s definitely worth thinking about signal handling.&lt;/p&gt;&#10;&lt;p&gt;There are ways to arrange for your code to be resilient to sudden termination, such as &lt;a href="https://crates.io/crates/atomicwrites"&gt;writing files atomically&lt;/a&gt;, or using a database like &lt;a href="https://www.sqlite.org/"&gt;SQLite&lt;/a&gt;. But even if your code doesn&amp;rsquo;t depend on correct signal handling, you can likely provide a better user experience if you do handle them.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Where you need to care most about signals is if you&amp;rsquo;re &lt;strong&gt;orchestrating an operation&lt;/strong&gt; in a distributed system. In those cases, if the process receives a signal locally, it may wish to send out cancellation messages over the wire.&lt;/p&gt;&#10;&lt;p&gt;Again, it&amp;rsquo;s a good idea to make your system fault-tolerant and resilient to sudden termination&amp;mdash;for example, by serializing state in a persistent store&amp;mdash;but at the very least, signals give you the opportunity to perform cleanup that can otherwise be difficult to do.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;strong&gt;Before we move on:&lt;/strong&gt; In this post, we&amp;rsquo;re going to focus on Unix, not Windows. We&amp;rsquo;re also going to focus on &lt;strong&gt;portable&lt;/strong&gt; signal handling, which means mechanisms that work on every Unix. Some Unix platforms offer alternative ways to handle signals&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;, but this post will not be discussing them.&lt;/p&gt;&#10;&lt;h3 id="a-basic-example"&gt;A basic example&lt;/h3&gt;&#10;&lt;p&gt;Let&amp;rsquo;s look at a simple example of a signal being sent to a process:&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/signal-crime-scene.png" alt="A screenshot of a terminal where `cargo build` was run, and Ctrl-C was pressed within a couple of seconds after. The last line shows &amp;#34;Building 225/305: object, regex-automata, syn,&amp;#34;." /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;What happened here?&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;I ran &lt;code&gt;cargo build&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;Then, a few seconds after, I hit Ctrl-C in my terminal.&lt;/li&gt;&#10;&lt;li&gt;The terminal sent a signal called &lt;code&gt;SIGINT&lt;/code&gt; (where &lt;code&gt;INT&lt;/code&gt; means &amp;ldquo;interrupt&amp;rdquo;) to the Cargo process.&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;SIGINT&lt;/code&gt; signal caused the Cargo process, as well as all the Rust compiler processes underneath it, to be interrupted and terminated.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;This example shows the two uses of signals. One is as a standardized, widely understood way for the kernel to interrupt a process. The other is as a basic, limited way to perform &lt;em&gt;interprocess communication&lt;/em&gt;, or &lt;em&gt;IPC&lt;/em&gt;.&lt;/p&gt;&#10;&lt;h3 id="sending-signals-via-kill"&gt;Sending signals via &lt;code&gt;kill&lt;/code&gt;&lt;/h3&gt;&#10;&lt;p&gt;Besides Ctrl-C and other shortcuts, the main way you’d be sending signals to processes on the command line is via &lt;a href="https://www.man7.org/linux/man-pages/man1/kill.1.html"&gt;the &lt;code&gt;kill&lt;/code&gt; command&lt;/a&gt;.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;To send &lt;code&gt;SIGINT&lt;/code&gt; or Ctrl-C, you can use &lt;code&gt;kill -INT &amp;lt;pid&amp;gt;&lt;/code&gt;, where &lt;code&gt;&amp;lt;pid&amp;gt;&lt;/code&gt; is the numeric process ID.&lt;/li&gt;&#10;&lt;li&gt;Each signal also has an associated number. For &lt;code&gt;SIGINT&lt;/code&gt; the number is always 2, so another way of saying this is &lt;code&gt;kill -2 &amp;lt;pid&amp;gt;&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;If you don’t specify a signal and just say &lt;code&gt;kill &amp;lt;pid&amp;gt;&lt;/code&gt;, it sends SIGTERM by default.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Within a programmatic context, libc has &lt;a href="https://man7.org/linux/man-pages/man2/kill.2.html"&gt;a &lt;code&gt;kill&lt;/code&gt; function&lt;/a&gt; you can call which does the same thing.&lt;/p&gt;&#10;&lt;h3 id="signal-names-and-numbers"&gt;Signal names and numbers&lt;/h3&gt;&#10;&lt;p&gt;As mentioned above, each signal has a name and number associated with it. Some of those numbers are standardized across Unix platforms, while others aren&amp;rsquo;t.&lt;/p&gt;&#10;&lt;p&gt;On Linux, if you type in &lt;code&gt;man 7 signal&lt;/code&gt;, you&amp;rsquo;ll see &lt;a href="https://man7.org/linux/man-pages/man7/signal.7.html"&gt;a long list&lt;/a&gt; of signals. Some of them are:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: center"&gt;Name&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: center"&gt;Number&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th style="text-align: center"&gt;Default action&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGINT&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;2&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Terminate&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGTERM&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;15&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Terminate&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;9&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Terminate, can&amp;rsquo;t customize&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGSEGV&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;11&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Terminate and core dump&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGTSTP&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Varies&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Stop process&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;&lt;code&gt;SIGCONT&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Varies&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td style="text-align: center"&gt;Resume process&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;In this table:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGKILL&lt;/code&gt; is also known as &lt;code&gt;kill -9&lt;/code&gt;, and it&amp;rsquo;s a special signal used to kill a process. What sets &lt;code&gt;SIGKILL&lt;/code&gt; apart is that unlike almost all other signals, its behavior can&amp;rsquo;t be customized in any way.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGSEGV&lt;/code&gt; might be familiar to you if you&amp;rsquo;ve ever encountered a core dump. Somewhat surprisingly, the behavior of &lt;code&gt;SIGSEGV&lt;/code&gt; &lt;em&gt;can&lt;/em&gt; be customized. For example, the Rust standard library customizes &lt;code&gt;SIGSEGV&lt;/code&gt;&amp;rsquo;s behavior to detect &lt;a href="./posts/rustc-segfault-illumos/"&gt;call stack exhaustion&lt;/a&gt;&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGTSTP&lt;/code&gt; and &lt;code&gt;SIGCONT&lt;/code&gt; are used for what is called &amp;ldquo;&lt;a href="https://en.wikipedia.org/wiki/Job_control_%28Unix%29"&gt;job control&lt;/a&gt;&amp;rdquo;. If you’ve ever used Ctrl-Z in &lt;code&gt;vim&lt;/code&gt;, or the commands &lt;code&gt;fg&lt;/code&gt; or &lt;code&gt;bg&lt;/code&gt;, then they uses these signals&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In general, all signals have a default action. Almost all of them also let you customize the default behavior, using what is called a &lt;em&gt;signal handler&lt;/em&gt;.&lt;/p&gt;&#10;&lt;h3 id="signal-handlers"&gt;Signal handlers&lt;/h3&gt;&#10;&lt;p&gt;A signal handler is a custom function that is used to intercept specific signals. Once a signal handler is set up, the kernel no longer follows the default action, calling the handler instead. In that sense, it’s a &lt;em&gt;reverse system call&lt;/em&gt;, also known as an &lt;em&gt;upcall&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;For example, if you’re writing or reading data, you use a &lt;em&gt;system call&lt;/em&gt; or &lt;em&gt;syscall&lt;/em&gt; to perform that action&amp;mdash;in that case, you’re calling the kernel. With an upcall, instead, the kernel calls you. (It&amp;rsquo;s an &amp;ldquo;upcall&amp;rdquo; because the call goes &amp;ldquo;up&amp;rdquo;, not &amp;ldquo;down&amp;rdquo;).&lt;/p&gt;&#10;&lt;p&gt;Importantly, signal upcalls can happen at &lt;em&gt;almost any time&lt;/em&gt;. And this is where we start running into issues.&lt;/p&gt;&#10;&lt;h2 id="2-an-example"&gt;2. An example&lt;/h2&gt;&#10;&lt;p&gt;To see the sorts of issues that signal handlers can create, let’s walk through a specific example: that of a download manager.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/download-manager.png" alt="Screenshot of a download manager on Windows XP. There are two windows visible, one in the background and one in the foreground. The background window is titled &amp;#34;Internet Download Manager 5.11&amp;#34; and has a list of partially and completely downloaded files in it. The foreground window says &amp;#34;85% mult.avi&amp;#34; and shows a download in progress." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Screenshot of a download manager on Windows XP.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Back in the 2000s, these programs were a lifesaver. I grew up with pretty terrible internet back then, and the download managers had several features that really helped. The most important feature was their ability to resume downloads, something that browsers didn&amp;rsquo;t support back then.&lt;/p&gt;&#10;&lt;p&gt;For this post, we&amp;rsquo;re going to work with a really simple download manager. Let&amp;rsquo;s say you provide a bunch of URLs to a tool, which then downloads them in parallel and maintains their status in a database.&lt;/p&gt;&#10;&lt;p&gt;Now, let’s say we want to handle &lt;code&gt;SIGINT&lt;/code&gt;. If the user hits Ctrl-C, we may want to follow a small set of sensible steps:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Cancel all running downloads.&lt;/li&gt;&#10;&lt;li&gt;Flush any data to disk.&lt;/li&gt;&#10;&lt;li&gt;In the database, mark the state of these downloads as interrupted.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Your first idea might be, &amp;ldquo;let’s just put all this logic in a signal handler&amp;rdquo;. Can you do that? The answer turns out to be that &lt;strong&gt;no, you can&amp;rsquo;t do that.&lt;/strong&gt; And &lt;em&gt;why&lt;/em&gt; you can&amp;rsquo;t do that is helpful at illustrating many of the pitfalls of signal handlers.&lt;/p&gt;&#10;&lt;h3 id="why-are-signal-handlers-limited"&gt;Why are signal handlers limited?&lt;/h3&gt;&#10;&lt;p&gt;Earlier, I mentioned that signal handlers can be called &lt;em&gt;at any time&lt;/em&gt;. It turns out that the ability to call a piece of code at any time is fraught with peril like little else in computing. This property of signal handlers is at the root of so many of the problems with them.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;For example, consider what happens if a signal handler is called while you’re holding a mutex or other lock. In general, trying to acquire the same lock again will result in a deadlock&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;. So you can’t call functions that try to acquire a lock.&lt;/p&gt;&#10;&lt;p&gt;Well, which functions acquire locks? Even something as basic as &lt;a href="https://man7.org/linux/man-pages/man3/malloc.3.html"&gt;allocating memory via &lt;code&gt;malloc&lt;/code&gt;&lt;/a&gt; requires a lock, because it pokes at global structures. This means that you cannot allocate memory in a signal handler. This alone shuts off a large percentage of the things you can do in a signal handler.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Another joy of signal handling is that while a handler is running, your process can receive a different signal and invoke a second signal handler. As you might imagine, this is just very hard to reason about in practice.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;These aren&amp;rsquo;t just theoretical concerns! You might have heard of &lt;a href="https://cve.mitre.org/"&gt;the CVE database&lt;/a&gt;, where security vulnerabilities are filed and listed. The CVE database has a lesser-known cousin called &lt;a href="https://cwe.mitre.org/"&gt;the CWE database&lt;/a&gt;, which lists out &amp;ldquo;common weaknesses&amp;rdquo; that result in security vulnerabilities. Within this database, there are no fewer than &lt;em&gt;four&lt;/em&gt; weaknesses related to incorrect signal handlers:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="https://cwe.mitre.org/data/definitions/364.html"&gt;CWE-364&lt;/a&gt;: Signal Handler Race Condition&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://cwe.mitre.org/data/definitions/432.html"&gt;CWE-432&lt;/a&gt;: Dangerous Signal Handler not Disabled During Sensitive Operations&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://cwe.mitre.org/data/definitions/479.html"&gt;CWE-479&lt;/a&gt;: Signal Handler Use of a Non-reentrant Function&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://cwe.mitre.org/data/definitions/828.html"&gt;CWE-828&lt;/a&gt;: Signal Handler with Functionality that is not Asynchronous-Safe&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;On Linux, &lt;a href="https://man7.org/linux/man-pages/man7/signal-safety.7.html"&gt;the man page on &lt;code&gt;signal-safety&lt;/code&gt;&lt;/a&gt; lists functions described by POSIX as okay to call in signal handlers, and it is pretty short. You can write to a file descriptor, but &lt;code&gt;malloc&lt;/code&gt; is not allowed. You also can&amp;rsquo;t open or seek a file.&lt;/p&gt;&#10;&lt;p&gt;The functions okay to call in signal handlers are called &lt;strong&gt;async-signal-safe functions&lt;/strong&gt;. The term is a bit confusing! &amp;ldquo;async&amp;rdquo; here has nothing to do with async Rust. (In many ways it’s the opposite of async Rust, because the defining characteristic of async Rust is that you &lt;em&gt;cannot&lt;/em&gt; just be interrupted or preempted at any time. You can only be interrupted at await points.)&lt;/p&gt;&#10;&lt;h3 id="the-self-pipe-trick"&gt;The self-pipe trick&lt;/h3&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/tips-and-tricks-piping.png" alt="A photo of the needle of a sewing machine working on a pipe in the context of a garment, with the label &amp;#34;Tips &amp;amp; tricks for piping perfectionists&amp;#34;." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;It&amp;rsquo;s important to get your piping right (&lt;a href="https://straight-grain.com/archives/13447"&gt;source&lt;/a&gt;).&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;So how do most modern programs handle signals? To understand that, we briefly need to introduce the concept of &lt;a href="https://www.sitepoint.com/the-self-pipe-trick-explained/"&gt;the &lt;em&gt;self-pipe trick&lt;/em&gt;&lt;/a&gt;. The trick uses a Unix feature called self-pipes, which have been described in the same breath as both &amp;ldquo;wonderful&amp;rdquo; and &amp;ldquo;cursed&amp;rdquo;: a high honor!&lt;/p&gt;&#10;&lt;p&gt;You might be familiar with pipes from having used them in shells via the namesake pipe (&lt;code&gt;|&lt;/code&gt;) operator. For example, consider a typical &lt;code&gt;find | xargs&lt;/code&gt; command.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;When this command is run, the shell creates a &lt;em&gt;pipe&lt;/em&gt;.&lt;/li&gt;&#10;&lt;li&gt;Each pipe has a &lt;em&gt;write end&lt;/em&gt; and a &lt;em&gt;read end&lt;/em&gt;.&lt;/li&gt;&#10;&lt;li&gt;In the case of &lt;code&gt;find | xargs&lt;/code&gt;, the write end is held by &lt;code&gt;find&lt;/code&gt;, and the read end by &lt;code&gt;xargs&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;A self-pipe is just a kind of pipe where the write and read ends are held by the same process.&lt;/p&gt;&#10;&lt;p&gt;Now, you might ask, &amp;ldquo;what&amp;rsquo;s the point of this?&amp;rdquo; And you&amp;rsquo;d be right to do so! Most of the time they don’t add any value. But they do add value in the specific context of signal handlers, because they let programs write signal handlers that are as simple as possible.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The program starts by creating a self-pipe.&lt;/li&gt;&#10;&lt;li&gt;It then hands the write end of the pipe to a signal handler, and holds on to the read end.&lt;/li&gt;&#10;&lt;li&gt;Then, the signal handler writes to that self-pipe. This is safe to do, because a pipe is a file descriptor, and writing to a file descriptor is async-signal-safe.&lt;/li&gt;&#10;&lt;li&gt;Then, the program reads from the self-pipe elsewhere.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Most C programs do this by hand, but in Rust you don’t have to write this delicate pattern manually. There are several crates which implement this pattern, &lt;a href="https://docs.rs/signal-hook/"&gt;like &lt;code&gt;signal-hook&lt;/code&gt;&lt;/a&gt;, and most people just use one of them.&lt;/p&gt;&#10;&lt;h3 id="now-what"&gt;Now what?&lt;/h3&gt;&#10;&lt;p&gt;Going back to the download manager example: Let’s say you write to a pipe, indicating that Ctrl-C has been pressed. How do you handle the read side of the pipe? Once you’ve received a signal, how do you handle it?&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;One option is to set some kind of &lt;strong&gt;global flag&lt;/strong&gt;, and check whether you&amp;rsquo;ve received a signal at every iteration of a loop&amp;ndash;or maybe every Nth iteration of a loop.&lt;/p&gt;&#10;&lt;p&gt;This works fine for small programs that are CPU-bound, but isn’t really scalable to large programs because those tend to have lots of loops in them (are you really going to add checks to every loop?)&lt;/p&gt;&#10;&lt;p&gt;Large programs are I/O bound anyway, and it&amp;rsquo;s a bit hard to check for signals while you&amp;rsquo;re waiting for some network operation to complete.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Another potential solution: Store all the state behind a mutex, and &lt;strong&gt;wrest control of the program&lt;/strong&gt; by locking out all the workers. This is a solution that some programs use, but it is really difficult to coordinate state between the workers and the signal handler. I really wouldn’t recommend following this approach.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;The most reasonable approach for I/O-bound programs is to use &lt;strong&gt;message passing&lt;/strong&gt;, where the parts of a program that deal with signals are notified that a signal has occurred.&lt;/p&gt;&#10;&lt;p&gt;This is possible to do in a synchronous model, but, as I&amp;rsquo;ve documented in my previous post about &lt;a href="./posts/nextest-and-tokio/"&gt;how nextest uses Tokio&lt;/a&gt;, it adds a great deal of unbounded complexity. What happens in practice is that you inevitably create a nest of threads whose only responsibility is to pass messages around.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The good news is that dealing with messages is much, much simpler with async Rust.&lt;/p&gt;&#10;&lt;h3 id="why-async-rust"&gt;Why async Rust?&lt;/h3&gt;&#10;&lt;p&gt;At this point you might ask: &amp;ldquo;I’m just a simple downloader tool, why would I need async for this?&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;Async Rust is presented as being for massively concurrent web or backend servers, but a secret about it is that that is just marketing. The scope of problems async Rust solves is much broader, and it happens to be incredibly well suited to signal handling for most programs. This is because async Rust provides some very expressive ways to perform advanced control flow, making such code readable without sacrificing performance.&lt;/p&gt;&#10;&lt;p&gt;To see how, we&amp;rsquo;re going to use Tokio&amp;rsquo;s signal handling functionality, which under the hood uses the same self-pipe trick mentioned above&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;. Here’s what a very simple example of Ctrl-C looks like under async Rust:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;signal&lt;/span&gt;::&lt;span class="n"&gt;unix&lt;/span&gt;::&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SignalKind&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ctrl_c_stream&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SignalKind&lt;/span&gt;::&lt;span class="n"&gt;interrupt&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ctrl_c_stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;got SIGINT&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The code sets up a stream of &lt;a href="https://docs.rs/tokio/1/tokio/signal/unix/struct.SignalKind.html#method.interrupt"&gt;&lt;code&gt;SignalKind::interrupt&lt;/code&gt;&lt;/a&gt; or Ctrl-C signals, then &lt;code&gt;await&lt;/code&gt;ing the &lt;code&gt;recv&lt;/code&gt; method.&lt;/li&gt;&#10;&lt;li&gt;The &lt;code&gt;recv&lt;/code&gt; method resolves each time the process receives Ctrl-C.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Now, this isn’t very special by itself; you can easily implement this with synchronous code. But this model really shines in more complex code paths, because of async Rust&amp;rsquo;s ability to perform &lt;strong&gt;heterogenous selects&lt;/strong&gt; &lt;a href="https://docs.rs/tokio/1/tokio/macro.select.html"&gt;via &lt;code&gt;tokio::select!&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;For a detailed discussion of heterogenous selects, see &lt;a href="./posts/nextest-and-tokio/#what-are-heterogenous-selects"&gt;my earlier post&lt;/a&gt; about them. But a quick summary is that &lt;code&gt;tokio::select!&lt;/code&gt; is a powerful control flow tool that waits for a set of futures to be ready concurrently, and resolves as soon as one of them completes.&lt;/p&gt;&#10;&lt;p&gt;What makes &lt;code&gt;tokio::select&lt;/code&gt; really special is it doesn’t just work against specific kinds of asynchronicity, but against any arbitrary, &lt;em&gt;heterogenous&lt;/em&gt; source of asynchronicity: signals, timers, any async function&amp;ndash;you name it. As a result, &lt;code&gt;tokio::select!&lt;/code&gt; is a great fit for signal handling&lt;sup id="fnref:6"&gt;&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref"&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;h3 id="implementing-signal-handling-using-tokioselect"&gt;Implementing signal handling using &lt;code&gt;tokio::select!&lt;/code&gt;&lt;/h3&gt;&#10;&lt;p&gt;Going back to our download manager example, let&amp;rsquo;s try using &lt;code&gt;tokio::select!&lt;/code&gt;. There are a few ways to organize this, but here&amp;rsquo;s one way.&lt;/p&gt;&#10;&lt;p&gt;We’re going to briefly introduce two constructs that make our life simpler:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="https://docs.rs/tokio/1/tokio/task/struct.JoinSet.html"&gt;The &lt;code&gt;JoinSet&lt;/code&gt; type&lt;/a&gt;, which stands for a set of worker &lt;em&gt;tasks&lt;/em&gt; (not threads!) that are running in parallel.&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://docs.rs/tokio/1/tokio/sync/broadcast/index.html"&gt;Broadcast channels&lt;/a&gt;, which allows a single producer to send messages to multiple consumers. The idea here is that the main task will receive signals, and then broadcast them to workers.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Here&amp;rsquo;s how the main function works:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Create a &lt;code&gt;JoinSet&lt;/code&gt; and a broadcast channel.&lt;/li&gt;&#10;&lt;li&gt;Spin up a stream of &lt;code&gt;SIGINT&lt;/code&gt; signals, as before.&lt;/li&gt;&#10;&lt;li&gt;Spawn a task for each worker on the &lt;code&gt;JoinSet&lt;/code&gt;, and pass in a receiver for broadcast messages.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[tokio::main]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;to_download&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* ... */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Make a JoinSet.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;join_set&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;JoinSet&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Also make a broadcast channel.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;broadcast&lt;/span&gt;::&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Spin up a stream of SIGINT signals.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ctrl_c_stream&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SignalKind&lt;/span&gt;::&lt;span class="n"&gt;interrupt&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Spawn a task for each worker, passing in a broadcast receiver.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;to_download&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;join_set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;worker_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... continued below&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;Next, the main function needs to wait for results from all the workers. How would we do it if we weren&amp;rsquo;t handling signals? Well, in that case we would loop and wait for each worker task to finish until they&amp;rsquo;re all done, handling errors along the way.&lt;/p&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// continued from above...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;join_set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join_next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Handle result...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// No more downloads left&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;To handle signals, we use a &lt;code&gt;tokio::select!&lt;/code&gt; with two branches from which items are fetched in parallel:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The first branch waits for worker tasks to be done with the same code as above&lt;/li&gt;&#10;&lt;li&gt;The second branch awaits a Ctrl-C message from the stream.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;CancelKind&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Interrupt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;join_set&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join_next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Handle result...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// No more downloads left&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ctrl_c_stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancelKind&lt;/span&gt;::&lt;span class="n"&gt;Interrupt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Now let&amp;rsquo;s look at the worker function. We first write our download function within an async block:&lt;/p&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;worker_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;: &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;: &lt;span class="nc"&gt;Receiver&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CancelKind&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;op&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Downloading&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;download_url_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Completed&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;Then, just like earlier, we write a loop with a &lt;code&gt;tokio::select!&lt;/code&gt; over two options:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The first branch drives the operation forward.&lt;/li&gt;&#10;&lt;li&gt;The second branch waits for cancellation messages over the broadcast channel.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 1. Drive the operation forward.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;op&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// 2. Wait for cancellation messages over the broadcast channel.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Handle the cancellation message received.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;What makes this model tick is how well it scales up to additional complexity. Two specific examples that are handled well by this model:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;First, you’ll likely want to handle other signals like &lt;code&gt;SIGTERM&lt;/code&gt;, since &lt;code&gt;SIGINT&lt;/code&gt; isn’t the only signal you’d receive.&lt;/li&gt;&#10;&lt;li&gt;Another common extension is to use what is sometimes called a &lt;em&gt;double Ctrl-C pattern&lt;/em&gt;. The first time the user hits Ctrl-C, you attempt to shut down the database cleanly, but the second time you encounter it, you give up and exit immediately.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;A fully working example of the download manager described above is &lt;a href="https://github.com/sunshowers/rustconf-2023-signals"&gt;in the demo repository&lt;/a&gt;. These two extensions are marked as exercises for you to complete; try solving them!&lt;/p&gt;&#10;&lt;h2 id="3-going-deeper"&gt;3. Going deeper&lt;/h2&gt;&#10;&lt;p&gt;If all you&amp;rsquo;re doing is orchestrating an external operation like downloading files, then this is most of what you need to know. But a lot of the really interesting details about signals lie when you&amp;rsquo;re spawning processes&amp;mdash;such as if you&amp;rsquo;re a shell, or a test runner like nextest. There is far too much to talk about here, but we’re going to go deeper into one particular example.&lt;/p&gt;&#10;&lt;h3 id="process-groups"&gt;Process groups&lt;/h3&gt;&#10;&lt;p&gt;In part 1, I mentioned that if you press Ctrl-C, the shell sends &lt;code&gt;SIGINT&lt;/code&gt; to the process you&amp;rsquo;re running. That isn&amp;rsquo;t quite correct; I fibbed a little! The truth is that the shell actually sends the signal to what is called a &lt;strong&gt;process group&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;p&gt;What is a process group? In Unix, processes can be organized into groups, each with its own unique identifier. Process groups allow a set of processes to be sent a signal at once. The shell creates a new process group for the command and its children, assigning them a unique group ID. This allows the shell to manage the processes as a unit, sending signals to the entire group at once.&lt;/p&gt;&#10;&lt;p&gt;On Linux, you can print process groups with a command like &lt;code&gt;ps fo pid,pgid,comm&lt;/code&gt;. (Other Unix platforms have their own &lt;code&gt;ps&lt;/code&gt; flags to display similar output.) For example, if you run this command while &lt;code&gt;cargo build&lt;/code&gt; is running in another terminal, you might get some output that looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ps fo pid,pgid,comm&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; PID PGID COMMAND&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; 1528 1528 zsh&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; 4100 4100 \_ cargo&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; 4261 4100 \_ rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; 4587 4100 \_ rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; 4640 4100 \_ rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this case:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The &lt;code&gt;zsh&lt;/code&gt; shell created a &lt;code&gt;cargo&lt;/code&gt; process numbered &lt;code&gt;4100&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;When &lt;code&gt;zsh&lt;/code&gt; did that, it also created a corresponding process group with the same number.&lt;/li&gt;&#10;&lt;li&gt;When &lt;code&gt;cargo build&lt;/code&gt; ran &lt;code&gt;rustc&lt;/code&gt;, those process groups were inherited.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Recall from earlier that if you want to send &lt;code&gt;SIGINT&lt;/code&gt; to a process via the &lt;code&gt;kill&lt;/code&gt; command, you’d use &lt;code&gt;kill -INT &amp;lt;pid&amp;gt;&lt;/code&gt;.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;If you want to send &lt;code&gt;SIGINT&lt;/code&gt; to a process &lt;em&gt;group&lt;/em&gt;, in very typical Unix fashion you have to use a negative number.&lt;/li&gt;&#10;&lt;li&gt;For example, if you run &lt;code&gt;kill -INT -4100&lt;/code&gt;, the &lt;code&gt;SIGINT&lt;/code&gt; signal gets sent atomically to the whole process group numbered &lt;code&gt;4100&lt;/code&gt;: the process, its children, grandchildren, everything.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;And that’s what really happens when you hit Ctrl-C: both the &lt;code&gt;cargo&lt;/code&gt; process itself and all the child &lt;code&gt;rustc&lt;/code&gt; processes are terminated by this signal sent to the process group.&lt;/p&gt;&#10;&lt;h3 id="making-your-own-process-groups"&gt;Making your own process groups&lt;/h3&gt;&#10;&lt;p&gt;Let’s say that you&amp;rsquo;re a test runner like nextest. What if you want to join the party? For example, it&amp;rsquo;s common for tests to spin up servers in another process to communicate with. A test runner that terminates a test likely also wants to kill off any processes created by that test.&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/pgroup.png" alt="A screenshot of part of Rust&amp;#39;s `std::os::unix::process::CommandExt`, with one method selected: fn process_group(&amp;amp;mut self, pgroup: i32) -&amp;gt; &amp;amp;mut Command." /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;To set the process group for a process, Unix provides a function called &lt;a href="https://pubs.opengroup.org/onlinepubs/9799919799/functions/setpgid.html"&gt;&lt;code&gt;setpgid&lt;/code&gt;&lt;/a&gt;. In Rust, access to this is provided via the &lt;code&gt;CommandExt&lt;/code&gt; extension trait&amp;rsquo;s &lt;a href="https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#tymethod.process_group"&gt;&lt;code&gt;process_group&lt;/code&gt; method&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Most of the time, you&amp;rsquo;ll want to pass in &lt;code&gt;-1&lt;/code&gt; for the process group, which means that the kernel will create a new process group for you with the same number as the process ID.&lt;/p&gt;&#10;&lt;h3 id="forwarding-signals-to-child-process-groups"&gt;Forwarding signals to child process groups&lt;/h3&gt;&#10;&lt;p&gt;Let&amp;rsquo;s say you&amp;rsquo;re a command-line process that is using process groups to manage its children. What happens if the user hits Ctrl-C?&lt;/p&gt;&#10;&lt;p&gt;You might assume that process groups form some sort of tree. Just like with the notion of a tree of processes with parent and child processes, you might imagine that process groups follow a similar pattern.&lt;/p&gt;&#10;&lt;p&gt;In reality: no, &lt;strong&gt;process groups don&amp;rsquo;t form a tree&lt;/strong&gt;. Each process group is its own island.&lt;/p&gt;&#10;&lt;p&gt;This means that as a &lt;em&gt;boundary process&lt;/em&gt; which manages process groups of your own, it is your responsibility to forward signals to child process groups.&lt;/p&gt;&#10;&lt;p&gt;(This is quite easy to fit into our async Rust model: the worker tasks, on receiving a broadcast message, send the corresponding signal to the process groups that they’re responsible for.)&lt;/p&gt;&#10;&lt;p&gt;Most programs would want to behave as similarly as possible to the world where they didn’t set up process groups. To achieve that you’ll want to at least forward these signals:&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;To match behavior,&lt;/strong&gt; for signals that are typically sent to process groups:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGINT&lt;/code&gt; (Ctrl-C).&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGQUIT&lt;/code&gt; (Ctrl-\).&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;SIGTSTP&lt;/code&gt; (Ctrl-Z), and &lt;code&gt;SIGCONT&lt;/code&gt; (&lt;code&gt;fg&lt;/code&gt; or &lt;code&gt;bg&lt;/code&gt;).&lt;/p&gt;&#10;&lt;p&gt;These two signals are interesting to handle in nextest: if you have timers running for your processes, for example if you want to timeout and kill process groups, you can combine &lt;code&gt;SIGTSTP&lt;/code&gt; and &lt;code&gt;SIGCONT&lt;/code&gt; with async Rust to pause and resume those timers. (There is also a deep story about an issue in POSIX lurking here, which I&amp;rsquo;ll cover in a sequel to this post.)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;strong&gt;Also consider&lt;/strong&gt;, to meet user expectations, forwarding &lt;code&gt;SIGTERM&lt;/code&gt; and other signals. These signals are not always sent to process groups, but it would make sense to forward them.&lt;/p&gt;&#10;&lt;h2 id="4-conclusion-echoes-of-the-past"&gt;4. Conclusion: echoes of the past&lt;/h2&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/signal-tower.jpg" alt="A photo of an abandoned microwave tower with graffiti all over its decaying sides. The tower is surrounded by a barbed wire fence, semiotically acting as both a beacon and a warning to stay away." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;An abandoned microwave tower in the Santa Monica Mountains, California (&lt;a href="https://www.wired.com/2015/03/spencer-harding-the-long-lines/"&gt;source&lt;/a&gt;).&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Signals are a fundamental part of the computing landscape, a legacy of design decisions made decades ago. They are a reminder that the systems we build today are shaped by the choices of the past, and that even the most well-intentioned innovations can have unintended consequences.&lt;/p&gt;&#10;&lt;p&gt;In &lt;a href="https://www.tuhs.org/pipermail/tuhs/2015-September/007509.html"&gt;the words of Doug McIlroy&lt;/a&gt;, creator of Unix pipes:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;code&gt;Signal()&lt;/code&gt; was there first and foremost to support &lt;code&gt;SIGKILL&lt;/code&gt;; it&#10;did not purport to provide a sound basis for asynchronous IPC.&#10;The complexity of &lt;code&gt;sigaction()&lt;/code&gt; is evidence that asynchrony remains&#10;untamed 40 years on.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Signals, for all their utility, were never meant to be the foundation of interprocess communication that they have become today. Yet we have found ways to adapt and evolve them to our needs, working around their limitations and turning them into opportunities.&lt;/p&gt;&#10;&lt;p&gt;Indeed, this is the essence of computing, and of technology itself: a story of creativity in the face of constraint, and of building upon the foundations of the past to create something new and beautiful.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;I hope this post has shed some light on the fascinating world of Unix signals, and perhaps even inspired you to think closely about the systems you build. If you&amp;rsquo;ll be at RustConf in Montreal, come find me: I&amp;rsquo;d love to chat more about this and hear your own stories about signals, or of other intricate systems with long-forgotten design decisions. See you there!&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Thanks to &lt;a href="https://infosec.exchange/@munin"&gt;Fiona&lt;/a&gt; for reviewing a draft of this post.&lt;/em&gt;&lt;/p&gt;&#10;&lt;/div&gt;</description></item><item><title>Debugging a rustc segfault on illumos</title><link>https://sunshowers.io/posts/rustc-segfault-illumos/</link><pubDate>Sun, 04 Aug 2024 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/rustc-segfault-illumos/</guid><description>&lt;div class="sectioned-post"&gt;&#10;&lt;p&gt;At &lt;a href="https://oxide.computer/"&gt;Oxide&lt;/a&gt;, we use &lt;a href="https://github.com/oxidecomputer/helios/"&gt;Helios&lt;/a&gt; as&#10;the base OS for the cloud computers we sell. Helios is a distribution of&#10;&lt;a href="https://illumos.org/"&gt;illumos&lt;/a&gt;, a Unix-based operating system descended from Solaris.&lt;/p&gt;&#10;&lt;p&gt;As someone who learned illumos on the job, I&amp;rsquo;ve been really impressed by the powerful debugging&#10;tools it provides. I had a chance to use some of them recently to track down a &lt;a href="https://en.wikipedia.org/wiki/Segmentation_fault"&gt;segmentation&#10;fault&lt;/a&gt; in the Rust compiler, with the help of&#10;several of my colleagues. I learned a lot from the process, and I thought I&amp;rsquo;d write about it!&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m writing this post for an audience of curious technologists who aren&amp;rsquo;t necessarily familiar with&#10;systems work. If you&amp;rsquo;re an experienced systems developer, parts of it are likely familiar to&#10;you&amp;mdash;feel free to skip over them.&lt;/p&gt;&#10;&lt;h2 id="the-crash"&gt;The crash&lt;/h2&gt;&#10;&lt;p&gt;A couple of weeks ago, I wanted to make a change to the Rust standard library on illumos. I logged&#10;into my illumos box and cloned the &lt;a href="https://github.com/rust-lang/rust"&gt;Rust repository&lt;/a&gt; (revision&#10;&lt;code&gt;2d5a628&lt;/code&gt;). Following the &lt;a href="https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html"&gt;setup&#10;instructions&lt;/a&gt;, I configured the &lt;code&gt;rustc&lt;/code&gt; build system with the &lt;code&gt;library&lt;/code&gt; build profile.&lt;/p&gt;&#10;&lt;p&gt;When I went to run &lt;code&gt;./x.py check&lt;/code&gt;, I saw an error with the following output:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./x.py check&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;Checking stage0 cranelift (x86_64-unknown-illumos)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; Checking cranelift-codegen v0.109.0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;rustc exited with signal: 11 (SIGSEGV) (core dumped)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;error: could not compile `cranelift-codegen` (lib)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;Caused by:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; process didn&amp;#39;t exit successfully: ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;Build completed unsuccessfully in 0:00:03&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Quite concerning! Like any good technologist I tried running the command again. But the segfault&#10;seemed to be completely deterministic: the program would crash while compiling &lt;code&gt;cranelift-codegen&lt;/code&gt;&#10;every time.&lt;/p&gt;&#10;&lt;p&gt;Coincidentally, we had our &lt;abbr title="Every 2 weeks"&gt;fortnightly&lt;/abbr&gt; &amp;ldquo;Rust @ Oxide&amp;rdquo; virtual meetup at around that time. There wasn&amp;rsquo;t much to discuss there, so we turned that meeting into a debugging session. (I love how my coworkers get excited about debugging strange issues.)&lt;/p&gt;&#10;&lt;h2 id="background-the-bootstrap-process"&gt;Background: the bootstrap process&lt;/h2&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/bootstrap-stages.png" alt="A flowchart to indicate stages of compilation. For a full description, see the link for &amp;#34;a series of stages&amp;#34;." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Rust compiler build stages.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Like the compilers for many other languages, the Rust compiler is written in the language it is&#10;intending to compile (in this case, Rust). In other words, the Rust compiler is&#10;&lt;a href="https://en.wikipedia.org/wiki/Self-hosting_%28compilers%29#Compilers"&gt;&lt;em&gt;self-hosting&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Any self-hosting compiler needs to answer the question: how in the world do you compile the compiler&#10;if you don&amp;rsquo;t already have a working compiler? This is known as the &lt;a href="https://en.wikipedia.org/wiki/Bootstrapping_%28compilers%29"&gt;&lt;em&gt;bootstrapping&#10;problem&lt;/em&gt;&lt;/a&gt;. There are several ways to&#10;address the problem, but the two most common are:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Use the previous version of the compiler.&lt;/strong&gt; In other words, use version N-1 of the compiler to&#10;compile version N. For example, use Rust 1.75 to compile Rust 1.76.&lt;/p&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;From where do you begin, though?&lt;/summary&gt;&#10;&lt;p&gt;The earliest versions of Rust were written in Ocaml. So if&#10;you&amp;rsquo;re spinning up Rust on a brand new platform and have an Ocaml compiler available, you can&#10;actually start &lt;a href="https://github.com/rust-lang/rust/tree/ef75860a0a72f79f97216f8aaa5b388d98da6480/src/boot"&gt;from&#10;there&lt;/a&gt;&#10;and effectively create your own lineage of compilers.&lt;/p&gt;&#10;&lt;p&gt;There are also implementations of Rust in other languages, like &lt;a href="https://github.com/thepowersgang/mrustc"&gt;&lt;code&gt;mrustc&lt;/code&gt;&lt;/a&gt; in C++, which can be used to build some (typically pretty old) version of the compiler. Interestingly, these other implementations don&amp;rsquo;t need to be perfect&amp;mdash;for example, since they&amp;rsquo;re only used to compile code that&amp;rsquo;s known to be valid, they don&amp;rsquo;t need to handle errors well. That&amp;rsquo;s a large chunk of the complexity of a real compiler.&lt;/p&gt;&#10;&lt;/details&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Cross-compile from another platform.&lt;/strong&gt; As a shortcut, if you have a way to cross-compile code&#10;from another platform, you can use that to set up the initial compiler. This is the most common&#10;method for setting up Rust on a new platform. (But note that method 1 must be used on at least&#10;one platform.)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;While bootstrapping from the previous version of Rust, the toolchain follows &lt;a href="https://rustc-dev-guide.rust-lang.org/building/bootstrapping/what-bootstrapping-does.html#stages-of-bootstrapping"&gt;a series of&#10;stages&lt;/a&gt;, ranging from &lt;em&gt;stage 0&lt;/em&gt; to &lt;em&gt;stage 2&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;In our case, since we&amp;rsquo;re working with the standard library we&amp;rsquo;re only concerned with &lt;em&gt;stage 0&lt;/em&gt;: the&#10;standard library compiled with the previous version of &lt;code&gt;rustc&lt;/code&gt;. That is the build process that crashed.&lt;/p&gt;&#10;&lt;h2 id="orienting-ourselves"&gt;Orienting ourselves&lt;/h2&gt;&#10;&lt;p&gt;The first thing to find is the version of &lt;code&gt;rustc&lt;/code&gt; that&amp;rsquo;s crashing. There are a few ways to find the compiler, but a simple &lt;code&gt;find&lt;/code&gt; command works well:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; find . -name rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;./compiler/rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;./src/doc/rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;./build/x86_64-unknown-illumos/stage0/bin/rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This command finds &lt;code&gt;rustc&lt;/code&gt; at &lt;code&gt;./build/x86_64-unknown-illumos/stage0/bin/rustc&lt;/code&gt;. Let&amp;rsquo;s ask it for its version:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./build/x86_64-unknown-illumos/stage0/bin/rustc -Vv&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;rustc 1.80.0-beta.1 (75ac3b633 2024-06-10)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;binary: rustc&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;commit-hash: 75ac3b6331873133c4f7a10f2252afd6f3906c6a&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;commit-date: 2024-06-10&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;host: x86_64-unknown-illumos&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;release: 1.80.0-beta.1&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;LLVM version: 18.1.7&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Can the bug be reproduced independently of the Rust toolchain? The toolchain does all sorts of&#10;non-standard things, so it&amp;rsquo;s worth checking. The output says &lt;code&gt;cranelift-codegen v0.109.0&lt;/code&gt;, so let&amp;rsquo;s try building that separately. Again, there are a few ways to do this, but the easiest is to make a simple Cargo project that depends on the crate.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-toml" data-lang="toml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;package&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;cranelift-codegen-test&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;0.1.0&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;edition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;2021&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;cranelift-codegen&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;=0.109.0&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And then run &lt;code&gt;cargo build&lt;/code&gt;. I didn&amp;rsquo;t have rustc 1.80.0 beta 1 on the machine, so I tried with the 1.80.0 release:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; cargo +1.80.0 build&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; Compiling cranelift-codegen v0.109.0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;error: could not compile `cranelift-codegen` (lib)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;Caused by:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; process didn&amp;#39;t exit successfully: `/home/rain/.rustup/toolchains/1.80.0-x86_64-unknown-illumos/bin/rustc ...` (signal: 11, SIGSEGV: invalid memory reference)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Yep, it crashes in the same spot.&lt;/p&gt;&#10;&lt;p&gt;This is a minimal-enough example, so let&amp;rsquo;s work with this.&lt;/p&gt;&#10;&lt;h2 id="finding-the-core-file"&gt;Finding the core file&lt;/h2&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/dumpster-fire.jpg" alt="A cute cartoon depiction of a dumpster fire with a smiley face." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Not this kind of dump! (&lt;a href="https://www.pinterest.com/pin/dumpster-fire-2021-by-bywayanyone--227502218670597019/"&gt;Pinterest&lt;/a&gt;)&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;When a program crashes, systems are typically configured to generate a &lt;a href="https://en.wikipedia.org/wiki/Core_dump"&gt;core dump&lt;/a&gt;, also known as a core file. The first step while debugging any crash is to ensure that core dumps are generated, and then to find one to examine it.&lt;/p&gt;&#10;&lt;p&gt;On illumos, many of the system-level administration tools are called &lt;code&gt;&amp;lt;something&amp;gt;adm&lt;/code&gt;. The tool for managing core files is called &lt;code&gt;coreadm&lt;/code&gt;. Let&amp;rsquo;s run that:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; coreadm&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; global core file pattern:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; global core file content: default&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; init core file pattern: core&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; init core file content: default&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; global core dumps: disabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; per-process core dumps: enabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; global setid core dumps: disabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; per-process setid core dumps: disabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; global core dump logging: disabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This suggests that core &amp;ldquo;per-process core dumps&amp;rdquo; are enabled. The lack of a pattern indicates that the defaults are used. Generally, on Unix systems the default is to generate a file named &lt;code&gt;core&lt;/code&gt; in the current directory of the crashing process.&lt;/p&gt;&#10;&lt;p&gt;A simple &lt;code&gt;ls&lt;/code&gt; in our little test project doesn&amp;rsquo;t show a &lt;code&gt;core&lt;/code&gt; file, which means that it might be elsewhere. Let&amp;rsquo;s just do a global &lt;code&gt;find&lt;/code&gt; for it.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; find / -name core -type f&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This showed a few files on my system, including: &lt;code&gt;~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cranelift-codegen-0.109.0/core&lt;/code&gt;. Bingo! That looks like a hit. (Why is it in the registry? Because when compiling a crate, Cargo sets the current working directory of the child &lt;code&gt;rustc&lt;/code&gt; process to the crate&amp;rsquo;s directory.)&lt;/p&gt;&#10;&lt;p&gt;The next step is to move the file into another directory&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. After doing that, let&amp;rsquo;s start examining it.&lt;/p&gt;&#10;&lt;h2 id="examining-the-core-file-registers-and-call-stack"&gt;Examining the core file: registers and call stack&lt;/h2&gt;&#10;&lt;p&gt;The best way to examine a core file on illumos is with the &lt;a href="https://illumos.org/books/mdb/preface.html"&gt;Modular Debugger, &lt;code&gt;mdb&lt;/code&gt;&lt;/a&gt;. &lt;code&gt;mdb&lt;/code&gt; is a powerful tool that can be used to inspect the state of both live and dead processes, as well as the kernel itself.&lt;/p&gt;&#10;&lt;p&gt;Using &lt;code&gt;mdb&lt;/code&gt; with the core file is simple: just run &lt;code&gt;mdb core&lt;/code&gt;.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; mdb core&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;Loading modules: [ libumem.so.1 libc.so.1 ld.so.1 ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first step is to enable symbol demangling&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;. The command to do that in &lt;code&gt;mdb&lt;/code&gt; is &lt;code&gt;$G&lt;/code&gt;, so let&amp;rsquo;s run that:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$G&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;C++ symbol demangling enabled&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(The output says &amp;ldquo;C++&amp;rdquo;, but illumos&amp;rsquo;s demangler can handle Rust symbols, too.)&lt;/p&gt;&#10;&lt;p&gt;Let&amp;rsquo;s look at the &lt;a href="https://en.wikipedia.org/wiki/Processor_register"&gt;CPU registers&lt;/a&gt; now. A register stores a small amount of data that the CPU can access very quickly. Core files typically have the contents of registers at the time of the crash, which can be very useful for debugging.&lt;/p&gt;&#10;&lt;p&gt;In &lt;code&gt;mdb&lt;/code&gt;, the command to print out registers is &lt;code&gt;$r&lt;/code&gt; or &lt;code&gt;::regs&lt;/code&gt;. Here&amp;rsquo;s the output:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$r&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x000000000fb0d460 %r8 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000001000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rbx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000000 %r9 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rcx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000000 %r10 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000010&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000001 %r11 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000286&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rsi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x000000000fb0d3d0 %r12 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000d96&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rdi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xfffffc7fed8e5f30 %r13 &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; %r14 = 0x000000000fb0d3d0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; %r15 = 0xfffffc7fed8e6200&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;cs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x0053 %fs &lt;span class="o"&gt;=&lt;/span&gt; 0x0000 %gs &lt;span class="o"&gt;=&lt;/span&gt; 0x0000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;ds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x004b %es &lt;span class="o"&gt;=&lt;/span&gt; 0x004b %ss &lt;span class="o"&gt;=&lt;/span&gt; 0x004b&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xfffffc7fd1adc4bb librustc_driver-86178b5e8d46877c.so&lt;span class="sb"&gt;`&lt;/span&gt;&amp;lt;rustc_parse::parser::Parser&amp;gt;::parse_path_segment+0x7b&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rbp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xfffffc7fed8e6140&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rsp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xfffffc7fed8e5c20&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;rflags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x00010216&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; id=0 vip=0 vif=0 ac=0 vm=0 rf=1 nt=0 iopl=0x0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; status=&amp;lt;of,df,IF,tf,sf,zf,AF,PF,cf&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;gsbase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0x0000000000000000&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;fsbase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xfffffc7fee830a80&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;%&lt;/span&gt;&lt;span class="nv"&gt;trapno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 0xe&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt; %err = 0x6&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;All right, there&amp;rsquo;s a lot going on here. A full accounting of the registers on x86-64 is beyond the scope of this post, but if you&amp;rsquo;re interested &lt;a href="https://math.hws.edu/eck/cs220/f22/registers.html"&gt;here&amp;rsquo;s a quick summary&lt;/a&gt;. The most important registers here are &lt;code&gt;%rip&lt;/code&gt;, &lt;code&gt;%rsp&lt;/code&gt;, and &lt;code&gt;%rbp&lt;/code&gt;. All three of these are 64-bit addresses.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/call-stack.png" alt="A visual depiction of a call stack, showing three inactive frames plus an active frame." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;A visual depiction of a call stack.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;%rip&lt;/code&gt; is the &lt;strong&gt;instruction pointer&lt;/strong&gt;, also known as the &lt;strong&gt;program counter&lt;/strong&gt;. &lt;code&gt;%rip&lt;/code&gt; is a special register that points to the next instruction to be executed. The CPU uses to keep track of where it is in the program.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;%rsp&lt;/code&gt; is the &lt;strong&gt;stack pointer&lt;/strong&gt;. The call stack is a region of memory that is used to store function call information and local variables. The stack pointer points to the head of the stack.&lt;/p&gt;&#10;&lt;p&gt;Note that on most architectures including x86-64, the stack grows down in memory: when a function is called, a new &lt;em&gt;stack frame&lt;/em&gt; is set up and the stack pointer is decremented by however much space the function needs.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;%rbp&lt;/code&gt; is the &lt;strong&gt;base pointer&lt;/strong&gt;, more commonly known as the &lt;strong&gt;frame pointer&lt;/strong&gt;. It points to the base of the current stack frame&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;We can also look at the call stack via the &lt;code&gt;$C&lt;/code&gt; command. The stack turns out to be enormous (&lt;a href="https://gist.github.com/sunshowers/5edb7207c1e1234b0400bc6517f45b29"&gt;full output&lt;/a&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$C&lt;/span&gt; ! wc -l&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;1493&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$C&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fed8e6140 librustc_driver-86178b5e8d46877c.so`&amp;lt;rustc_parse::parser::Parser&amp;gt;::parse_path_segment+0x7b()&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;&amp;lt;... snipped ...&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(The &lt;code&gt;!&lt;/code&gt; is used to send the output to a shell command, in this case one that counts the number of lines.)&lt;/p&gt;&#10;&lt;p&gt;It looks like the crash is in the &lt;code&gt;rustc&lt;/code&gt; parser. (Notably, the crash is while compiling a crate called &lt;code&gt;cranelift-codegen&lt;/code&gt;, which suggests automatic code generation. Generated code often tends to stress the parser in ways that manually written code does not.)&lt;/p&gt;&#10;&lt;p&gt;Based on the call stack, it looks like the &lt;code&gt;rustc&lt;/code&gt; parser is recursive in nature. A quick Google&#10;search&#10;&lt;a href="https://users.rust-lang.org/t/what-type-of-parser-does-the-rust-compiler-use/71430"&gt;confirms&lt;/a&gt; that&#10;the &lt;code&gt;rustc&lt;/code&gt; parser is a &amp;ldquo;simple hand-written recursive descent parser&amp;rdquo;. This isn&amp;rsquo;t surprising, since&#10;most production parsers are written this way. (For example, &lt;a href="https://docs.rs/syn"&gt;&lt;code&gt;syn&lt;/code&gt;&lt;/a&gt; is also a&#10;recursive descent parser.)&lt;/p&gt;&#10;&lt;p&gt;Turning our attention to the instruction pointer &lt;code&gt;fffffc7fd1adc4bb&lt;/code&gt;, we can use the &lt;code&gt;::dis&lt;/code&gt; command to disassemble the function at that address. (&lt;a href="https://gist.github.com/sunshowers/959c649926fe4a9d8bc53d967f895cdf#file-gistfile0-txt-L11"&gt;Full output&lt;/a&gt;; the &lt;code&gt;-a&lt;/code&gt; flag ensures that addresses are not converted to very long function names.)&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; fffffc7fd1adc4bb::dis -a&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;&amp;lt;... snipped ...&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fd1adc4b6 movl $0x1,%edx&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fd1adc4bb call +0x1caf0 &amp;lt;librustc_driver-86178b5e8d46877c.so`&amp;lt;rustc_parse::parser::Parser&amp;gt;::parse_ident_common&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fd1adc4c0 cmpl $0x0,0xfffffffffffffdf0(%rbp)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;&amp;lt;... snipped ...&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So it looks like the crash is happening in a &lt;code&gt;call&lt;/code&gt; instruction to another function,&#10;&lt;code&gt;parse_ident_common&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;(Keep in mind that this information could be completely unreliable! The stack might be corrupted, the registers might be wrong, and so on. But it&amp;rsquo;s what we have for now.)&lt;/p&gt;&#10;&lt;h2 id="examining-the-address-space"&gt;Examining the address space&lt;/h2&gt;&#10;&lt;p&gt;On &lt;a href="https://en.wikipedia.org/wiki/Virtual_memory"&gt;virtual memory systems&lt;/a&gt;, which includes all modern&#10;desktop and server systems, each process gets the illusion that it has a very large amount of memory&#10;all to itself. This is called the address space of a process. The instructions, the call stack, and&#10;the heap all get their own regions of addresses in that space, called &lt;em&gt;memory mappings&lt;/em&gt;. The 64-bit&#10;addresses that we saw earlier are all part of the address space.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;mdb&lt;/code&gt; has a command called &lt;code&gt;whatis&lt;/code&gt; to look up which part of memory an address is at. Let&amp;rsquo;s look at the stack pointer first:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; fffffc7fed8e5c20::whatis&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fed8e5c20 is in [ unknown ] [fffffc7fed8e5000,fffffc7fed8e6000)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This tells us that the address is in the range &lt;code&gt;0xfffffc7fed8e5000&lt;/code&gt; to &lt;code&gt;0xfffffc7fed8e6000&lt;/code&gt;. This is&#10;a small 4 KiB range.&lt;/p&gt;&#10;&lt;p&gt;What about the frame pointer?&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt; fffffc7fed8e6140::whatis&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;fffffc7fed8e6140 is in [ unknown ] [fffffc7fed8e6000,fffffc7fed9e7000)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This appears to be in a different range.&lt;/p&gt;&#10;&lt;p&gt;In this case, the ending address is &lt;code&gt;fffffc7fed9e7000&lt;/code&gt; (note the &lt;code&gt;9e&lt;/code&gt;, not the &lt;code&gt;8e&lt;/code&gt;!). This address&#10;is &lt;strong&gt;&lt;code&gt;0x101000&lt;/code&gt;&lt;/strong&gt; bytes away from the starting address. That is equal to 1028 &lt;abbr title="Kibibyte, or 1024 bytes"&gt;KiB&lt;/abbr&gt;, or 1 &lt;abbr title="Mebibyte, or 1024 kibibytes"&gt;MiB&lt;/abbr&gt; + 4 KiB page&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;Something else that&amp;rsquo;s relevant here is what permissions each range of addresses has. Like files on Unix, a block of virtual memory can have &lt;em&gt;read&lt;/em&gt;, &lt;em&gt;write&lt;/em&gt;, or &lt;em&gt;execute&lt;/em&gt; permissions. (In this case, &lt;em&gt;execute&lt;/em&gt; means that it is valid for the instruction pointer to point here&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;.)&lt;/p&gt;&#10;&lt;p&gt;On illumos, a tool called &lt;code&gt;pmap&lt;/code&gt; can show these spaces. &lt;code&gt;pmap&lt;/code&gt; works on both live processes and core files. Running &lt;code&gt;pmap core&lt;/code&gt; shows the permissions for the addresses we&amp;rsquo;re interested in (&lt;a href="https://gist.github.com/sunshowers/03fdbd76162a838d9b11b3c9beba6a81#file-gistfile0-txt-L27-L28"&gt;full output&lt;/a&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; pmap core&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;&amp;lt;... snipped ..&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;FFFFFC7FED8E5000 4K ----- [ anon ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;FFFFFC7FED8E6000 1028K rw--- [ anon ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;&amp;lt;... snipped ..&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The 1028 KiB range is read-write, and the 4 KiB range above that doesn&amp;rsquo;t have any permissions whatsoever.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;This would explain the segfault&lt;/strong&gt;. A segfault is an attempt to operate on a part of memory that the program doesn&amp;rsquo;t have permissions for. Attempting to read from or write to memory which has no permissions is an example of that.&lt;/p&gt;&#10;&lt;h2 id="formulating-a-theory"&gt;Formulating a theory&lt;/h2&gt;&#10;&lt;p&gt;At this point, we have enough information to come up with a theory:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The thread had a call stack of 1028 KiB available to it, starting at &lt;code&gt;fffffc7fed8e6000&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;The call stack pointer was at &lt;code&gt;fffffc7fed8e6140&lt;/code&gt; (only &lt;code&gt;0x140&lt;/code&gt; = 320 bytes away), and it tried to create a frame of size &lt;code&gt;0x520&lt;/code&gt; (1312) bytes, at &lt;code&gt;fffffc7fed8e5c20&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;This caused the call stack to be &lt;em&gt;exhausted&lt;/em&gt;: the thread ran out of space&lt;sup id="fnref:6"&gt;&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref"&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;&#10;&lt;li&gt;When the thread ran out of space, it indexed into a 4 KiB section known as a &lt;em&gt;guard page&lt;/em&gt;. The thread did not have any permissions to operate on the page, and was in fact designed to cause a segfault if accessed in any way.&lt;/li&gt;&#10;&lt;li&gt;The program then (correctly) segfaulted.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;But there are also other bits of evidence that this theory doesn&amp;rsquo;t explain, or even cuts against. (This is what makes post-mortem debugging exciting! There are often contradictory-seeming pieces of information that need to be explained.)&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;The memory is marked &lt;code&gt;anon&lt;/code&gt; or &lt;code&gt;unknown&lt;/code&gt;.&lt;/strong&gt; That&amp;rsquo;s not how call stacks are supposed to be marked! In the &lt;code&gt;pmap&lt;/code&gt; output, there&amp;rsquo;s a line which says:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;FFFFFC7FED7B1000 316K rw--- [ stack tid=3 ]&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So you&amp;rsquo;d expect call stacks to be marked with &lt;code&gt;[ stack tid=&amp;lt;something&amp;gt; ]&lt;/code&gt;, not &lt;code&gt;[ anon ]&lt;/code&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Why is the size of the allocation 1028 KiB?&lt;/strong&gt; You&amp;rsquo;d generally expect stack sizes to be a round power of two.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Isn&amp;rsquo;t 1028 KiB kind of small?&lt;/strong&gt; The thread is a non-main thread, and &lt;a href="https://doc.rust-lang.org/std/thread/#stack-size"&gt;the default stack size for Rust threads is 2 MiB&lt;/a&gt;. Why is our thread ~1 MiB and not 2 MiB?&lt;/p&gt;&#10;&lt;details&gt;&#10;&lt;summary&gt;How are call stack sizes determined?&lt;/summary&gt;&#10;&lt;p&gt;On Unix platforms, for the main thread, the call stack size is determined by &lt;code&gt;ulimit -s&lt;/code&gt; (in KiB). On my illumos machine, this printed &lt;code&gt;10240&lt;/code&gt;, indicating a 10 MiB call stack.&lt;/p&gt;&#10;&lt;p&gt;For child threads, the call stack size is determined by whatever created them. For Rust, the default is 2 MiB.&lt;/p&gt;&#10;&lt;/details&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Why doesn&amp;rsquo;t this crash happen on other platforms?&lt;/strong&gt; If this is a crash in the &lt;code&gt;rustc&lt;/code&gt; parser, one would ordinarily expect it to arise everywhere. Yet it doesn&amp;rsquo;t seem to occur on Linux, macOS, or Windows. What&amp;rsquo;s special about illumos?&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Setting &lt;code&gt;RUST_MIN_STACK&lt;/code&gt; doesn&amp;rsquo;t help.&lt;/strong&gt; Rust-created thread stack sizes can be configured via &lt;a href="https://doc.rust-lang.org/std/thread/#stack-size"&gt;the &lt;code&gt;RUST_MIN_STACK&lt;/code&gt; environment variable&lt;/a&gt;. If we try to use that:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;RUST_MIN_STACK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt; cargo build&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It turns out that &lt;code&gt;rustc&lt;/code&gt; crashes at exactly the same spot. That&amp;rsquo;s really strange!&lt;/p&gt;&#10;&lt;p&gt;It is possible that the stack size was overridden at thread creation time. The documentation for &lt;code&gt;RUST_MIN_STACK&lt;/code&gt; says: &amp;ldquo;Note that setting &lt;code&gt;Builder::stack_size&lt;/code&gt; will override this.&amp;rdquo; But that seems unlikely.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="a-closer-look-at-the-call-stack"&gt;A closer look at the call stack&lt;/h2&gt;&#10;&lt;p&gt;Looking towards the bottom of the call stack, there&amp;rsquo;s &lt;a href="https://gist.github.com/sunshowers/5edb7207c1e1234b0400bc6517f45b29#file-gistfile0-txt-L1478-L1483"&gt;something really strange&lt;/a&gt;:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;fffffc7fed9e5f80 librustc_driver-86178b5e8d46877c.so`rustc_query_system::query::plumbing::try_execute_query...&#10;fffffc7fed9e5fd0 librustc_driver-86178b5e8d46877c.so`stacker::grow::&amp;lt;rustc_middle::query::erase::Erased&amp;lt;[u8; 16]&amp;gt;, ...&amp;gt;&#10;fffffc7fed9e5ff0 librustc_driver-86178b5e8d46877c.so`psm::on_stack::with_on_stack...&#10;fffffc7fed7e4960 librustc_driver-86178b5e8d46877c.so`rust_psm_on_stack+9()&#10;fffffc7fed7e4a20 librustc_driver-86178b5e8d46877c.so`stacker::_grow+0x13e()&#10;fffffc7fed7e4ad0 librustc_driver-86178b5e8d46877c.so`rustc_query_impl::query_impl::resolver_for_lowering_raw::get_query_non_incr...&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice the jump in addresses from &lt;code&gt;fffffc7fed7e4960&lt;/code&gt; to &lt;code&gt;fffffc7fed9e5ff0&lt;/code&gt;? Normally, stack addresses are decremented as new functions are called: the number goes down. In this case the stack address is &lt;em&gt;incremented&lt;/em&gt;. The number went up. Strange.&lt;/p&gt;&#10;&lt;p&gt;Also notice that this coincides with the use of a function called &lt;code&gt;stacker::_grow&lt;/code&gt;. Now that&amp;rsquo;s a real lead!&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;What part of memory is &lt;code&gt;fffffc7fed7e4960&lt;/code&gt; in?&lt;/strong&gt; &lt;code&gt;mdb&lt;/code&gt; says:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;&amp;gt; fffffc7fed7e4960::whatis&#10;fffffc7fed7e4960 is in [ stack tid=3 ]&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So &lt;em&gt;this&lt;/em&gt; address is part of the stack for thread 3. &lt;a href="https://gist.github.com/sunshowers/03fdbd76162a838d9b11b3c9beba6a81#file-gistfile0-txt-L26"&gt;&lt;code&gt;pmap&lt;/code&gt; agrees&lt;/a&gt;:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;FFFFFC7FED7B1000 316K rw--- [ stack tid=3 ]&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;What is &lt;code&gt;stacker&lt;/code&gt;?&lt;/strong&gt; Time for some googling! Per &lt;a href="https://docs.rs/stacker"&gt;the documentation&lt;/a&gt;, &lt;code&gt;stacker&lt;/code&gt; is:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;A library to help grow the stack when it runs out of space.&lt;/p&gt;&#10;&lt;p&gt;This is an implementation of manually instrumented segmented stacks where&#10;points in a program’s control flow are annotated with “maybe grow the stack&#10;here”. Each point of annotation indicates how far away from the end of the&#10;stack it’s allowed to be, plus the amount of stack to allocate if it does&#10;reach the end.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Because the &lt;code&gt;rustc&lt;/code&gt; parser is recursive, it is susceptible to call stack exhaustion. The use of &lt;code&gt;stacker&lt;/code&gt; is supposed to prevent, or at least mitigate, that.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;How does &lt;code&gt;stacker&lt;/code&gt; work?&lt;/strong&gt; The library has &lt;a href="https://docs.rs/stacker/0.1.15/stacker/fn.maybe_grow.html"&gt;a pretty simple API&lt;/a&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;pub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;maybe_grow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;: &lt;span class="nb"&gt;FnOnce&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;red_zone&lt;/span&gt;: &lt;span class="kt"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stack_size&lt;/span&gt;: &lt;span class="kt"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;callback&lt;/span&gt;: &lt;span class="nc"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/rust-stack.jpg" alt="An image from YouTube showing Rust the video game, titled &amp;#34;EZ Triple Floor Stack&amp;#34;." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Er, wrong Rust.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;The developer is expected to intersperse calls to &lt;code&gt;maybe_grow&lt;/code&gt; within their recursive function. If less than &lt;code&gt;red_zone&lt;/code&gt; bytes of stack space remain, &lt;code&gt;stacker&lt;/code&gt; will allocate a new segment of &lt;code&gt;stack_size&lt;/code&gt; bytes, and run &lt;code&gt;callback&lt;/code&gt; with the stack pointer pointing to the new segment.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;How does rustc use &lt;code&gt;stacker&lt;/code&gt;?&lt;/strong&gt; The code is in &lt;a href="https://github.com/rust-lang/rust/blob/dba8e2d2c2890a8b9e88cbf4855ac5711337946c/compiler/rustc_data_structures/src/stack.rs#L17"&gt;this file&lt;/a&gt;. The code requests an additional 1 MiB stack with a red zone of 100 KiB.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Why did &lt;code&gt;stacker&lt;/code&gt; create a new stack segment?&lt;/strong&gt; In our case, the call is at the very bottom of the stack, when plenty of space should be available, so ordinarily &lt;code&gt;stacker&lt;/code&gt; should not need to allocate a new segment. Why did it do so here?&lt;/p&gt;&#10;&lt;p&gt;The answer is &lt;a href="https://github.com/rust-lang/stacker/blob/5df2309ccf7b1671909386c2670c7342a4d44142/src/lib.rs#L412-L457"&gt;in &lt;code&gt;stacker&lt;/code&gt;&amp;rsquo;s source code&lt;/a&gt;. There is code to guess the stack size on many platforms. But it isn&amp;rsquo;t enabled on illumos: &lt;code&gt;guess_os_stack_limit&lt;/code&gt; always returns &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="putting-it-together"&gt;Putting it together&lt;/h2&gt;&#10;&lt;p&gt;With this information in hand, we can flesh out our call stack exhaustion theory:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Some file in &lt;code&gt;cranelift-codegen&lt;/code&gt; was triggering the crash by requiring more than 1 MiB of stack space.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The &lt;code&gt;rustc&lt;/code&gt; parser running against &lt;code&gt;cranelift-codegen&lt;/code&gt; needed more than 1 MiB of stack space, but less than 2 MiB.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Had this bug occurred on other platforms like Linux, this issue would have been a showstopper. However, it wasn&amp;rsquo;t visible on those platforms because:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Threads created by Rust use a 2 MiB stack by default.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;rustc&lt;/code&gt; requested that &lt;code&gt;stacker&lt;/code&gt; create a 1 MiB stack segment, but only if less than 100 KiB of stack space was left.&lt;/li&gt;&#10;&lt;li&gt;On the other platforms, &lt;code&gt;stacker&lt;/code&gt; could see that well over 100 KiB of stack space was left, and so it did not allocate a new segment.&lt;/li&gt;&#10;&lt;li&gt;On illumos, &lt;code&gt;stacker&lt;/code&gt; could not see how much stack was left, and so it allocated a new 1 MiB segment.&lt;/li&gt;&#10;&lt;li&gt;This 1 MiB stack was simply not enough to parse &lt;code&gt;cranelift-codegen&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;rustc&lt;/code&gt; didn&amp;rsquo;t call &lt;code&gt;stacker::maybe_grow&lt;/code&gt; enough! In order for it to work, &lt;code&gt;stacker&lt;/code&gt; needs to be interspersed throughout the recursive code. But some recursive parts did not appear to have called it.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;(It is somewhat ironic that &lt;code&gt;stacker&lt;/code&gt;, a library meant to prevent call stack exhaustion, was actively making life worse here.)&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Where does the 1028 KiB come from?&lt;/strong&gt; Looking at the &lt;a href="https://github.com/rust-lang/stacker/blob/5df2309ccf7b1671909386c2670c7342a4d44142/src/lib.rs#L228-L234"&gt;&lt;code&gt;stacker&lt;/code&gt; source code&lt;/a&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;page_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;page_size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;requested_pages&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stack_size&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;checked_add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;unreasonably large stack requested&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;page_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stack_pages&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;::&lt;span class="n"&gt;cmp&lt;/span&gt;::&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;requested_pages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stack_bytes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stack_pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;checked_mul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page_size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;unreasonably large stack requesteed&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It looks like &lt;code&gt;stacker&lt;/code&gt; first computes the number of requested pages by dividing the requested stack size by the page size, rounding up. Then it adds 2 to that. In our case:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The requested stack size is 1 MiB.&lt;/li&gt;&#10;&lt;li&gt;With 4 KiB pages, this works out to 256 pages.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;stacker&lt;/code&gt; then requests 256 + 2 = 258 pages, which is 1032 KiB.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This explains both the 1028 KiB allocation (one guard page after the stack), and the 4 KiB guard page we&amp;rsquo;re crashing at (one guard page before the stack).&lt;/p&gt;&#10;&lt;h2 id="triggering-the-bug-on-other-platforms"&gt;Triggering the bug on other platforms&lt;/h2&gt;&#10;&lt;p&gt;If the issue is that a 1 MiB stack isn&amp;rsquo;t enough, it should be possible to reproduce this on other platforms by setting their stack size to something smaller than the 2 MiB default.&lt;/p&gt;&#10;&lt;p&gt;With a stack size &amp;lt;= 1 MiB, we would expect that:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;rustc&lt;/code&gt; calls &lt;code&gt;stacker&lt;/code&gt; as before.&lt;/li&gt;&#10;&lt;li&gt;There are two possibilities: either &lt;code&gt;stacker&lt;/code&gt; decides there is enough stack space and doesn&amp;rsquo;t create a new segment, or it decides there isn&amp;rsquo;t enough and does create a new 1 MiB segment.&lt;/li&gt;&#10;&lt;li&gt;In either case, 1 MiB is simply not enough to parse &lt;code&gt;cranelift-codegen&lt;/code&gt;, and the program crashes.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Let&amp;rsquo;s try to compile &lt;code&gt;cranelift-codegen&lt;/code&gt; on Linux with a reduced stack size.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; cranelift-codegen-test&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; &lt;span class="nv"&gt;RUST_MIN_STACK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1048576&lt;/span&gt; cargo +1.80.0 build&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;note: rustc unexpectedly overflowed its stack! this is a bug&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;note: maximum backtrace depth reached, frames may have been lost&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;note: we would appreciate a report at https://github.com/rust-lang/rust&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;help: you can increase rustc&amp;#39;s stack size by setting RUST_MIN_STACK=2097152&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;note: backtrace dumped due to SIGSEGV! resuming signal&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This does crash as expected. The full output is &lt;a href="https://gist.github.com/sunshowers/3ac000e5a5022acd3f07886a16a39520"&gt;here&lt;/a&gt;. Some of the symbols are missing, but the crash does seem to be in parser code.&lt;/p&gt;&#10;&lt;p&gt;(At this point, we could have gone further and tried to make &lt;a href="https://github.com/rust-lang/rust/issues/116249#issuecomment-1741572717"&gt;a debug-assertions build of &lt;code&gt;rustc&lt;/code&gt;&lt;/a&gt; &amp;ndash; but it was already pretty clear why the crash was happening.)&lt;/p&gt;&#10;&lt;h2 id="what-codes-failing-to-parse-anyway"&gt;What code&amp;rsquo;s failing to parse, anyway?&lt;/h2&gt;&#10;&lt;p&gt;Call stack exhaustion in the parser suggests that the crash is happening in some kind of large, automatically generated file. But what file is it?&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/strace-mascot.png" alt="A cartoon ostrich with a light orange head, yellow eyes, orange beak and feet, and a coat of black and a couple shades of grey." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Der Strauß, the strace mascot. CC BY-SA 4.0, by Vitaly Chaykovsky.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;It&amp;rsquo;s hard to tell by looking at the core file itself, but we have another dimension of debugging at hand: syscall tracers! These tools print out all the &lt;abbr title="System calls: calls from user programs into the kernel"&gt;syscalls&lt;/abbr&gt; made by a process. Most OSes have some means to trace syscalls: &lt;a href="https://strace.io/"&gt;&lt;code&gt;strace&lt;/code&gt;&lt;/a&gt; on Linux, &lt;a href="https://opensource.apple.com/source/dtrace/dtrace-147/DTTk/dtruss.auto.html"&gt;&lt;code&gt;dtruss&lt;/code&gt;&lt;/a&gt; on macOS, &lt;a href="https://en.wikipedia.org/wiki/Process_Monitor"&gt;Process Monitor&lt;/a&gt; on Windows, and &lt;a href="https://illumos.org/man/1/truss"&gt;&lt;code&gt;truss&lt;/code&gt;&lt;/a&gt; on illumos&lt;sup id="fnref:7"&gt;&lt;a href="#fn:7" class="footnote-ref" role="doc-noteref"&gt;7&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;p&gt;Since we&amp;rsquo;re interested in file reads, we can try filtering it down to &lt;a href="https://pubs.opengroup.org/onlinepubs/9699919799/"&gt;the &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;openat&lt;/code&gt; syscalls&lt;/a&gt;. You need to open a file to read it, after all. (Alternatively, we can also simply not filter out any syscalls, dump the entire trace to a file, and then look at it afterwards.)&lt;/p&gt;&#10;&lt;p&gt;On illumos, we tell &lt;code&gt;truss&lt;/code&gt; to run &lt;code&gt;cargo build&lt;/code&gt;, filtering syscalls to &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;openat&lt;/code&gt; (&lt;code&gt;-t&lt;/code&gt;), and following child processes (&lt;code&gt;-f&lt;/code&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; truss -ft open,openat cargo build&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This prints out every file that the child &lt;code&gt;rustc&lt;/code&gt; tries to open (&lt;a href="https://gist.github.com/sunshowers/1d9d67722d10b4b3f80ac2fe42d61e7a#file-gistfile0-txt-L549-L555"&gt;full output&lt;/a&gt;):&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;20755/3:&#9;open(&amp;#34;/home/rain/dev/cranelift-codegen-test/target/debug/build/cranelift-codegen-dad37ce046df129a/out/isle_opt.rs&amp;#34;, O_RDONLY|O_CLOEXEC) = 13&#10;20755/3:&#9; Incurred fault #6, FLTBOUNDS %pc = 0xFFFFFC7FD9E74361&#10;20755/3:&#9; siginfo: SIGSEGV SEGV_ACCERR addr=0xFFFFFC7FED22CA58&#10;20755/3:&#9; Received signal #11, SIGSEGV [default]&#10;20755/3:&#9; siginfo: SIGSEGV SEGV_ACCERR addr=0xFFFFFC7FED22CA58&#10;20754/3:&#9; Received signal #18, SIGCLD, in waitid() [default]&#10;20754/3:&#9; siginfo: SIGCLD CLD_DUMPED pid=20755 status=0x000B&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It looks like the crash is in a file called &lt;code&gt;isle_opt.rs&lt;/code&gt; in the &lt;code&gt;out/&lt;/code&gt; directory. With Cargo, a file being in an &lt;code&gt;out/&lt;/code&gt; directory is a pretty strong indication that it is generated by a build script.&lt;/p&gt;&#10;&lt;p&gt;On Linux, a similar &lt;code&gt;strace&lt;/code&gt; command is:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="go"&gt;RUST_MIN_STACK=1048576 strace -fe open,openat cargo build&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This command also blames the same file, &lt;code&gt;isle_opt.rs&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;What does this file look like, anyway? &lt;a href="https://gist.githubusercontent.com/sunshowers/22b1a612ee1cb88047c456532e0f6877/raw/9403b8b644f25c600d426ee1a39e2459684244dc/gistfile0.txt"&gt;Here&amp;rsquo;s my copy.&lt;/a&gt; It&amp;rsquo;s pretty big and deeply nested! It does look large and complex enough to trigger call stack exhaustion.&lt;/p&gt;&#10;&lt;p&gt;Syscall traces would definitely be somewhat harder to get if the crash weren&amp;rsquo;t so easily reproducible. Someone smarter than me should write about how to figure this out using just the core file. The file&amp;rsquo;s fully loaded into memory so it seems like it should be possible.&lt;/p&gt;&#10;&lt;h2 id="unblocking-myself"&gt;Unblocking myself&lt;/h2&gt;&#10;&lt;p&gt;Going back to the beginning: the reason I went down this adventure was because I wanted to make an unrelated change to the Rust standard library. But the stage 0 compiler being broken meant that it was impossible to get to the point where I could build the standard library as-is, let alone test that change.&lt;/p&gt;&#10;&lt;p&gt;How can we work around this? Well, going back to basics, where did the stage 0 compiler come from? It came from Rust&amp;rsquo;s CI, and it wasn&amp;rsquo;t actually built on illumos! (Partly because there&amp;rsquo;s no publicly-available CI system running illumos.) Instead, it was cross-compiled from Linux to illumos.&lt;/p&gt;&#10;&lt;p&gt;Based on this, my coworker Joshua suggested that I try and do whatever Rust&amp;rsquo;s CI does to build a stage 0 compiler for illumos.&lt;/p&gt;&#10;&lt;p&gt;Rust&amp;rsquo;s CI uses &lt;a href="https://github.com/rust-lang/rust/tree/75ac3b633/src/ci/docker"&gt;a set of Docker images&lt;/a&gt; to build distribution artifacts. In theory, building a patched rustc should be as simple as running these commands on my Linux machine:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;#&lt;/span&gt; Check out the exact version of the stage0 compiler&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; git checkout 75ac3b633&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;#&lt;/span&gt; Make changes...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;#&lt;/span&gt; Run Docker build&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; ./src/ci/docker/run.sh dist-x86_64-illumos&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In reality, there were some Docker permissions issues due to which I had to make a couple of changes to the script. Overall, though, it was quite simple. &lt;a href="https://gist.github.com/sunshowers/2dacc5902ad4aecc50d215f018f55232"&gt;Here&amp;rsquo;s the patch&lt;/a&gt; I built the compiler with, including the changes to the CI scripts.&lt;/p&gt;&#10;&lt;p&gt;The result of building the compiler was a set of &lt;code&gt;.tar.xz&lt;/code&gt; files, just like &lt;a href="https://github.com/rust-lang/rust/blob/2d5a628a1de1d38318909a710ef37da6251e362e/src/stage0"&gt;the ones published by Rust&amp;rsquo;s CI&lt;/a&gt;. After copying the files over to my illumos machine, I wasn&amp;rsquo;t sure which tarballs to extract. So I made &lt;a href="https://gist.github.com/sunshowers/9b6774edfabbea6985881617302caf34"&gt;a small change&lt;/a&gt; to the bootstrap script to use my patched tarballs.&lt;/p&gt;&#10;&lt;p&gt;With this patch, I was able to successfully build Rust&amp;rsquo;s standard library on illumos and test my changes. Hooray! (&lt;a href="https://github.com/rust-lang/rust/pull/128259"&gt;Here&amp;rsquo;s&lt;/a&gt; what I was trying to test.)&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Update 2024-08-05: After this post was published, jyn pointed out &lt;a href="https://hachyderm.io/deck/@jyn@tech.lgbt/112906410687051157"&gt;on Mastodon&lt;/a&gt; that &lt;code&gt;cranelift-codegen&lt;/code&gt; is actually optional, and that I could have also worked around the issue by disabling it in the &lt;code&gt;rustc&lt;/code&gt; build system&amp;rsquo;s &lt;code&gt;config.toml&lt;/code&gt;. Thanks!&lt;/em&gt;&lt;/p&gt;&#10;&lt;h2 id="what-did-we-learn"&gt;What did we learn?&lt;/h2&gt;&#10;&lt;p&gt;The bug occurred due to a combination of several factors. It also revealed a few other issues, such as the lack of an environment variable workaround and some missing error reporting.&lt;/p&gt;&#10;&lt;p&gt;Here are some ways we can make the situation better, and help us have an easier time&#10;debugging similar issues in the future.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;rustc&lt;/code&gt; isn&amp;rsquo;t using &lt;code&gt;stacker&lt;/code&gt; enough.&lt;/strong&gt; The basic problem underneath it all is that the part of the &lt;code&gt;rustc&lt;/code&gt; parser that triggered the bug wasn&amp;rsquo;t calling &lt;code&gt;stacker&lt;/code&gt; often enough to make new stack segments. &lt;code&gt;rustc&lt;/code&gt; should be calling &lt;code&gt;stacker&lt;/code&gt; more than it is today.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Filed as &lt;a href="https://github.com/rust-lang/rust/issues/128422"&gt;rust-lang/rust#128422&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;stacker&lt;/code&gt; cannot detect the stack size on illumos.&lt;/strong&gt; This is something that we should fix in &lt;code&gt;stacker&lt;/code&gt;, but this is actually a secondary issue here. On other platforms, &lt;code&gt;stacker&lt;/code&gt;&amp;rsquo;s ability to detect the stack size was masking the &lt;code&gt;rustc&lt;/code&gt; bug.&lt;/p&gt;&#10;&lt;p&gt;Fixing this requires two changes:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;A &lt;a href="https://github.com/rust-lang/libc/pull/3788"&gt;PR to &lt;code&gt;libc&lt;/code&gt;&lt;/a&gt; to add the &lt;code&gt;pthread_attr_get_np&lt;/code&gt; function to it.&lt;/li&gt;&#10;&lt;li&gt;A &lt;a href="https://github.com/rust-lang/stacker/pull/88"&gt;PR to &lt;code&gt;stacker&lt;/code&gt;&lt;/a&gt; to use this function to detect the stack size on illumos.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;stacker&lt;/code&gt;-created segments don&amp;rsquo;t print a nice message on stack exhaustion.&lt;/strong&gt; This is a bit ironic because &lt;code&gt;stacker&lt;/code&gt; is supposed to prevent stack exhaustion. But when it does happen, it would be nice if &lt;code&gt;stacker&lt;/code&gt; printed out a message like standard Rust does.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;This is &lt;a href="https://github.com/rust-lang/stacker/issues/59"&gt;rust-lang/stacker#59&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;On illumos, the Rust runtime doesn&amp;rsquo;t print a message on stack exhaustion.&lt;/strong&gt; Separate from the previous point, on illumos the Rust runtime doesn&amp;rsquo;t print a message on stack exhaustion even when using native stacks.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Filed as &lt;a href="https://github.com/rust-lang/rust/issues/128568"&gt;rust-lang/rust#128568&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Rust&amp;rsquo;s CI doesn&amp;rsquo;t run on illumos.&lt;/strong&gt; At Oxide, we have an existential dependency on Rust targeting illumos. Even a shadow CI that ran on nightly releases would have caught this issue right away.&lt;/p&gt;&#10;&lt;p&gt;We&amp;rsquo;re discussing the possibilities for this internally; stay tuned!&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;stacker&lt;/code&gt; segment sizes can&amp;rsquo;t be controlled via the environment.&lt;/strong&gt; Being able to control stack sizes with &lt;code&gt;RUST_MIN_STACK&lt;/code&gt; is a great way to work around issues. It doesn&amp;rsquo;t appear that &lt;code&gt;stacker&lt;/code&gt; segment sizes can be controlled in this manner. Maybe that functionality should be added to &lt;code&gt;rustc&lt;/code&gt;, or to &lt;code&gt;stacker&lt;/code&gt; itself?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Opened a &lt;a href="https://internals.rust-lang.org/t/allow-controlling-rustc-stacker-segment-sizes-via-the-environment/21292"&gt;discussion on internals.rust-lang.org&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Maybe a &lt;a href="https://github.com/rust-lang/crater"&gt;crater&lt;/a&gt; run with a smaller stack size?&lt;/strong&gt; It would be interesting to see if there are other parts of the Rust codebase that need to call &lt;code&gt;stacker&lt;/code&gt; more as well.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;x.py&lt;/code&gt; suggests disabling optional components.&lt;/strong&gt; Since &lt;code&gt;cranelift-codegen&lt;/code&gt; was an optional component that can be disabled, the &lt;code&gt;x.py&lt;/code&gt; tooling&#10;could notice if a build failed in such a component, and recommend disabling that component. &lt;em&gt;Added 2024-08-05, suggested &lt;a href="https://hachyderm.io/deck/@jyn@tech.lgbt/112906451310138312"&gt;by jyn&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;To me, this is the most exciting part of debugging: what kinds of changes can we make, both&#10;specific and systemic ones, to make life easier for our future selves?&lt;/p&gt;&#10;&lt;h2 id="conclusion-and-credits"&gt;Conclusion and credits&lt;/h2&gt;&#10;&lt;p&gt;This was a really fun debugging experience because I got to learn about several illumos debugging&#10;tools, and also because we could synthesize information from several sources to figure out a complex&#10;issue. (Thankfully, the root cause was straightforward, with no memory corruption or other &amp;ldquo;spooky&#10;action at a distance&amp;rdquo; involved.)&lt;/p&gt;&#10;&lt;p&gt;Debugging this was a real team effort. I couldn&amp;rsquo;t have done it without the assistance of several of&#10;my exceptional colleagues. In no particular order:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://m.unix.house/@jmc"&gt;Joshua M. Clulow&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://mattkeeter.com"&gt;Matt Keeter&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://discuss.systems/@cross"&gt;Dan Cross&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://cliffle.com/"&gt;Cliff Biffle&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://steveklabnik.com"&gt;Steve Klabnik&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://artemis.sh"&gt;artemis everfree&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Thanks to all of you!&lt;/p&gt;&#10;&lt;/div&gt;</description></item><item><title>Professionals demonstrate empathy</title><link>https://sunshowers.io/posts/be-an-adult/</link><pubDate>Tue, 02 Jul 2024 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/be-an-adult/</guid><description>&lt;p&gt;As of this writing, it appears that another open-source project lead has rejected a proposal to replace gendered &lt;em&gt;he&lt;/em&gt; language with &lt;em&gt;they&lt;/em&gt;, citing vague concerns about &amp;ldquo;politics.&amp;rdquo; (Please refrain from contacting the project about this; it&amp;rsquo;s counterproductive.)&lt;/p&gt;&#10;&lt;p&gt;At Oxide, one of our &lt;a href="https://oxide.computer/principles"&gt;core values&lt;/a&gt; is empathy: the ability to understand others&amp;rsquo; perspectives. I believe empathy is essential in our professional lives. While we are naturally inclined to empathize, applying it effectively is a skill that requires ongoing effort. (I certainly haven&amp;rsquo;t always been perfect at it!)&lt;/p&gt;</description></item><item><title>ECC RAM on AMD Ryzen 7000 desktop CPUs</title><link>https://sunshowers.io/posts/am5-ryzen-7000-ecc-ram/</link><pubDate>Mon, 09 Oct 2023 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/am5-ryzen-7000-ecc-ram/</guid><description>&lt;h1 id="introduction"&gt;Introduction&lt;/h1&gt;&#10;&lt;p&gt;One of the coolest features of AMD&amp;rsquo;s Ryzen desktop CPUs, and historically a great reason to get them&#10;over the competition, was the official support for error-corrected memory (ECC RAM)&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. With most Ryzen&#10;1000 through 5000 series CPUs and the right motherboards, ordinary users could get ECC RAM going&#10;without having to spring for more expensive workstation-grade CPUs.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;a href="./images/b550-specs.png"&gt;&#10; &#10; &lt;img src="./images/b550-specs.png" alt="Screenshot of B550 Steel Legend specification page, showing support for ECC &amp;amp; non-ECC, unbuffered memory" /&gt;&#10; &#10; &lt;/a&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Specification page for the B550 Steel Legend motherboard.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;For example, here&amp;rsquo;s the &lt;a href="https://www.asrock.com/mb/AMD/B550%20Steel%20Legend/index.asp#Specification"&gt;specification&#10;page&lt;/a&gt; for the ASRock&#10;B550 Steel Legend motherboard. This is a mainstream &amp;ldquo;B&amp;rdquo; series motherboard which lists detailed&#10;compatibility information for ECC RAM by processor generation.&lt;/p&gt;</description></item><item><title>Dealing with tempfile cleaners</title><link>https://sunshowers.io/posts/tmp-cleaner/</link><pubDate>Thu, 05 Oct 2023 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/tmp-cleaner/</guid><description>&lt;p&gt;&lt;em&gt;I used to use this as a place to write long-form thoughts, but with the demise of Twitter I want to&#10;also write short-form posts here under the &lt;a href="./tags/shorts/"&gt;#shorts&lt;/a&gt; tag. I also post &lt;a href="https://hachyderm.io/@rain"&gt;on&#10;Mastodon&lt;/a&gt; but that&amp;rsquo;s a little more ephemeral and less searchable than&#10;this blog.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Let&amp;rsquo;s say you&amp;rsquo;re creating a temporary directory with some cached artifacts to share across several&#10;tests. It&amp;rsquo;s a bit more ephemeral than, say, the Cargo &lt;code&gt;target/&lt;/code&gt; directory, so your initial idea&#10;might be to just put your artifacts in &lt;a href="https://doc.rust-lang.org/nightly/std/env/fn.temp_dir.html"&gt;the temporary&#10;directory&lt;/a&gt;. Also, let&amp;rsquo;s say that you&#10;want to reuse the directory across a span of several days or more (perhaps by hashing a set of&#10;inputs and using that to name the directory).&lt;/p&gt;</description></item><item><title>How (and why) nextest uses tokio</title><link>https://sunshowers.io/posts/nextest-and-tokio/</link><pubDate>Mon, 03 Oct 2022 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/nextest-and-tokio/</guid><description>&lt;div class="sectioned-post"&gt;&#10;&lt;h2 id="1-introduction"&gt;1. Introduction&lt;/h2&gt;&#10;&lt;p&gt;&lt;em&gt;Update 2022-11-03: This was originally the first in a series of two blog posts. I&amp;rsquo;ve now marked this as a standalone post. Content originally slated for part 2 will now be published as followup posts.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m the primary author and maintainer of &lt;a href="https://nexte.st/"&gt;cargo-nextest&lt;/a&gt;, a next-generation test&#10;runner for Rust released earlier this year. (It reached a thousand stars on GitHub recently; &lt;a href="https://github.com/nextest-rs/nextest"&gt;go&#10;star it here!&lt;/a&gt;)&lt;/p&gt;&#10;&lt;div class="center"&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/nextest-rs/nextest"&gt;&lt;img src="https://img.shields.io/github/stars/nextest-rs/nextest?style=social" alt="GitHub stars shield"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Nextest is &lt;a href="https://nexte.st/book/benchmarks.html"&gt;faster than &lt;code&gt;cargo test&lt;/code&gt;&lt;/a&gt; for most Rust projects,&#10;and provides a number of extra features, such as &lt;a href="https://nexte.st/book/retries"&gt;test retries&lt;/a&gt;,&#10;&lt;a href="https://nexte.st/book/reusing-builds"&gt;reusing builds&lt;/a&gt;, and &lt;a href="https://nexte.st/book/partitioning"&gt;partitioning (sharding) test&#10;runs&lt;/a&gt;. To power these features, nextest uses&#10;&lt;a href="https://tokio.rs/"&gt;Tokio&lt;/a&gt;, the leading Rust async runtime for writing network applications.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Wait, what?&lt;/em&gt; Nextest isn&amp;rsquo;t a network app at all&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. However, nextest has to juggle&#10;multiple concurrent tests, each of which can be in the process of starting, printing output,&#10;failing, timing out, being cancelled or even forcibly stopped. In this post, I&amp;rsquo;ll try to convince&#10;you that an async runtime is actually the perfect approach to handle a large number of heterogenous&#10;events like this.&lt;/p&gt;&#10;&lt;h3 id="theres-more-than-one-kind-of-distributed-system"&gt;There&amp;rsquo;s more than one kind of distributed system&lt;/h3&gt;&#10;&lt;p&gt;The challenges posed by interprocess communication &lt;em&gt;within a system&lt;/em&gt; are really similar to those&#10;posed by remote communication &lt;em&gt;across systems&lt;/em&gt;. Your computer is &lt;a href="http://catern.com/compdist.html"&gt;a distributed&#10;system&lt;/a&gt;. Just like most network services, nextest spends almost all&#10;of its time waiting rather than computing.&lt;/p&gt;&#10;&lt;p&gt;In other words, nextest provides the prototypical use-case for &lt;em&gt;asynchronous concurrency&lt;/em&gt;, one of&#10;the two main paradigms for non-sequential code.&lt;/p&gt;&#10;&#10; &lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/io-compute-bound.png" alt="A simple diagram showing the two main paradigms for non-sequential code. To the left is &amp;#39;I/O-bound&amp;#39; with &amp;#39;Async concurrency (Tokio)&amp;#39;, and to the right is &amp;#39;Compute-bound&amp;#39; with &amp;#39;Data Parallelism (Rayon)&amp;#39;" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;ul&gt;&#10;&lt;li&gt;The async concurrency style is best for I/O-bound applications, where most time is spent waiting&#10;on other parts of the overall system to respond. An I/O-bound application becomes faster if I/O&#10;becomes faster&amp;mdash;for example, if your network link becomes faster, or you replace a spinning disk&#10;hard drive with a solid-state drive.&lt;/li&gt;&#10;&lt;li&gt;On the other end of the spectrum, &lt;em&gt;data parallelism&lt;/em&gt; with libraries like&#10;&lt;a href="https://github.com/rayon-rs/rayon"&gt;Rayon&lt;/a&gt; is best for compute-bound applications, where you&amp;rsquo;re&#10;running a lot of computations in parallel on a set of input data. These become faster if you replace&#10;your CPU with a faster one.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Why is an async runtime like Tokio good for I/O-bound applications? The answer has to do with&#10;&lt;em&gt;heterogenous selects&lt;/em&gt;&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&#10;&lt;h3 id="what-are-heterogenous-selects"&gt;What are heterogenous selects?&lt;/h3&gt;&#10;&lt;p&gt;You might have heard of the &lt;em&gt;select&lt;/em&gt; operation: in computer parlance, it means to observe&#10;two or more operations, and then to proceed with the first one that completes.&lt;/p&gt;&#10;&lt;p&gt;The name of the operation comes from the Unix&#10;&lt;a href="https://man7.org/linux/man-pages/man2/select.2.html"&gt;&lt;code&gt;select&lt;/code&gt;&lt;/a&gt; syscall. With &lt;code&gt;select&lt;/code&gt; and its successors&#10;like &lt;a href="https://man7.org/linux/man-pages/man7/epoll.7.html"&gt;&lt;code&gt;epoll&lt;/code&gt;&lt;/a&gt;, you can typically only wait on one of&#10;several I/O read or write operations (represented through file descriptors). But real-world&#10;I/O-bound programs need to do much more than that! Here are some other kinds of operations they may&#10;need to wait on:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Timeouts or recurring timers&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://tokio.rs/tokio/tutorial/channels"&gt;Channel reads and writes&lt;/a&gt; when using message-passing,&#10;including bounded channels with backpressure&lt;/li&gt;&#10;&lt;li&gt;Process exits&lt;/li&gt;&#10;&lt;li&gt;Unix signals&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;I/O-bound programs often need to wait across several of these sources at the same time. For example,&#10;it&amp;rsquo;s common for services to make a network request, then select across:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;a network response&lt;/li&gt;&#10;&lt;li&gt;a timeout&lt;/li&gt;&#10;&lt;li&gt;a termination signal, so any pending network operations can be canceled&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This is an example of a &lt;em&gt;heterogenous select&lt;/em&gt; (or, spelled slightly differently, a &lt;em&gt;heterogeneous select&lt;/em&gt;): a select operation that can span several different&#10;kinds of sources.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;We also run into portability considerations: &lt;code&gt;select&lt;/code&gt; is Unix-specific, and &lt;code&gt;epoll&lt;/code&gt; is for the most&#10;part Linux-only&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;. Other operating systems have their own &lt;code&gt;epoll&lt;/code&gt;-like abstractions, each&#10;with their own peculiarities:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;BSD-like platforms have &lt;code&gt;kqueue&lt;/code&gt; (&lt;a href="https://www.freebsd.org/cgi/man.cgi?kqueue"&gt;FreeBSD&lt;/a&gt;, &lt;a href="https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/kqueue.2.html"&gt;macOS&lt;/a&gt;).&lt;/li&gt;&#10;&lt;li&gt;Windows has &lt;a href="https://docs.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports"&gt;I/O completion ports&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Some platforms may also support selecting across some kinds of heterogenous sources. For example,&#10;Linux has &lt;a href="https://man7.org/linux/man-pages/man2/signalfd.2.html"&gt;&lt;code&gt;signalfd&lt;/code&gt;&lt;/a&gt; and&#10;&lt;a href="https://man7.org/linux/man-pages/man2/timerfd_create.2.html"&gt;&lt;code&gt;timerfd&lt;/code&gt;&lt;/a&gt;. To the extent that these&#10;are supported, there are significant differences in the APIs available on each platform. And all of&#10;them make selecting across channels and other user code quite difficult.&lt;/p&gt;&#10;&lt;p&gt;The divergence in platform APIs and capabilities may not be a huge deal for certain kinds of network&#10;services that only target one platform, but is a real problem for a cross-platform developer tool&#10;like nextest.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;So there are two separate but related sets of problems here:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The need to select over arbitrary kinds of heterogenous sources, not just I/O operations.&lt;/li&gt;&#10;&lt;li&gt;The need to do so in a cross-platform manner.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Async runtimes like Tokio solve both sets of problems in one go&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;. In fact, I&amp;rsquo;d like to make a bold claim here:&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;The ability to do heterogenous selects is &lt;em&gt;the point of async Rust&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;&#10;&lt;div class="framed"&gt;&#10;&lt;h3 id="async-runtimes-vs-asyncawait"&gt;Async runtimes vs async/await&lt;/h3&gt;&#10;&lt;p&gt;At this point it&amp;rsquo;s worth mentioning that there are two related, but distinct, things here:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;An &lt;em&gt;async runtime&lt;/em&gt;: a framework that manages an event loop, handling events from heterogenous sources and associating them with individual tasks.&lt;/li&gt;&#10;&lt;li&gt;The &lt;em&gt;&lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords&lt;/em&gt;, which provide an easy way to write tasks that wait on events handled by an async runtime.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;It is definitely possible to leverage async runtimes without ever using the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords&amp;mdash;in fact, I&amp;rsquo;ve written large chunks of a production service in Rust that used an async runtime before &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; was ever a thing.&lt;/p&gt;&#10;&lt;p&gt;However, without syntactic support it is often very difficult to do so, since you may need to hand-write complicated state machines. The &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords dramatically simplify the experience of writing state machines. David Tolnay talks about this more &lt;a href="https://docs.rs/dtolnay/latest/dtolnay/macro._01__await_a_minute.html#native-control-flow"&gt;in his blog post&lt;/a&gt;. (The &lt;code&gt;poll_next&lt;/code&gt;/&lt;code&gt;EncodeState&lt;/code&gt; example in his blog post was part of the aforementioned production service, and it was my code that he rewrote.)&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 id="2-the-initial-implementation"&gt;2. The initial implementation&lt;/h2&gt;&#10;&lt;p&gt;With this background, we&amp;rsquo;re now ready to start looking at how nextest&amp;rsquo;s current architecture came about.&lt;/p&gt;&#10;&lt;p&gt;The first versions of nextest all ran tests using &amp;ldquo;normal&amp;rdquo; threads. Here&amp;rsquo;s the general structure&#10;they used for their &lt;a href="https://nexte.st/book/how-it-works.html"&gt;execution model&lt;/a&gt;:&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/nextest-runner-flowchart.png" alt="A flowchart showing the main ways data moves around among nextest&amp;#39;s components. See below for a text description." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;The main ways data moves around among nextest&amp;rsquo;s components.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;&lt;em&gt;The run pool:&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Tests were run via a thread pool.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Nextest first &lt;a href="https://nexte.st/book/how-it-works.html#the-nextest-model"&gt;built up&lt;/a&gt; a list of tests.&lt;/li&gt;&#10;&lt;li&gt;It then spun up a thread pool with the number of tests&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;, called the &lt;em&gt;run pool&lt;/em&gt;.&lt;/li&gt;&#10;&lt;li&gt;For each test in the list, nextest used a thread in the run pool. The thread ran the test using&#10;the &lt;a href="https://docs.rs/duct/"&gt;duct&lt;/a&gt; library, then blocked until the test exited. (nextest used duct for convenience&amp;mdash;we could have used &lt;code&gt;std::process::Command&lt;/code&gt; instead, which wouldn&amp;rsquo;t have changed much.)&lt;/li&gt;&#10;&lt;li&gt;To send information about test events (the start, end, success/failure, and time taken for&#10;individual tests) to the main thread, nextest used a &lt;a href="https://docs.rs/crossbeam-channel/0.5/crossbeam_channel/"&gt;crossbeam MPSC&#10;channel&lt;/a&gt;&lt;sup id="fnref:6"&gt;&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref"&gt;6&lt;/a&gt;&lt;/sup&gt;: we&amp;rsquo;ll call this the&#10;&lt;em&gt;test event channel&lt;/em&gt;.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;The signal thread:&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;To handle signals such as Ctrl-C cleanly, nextest spun up a separate thread, and if a signal is&#10;detected, wrote to another channel: the &lt;em&gt;signal event channel&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;The main thread:&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;The main thread was dedicated to monitoring the two event channels. This thread would&#10;&lt;a href="https://docs.rs/crossbeam-channel/0.5/crossbeam_channel/macro.select.html"&gt;select&lt;/a&gt; across&#10;the &lt;a href="https://github.com/nextest-rs/nextest/blob/ce98c9405bfe512e2f411d78bfe04468722ae69b/nextest-runner/src/runner.rs#L267-L287"&gt;&lt;em&gt;test event&lt;/em&gt; and &lt;em&gt;signal event&lt;/em&gt;&#10;channels&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// InternalEvent has this definition:&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[derive(Debug)]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;InternalEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;&amp;#39;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InternalTestEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;&amp;#39;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SignalEvent&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Note that receiving an Err() from a receiver (a RecvError) means that there are&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// no more senders.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;crossbeam_channel&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_receiver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// internal_event is a Result&amp;lt;InternalTestEvent, RecvError&amp;gt;.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;InternalEvent&lt;/span&gt;::&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// All senders were dropped -- the test run completed.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal_receiver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// internal_event is a Result&amp;lt;SignalEvent, RecvError&amp;gt;.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;InternalEvent&lt;/span&gt;::&lt;span class="n"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Ignore the signal thread being dropped. This is done for&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// noop signal handlers.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... process internal_event&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;If this loop received a test event, it would bubble that up to the reporter (the component that&#10;prints nextest&amp;rsquo;s output). If a test failed, it would set a cancellation flag which would cause no&#10;further tests to be scheduled.&lt;/li&gt;&#10;&lt;li&gt;If the loop received a signal event, it would set the cancellation flag.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Importantly, &lt;code&gt;crossbeam_channel::select!&lt;/code&gt; only lets you select over crossbeam channels, not&#10;heterogenous sources in general. Anything else, including pipe reads or timers, must be translated&#10;over to crossbeam channels somehow.&lt;/p&gt;&#10;&lt;h3 id="the-first-issues-with-the-model"&gt;The first issues with the model&lt;/h3&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/nextest-runner-with-wait.png" alt="A flowchart showing how nextest&amp;#39;s runner loop detected slow tests. See below for a text description." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;How nextest&amp;rsquo;s original runner loop detected slow tests.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;One of the first demands placed on nextest was the need to identify &lt;a href="https://nexte.st/book/slow-tests"&gt;slow&#10;tests&lt;/a&gt;. Note that duct&amp;rsquo;s&#10;&lt;a href="https://docs.rs/duct/0.13/duct/struct.Handle.html#method.wait"&gt;&lt;code&gt;Handle::wait&lt;/code&gt;&lt;/a&gt;&#10;method doesn&amp;rsquo;t have any notion of a timeout: it just blocks until the process is completed.&lt;/p&gt;&#10;&lt;p&gt;Luckily, &lt;code&gt;crossbeam_channel&lt;/code&gt; has a &lt;a href="https://docs.rs/crossbeam-channel/0.5/crossbeam_channel/struct.Receiver.html#method.recv_timeout"&gt;&lt;code&gt;recv_timeout&lt;/code&gt;&#10;method&lt;/a&gt;.&#10;So the solution was straightforward: set up another thread pool, called the &lt;em&gt;wait pool&lt;/em&gt;, which would wait on test processes to exit.&lt;/p&gt;&#10;&lt;p&gt;So now this becomes:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Start the test process using a thread in the run pool.&lt;/li&gt;&#10;&lt;li&gt;Spawn a wait pool thread to wait on the test.&lt;/li&gt;&#10;&lt;li&gt;In the run pool, wait on the thread in the wait pool with a timeout.&#10;&lt;ul&gt;&#10;&lt;li&gt;If the test finishes before the timeout, mark it done.&lt;/li&gt;&#10;&lt;li&gt;If the timeout is hit, mark the test slow and go back to waiting on the test.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/nextest-rs/nextest/blob/5b82369cb42209c7f513ee686faa3dd340d971b7/nextest-runner/src/runner.rs#L358-L389"&gt;Here&amp;rsquo;s the code&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;in_place_scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;crossbeam_channel&lt;/span&gt;::&lt;span class="n"&gt;bounded&lt;/span&gt;::&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;wait_handle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Spawn a task on the threadpool that waits for the test to finish.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;wait_handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(());&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Continue waiting for the test to finish with a timeout, logging at&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// slow-timeout intervals.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv_timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slow_timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RecvTimeoutError&lt;/span&gt;::&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... bubble up the slow event so the reporter can print out a&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// SLOW line&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RecvTimeoutError&lt;/span&gt;::&lt;span class="n"&gt;Disconnected&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;unreachable!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Waiting thread should never drop the sender&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;});&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;into_output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// ... process output&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;This works, but you can already see the cracks starting to show. To solve this issue we had to&#10;introduce a whole new thread pool on top of the existing run pool.&lt;/p&gt;&#10;&lt;h3 id="terminating-tests-using-a-polling-loop"&gt;Terminating tests using a polling loop&lt;/h3&gt;&#10;&lt;p&gt;A few months after nextest was released, a user requested another feature: the ability to terminate&#10;tests that take more than a certain amount of time.&lt;/p&gt;&#10;&lt;p&gt;Now, nextest &lt;em&gt;could&lt;/em&gt; terminate tests immediately with the unblockable &lt;a href="https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html"&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&#10;signal&lt;/a&gt;&lt;sup id="fnref:7"&gt;&lt;a href="#fn:7" class="footnote-ref" role="doc-noteref"&gt;7&lt;/a&gt;&lt;/sup&gt;&#10;(aka &lt;code&gt;kill -9&lt;/code&gt;). But it&amp;rsquo;s generally a good idea to give tests a bit of a grace period. So we chose&#10;to first send tests a &lt;code&gt;SIGTERM&lt;/code&gt; signal, then wait a bit for them to clean up before sending&#10;&lt;code&gt;SIGKILL&lt;/code&gt;.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/nextest-runner-with-wait-and-timeout.png" alt="A flowchart to show how nextest&amp;#39;s runner loop implemented test timeouts. See below for text description." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;How nextest&amp;rsquo;s original runner loop implemented test timeouts.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;To do this, we defined two timeouts:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A &lt;em&gt;slow timeout&lt;/em&gt;, after which tests are marked as slow. Nextest&amp;rsquo;s default slow timeout is 60 seconds.&lt;/li&gt;&#10;&lt;li&gt;A &lt;em&gt;termination timeout&lt;/em&gt;, after which tests are killed. Nextest doesn&amp;rsquo;t have a default termination timeout, but a typical one users might set is 5 minutes (300 seconds).&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The test execution flow is then:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Start the test process using a thread in the run pool.&lt;/li&gt;&#10;&lt;li&gt;Spawn a wait pool thread to wait on the test.&lt;/li&gt;&#10;&lt;li&gt;In the run pool, wait on the thread in the wait pool with the slow timeout.&#10;&lt;ul&gt;&#10;&lt;li&gt;If the test finishes before the slow timeout, mark it done.&lt;/li&gt;&#10;&lt;li&gt;If the slow timeout is hit and the termination timeout hasn&amp;rsquo;t been hit yet, mark the test as slow and go back to waiting on it.&lt;/li&gt;&#10;&lt;li&gt;If the termination timeout is hit, set up a termination loop.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;And the termination loop:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Send the process &lt;code&gt;SIGTERM&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;In a loop, for up to 10 seconds:&#10;&lt;ul&gt;&#10;&lt;li&gt;Check if the test process exited.&lt;/li&gt;&#10;&lt;li&gt;If it did, break out of the loop.&lt;/li&gt;&#10;&lt;li&gt;Otherwise, wait 100ms and check the test&amp;rsquo;s status again.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;If the test hasn&amp;rsquo;t exited by the end of the loop, send it &lt;code&gt;SIGKILL&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/nextest-rs/nextest/blob/ce98c9405bfe512e2f411d78bfe04468722ae69b/nextest-runner/src/runner.rs#L412-L473"&gt;Here&amp;rsquo;s the code&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout_hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RecvTimeoutError&lt;/span&gt;::&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... bubble up the slow event to the reporter&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout_hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;slow_timeout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout_hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Attempt to terminate the slow test.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// As there is a race between shutting down a slow test and its&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// own completion, we silently ignore errors to avoid printing&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// false warnings.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cp"&gt;#[cfg(unix)]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;exited&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;duct&lt;/span&gt;::&lt;span class="n"&gt;unix&lt;/span&gt;::&lt;span class="n"&gt;HandleExt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;use&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;libc&lt;/span&gt;::&lt;span class="no"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send_signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// give the process a grace period of 10s&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;try_wait&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// in case of an error we&amp;#39;ll send SIGKILL nonetheless&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;::&lt;span class="n"&gt;thread&lt;/span&gt;::&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;::&lt;span class="n"&gt;from_millis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cp"&gt;#[cfg(not(unix))]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;exited&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;exited&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ExecutionResult&lt;/span&gt;::&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;You can already start seeing the code becoming long and rather janky, with its 100ms polling loop.&lt;/p&gt;&#10;&lt;h3 id="detecting-leaked-handles"&gt;Detecting leaked handles&lt;/h3&gt;&#10;&lt;p&gt;Some tests spawn server or other subprocesses, and sometimes those subprocesses don&amp;rsquo;t get cleaned up&#10;at the end of the test. In particular, nextest is concerned about processes that&#10;&lt;a href="https://doc.rust-lang.org/std/process/struct.Stdio.html#method.inherit"&gt;&lt;em&gt;inherit&lt;/em&gt;&lt;/a&gt; standard output&#10;and/or standard error. Here&amp;rsquo;s an example test:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#[test]&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;test_subprocess_doesnt_exit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Spawn a subprocess that sleeps for 2 minutes, then exits.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;::&lt;span class="n"&gt;process&lt;/span&gt;::&lt;span class="n"&gt;Command&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;sleep&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;120&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// This sleep command inherits standard output and standard error from the test.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Since &lt;code&gt;sleep&lt;/code&gt; inherits standard output and standard error from the test, it keeps those file&#10;descriptors open until it exits. Previously, nextest would simply wait until those subprocesses&#10;exited. In the worst case, the subprocesses wouldn&amp;rsquo;t exit at all: think of a server process that&#10;gets started but never shut down. That can result in nextest simply hanging, which isn&amp;rsquo;t great.&lt;/p&gt;&#10;&lt;p&gt;How can nextest detect &lt;a href="https://nexte.st/book/leaky-tests.html"&gt;these sorts of &lt;em&gt;leaky&lt;/em&gt; tests&lt;/a&gt;? One&#10;solution is to wait for standard output and standard error to be closed until a short duration (say&#10;100ms) passes. While this is conceptually simple, &lt;em&gt;implementing it&lt;/em&gt; is surprisingly complicated.&#10;Waiting on one handle being closed is nontrivial; waiting on &lt;em&gt;two&lt;/em&gt; handles being closed (standard&#10;output and standard error) adds an extra challenge.&lt;/p&gt;&#10;&lt;p&gt;I spent some time looking at how to implement it manually, and came away from it with the&#10;understanding that I&amp;rsquo;d have to maintain separate implementations for Windows and Unix. Not only&#10;that, I&amp;rsquo;d just be duplicating a lot of the work Tokio had already done to abstract over platform&#10;differences. So I started looking at porting over nextest to Tokio.&lt;/p&gt;&#10;&lt;h2 id="3-using-tokio"&gt;3. Using Tokio&lt;/h2&gt;&#10;&lt;p&gt;I spent a weekend porting over nextest to use async Rust with Tokio. After a bit of iteration, I&#10;settled on this general architecture:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Create a &lt;a href="https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html"&gt;Tokio &lt;code&gt;Runtime&lt;/code&gt;&lt;/a&gt; in the&#10;beginning. The &lt;code&gt;Runtime&lt;/code&gt; lasts for as long as the runner loop does.&lt;/li&gt;&#10;&lt;li&gt;Turn the list of tests into a&#10;&lt;a href="https://docs.rs/futures/0.3/futures/stream/trait.Stream.html"&gt;&lt;code&gt;Stream&lt;/code&gt;&lt;/a&gt;, then for each test,&#10;create a future that executes the test. Use&#10;&lt;a href="https://docs.rs/futures/0.3/futures/stream/trait.StreamExt.html#method.buffer_unordered"&gt;&lt;code&gt;buffer_unordered&lt;/code&gt;&lt;/a&gt;&#10;to control concurrency&lt;sup id="fnref:8"&gt;&lt;a href="#fn:8" class="footnote-ref" role="doc-noteref"&gt;8&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;&#10;&lt;li&gt;Switch over crossbeam channels to &lt;a href="https://docs.rs/tokio/1/tokio/sync/mpsc/index.html"&gt;tokio&#10;channels&lt;/a&gt; as needed, and&#10;&lt;code&gt;crossbeam_channel::select!&lt;/code&gt; invocations &lt;a href="https://docs.rs/tokio/1/tokio/macro.select.html"&gt;to&#10;&lt;code&gt;tokio::select!&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Here&amp;rsquo;s the bit that &lt;a href="https://github.com/nextest-rs/nextest/blob/8703af50b649fc882e601d4eb01f7c2c104c0395/nextest-runner/src/runner.rs#L325-L347"&gt;selects over the test event and signal event channels&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;signals_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;run_receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// internal_event is an Option&amp;lt;InternalTestEvent&amp;gt;.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;InternalEvent&lt;/span&gt;::&lt;span class="n"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// All senders were dropped -- the test run completed.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;signal_handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;signals_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// internal_event is an Option&amp;lt;SignalEvent&amp;gt;.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;internal_event&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;InternalEvent&lt;/span&gt;::&lt;span class="n"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;signals_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... process internal_event&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;This is really similar to the crossbeam-based implementation above, with just one real change: The&#10;code tracks a &lt;code&gt;signals_done&lt;/code&gt; state to see if the signal event channel has been dropped. This is to&#10;ensure that the loop doesn&amp;rsquo;t repeatedly get &lt;code&gt;None&lt;/code&gt; values from the signal handler, wasting cycles.&#10;(This is more important with futures that, once completed, cannot be waited on further, as we&amp;rsquo;ll&#10;see in the next section.)&lt;/p&gt;&#10;&lt;h3 id="waiting-on-process-completion-with-tokio"&gt;Waiting on process completion with Tokio&lt;/h3&gt;&#10;&lt;p&gt;Where this gets really interesting is with the loop nextest uses to wait for tests to complete. It&#10;turns out that &lt;em&gt;all&lt;/em&gt; the code we wrote above can be replaced with a set of Rust and Tokio&#10;primitives, written in a roughly declarative style.&lt;/p&gt;&#10;&lt;p&gt;After starting the process, nextest &lt;a href="https://github.com/nextest-rs/nextest/blob/e11c35547511e728a422cabdcd04ea542df3bef5/nextest-runner/src/runner.rs#L600-L648"&gt;sets&#10;up&lt;/a&gt;&#10;a timer, and futures to read from standard output and standard error:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Set up a timer to produce events at predefined intervals.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;time&lt;/span&gt;::&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow_timeout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// The first tick is immediate.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stdout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stderr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;::&lt;span class="n"&gt;BytesMut&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;::&lt;span class="n"&gt;BytesMut&lt;/span&gt;::&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_all_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;::&lt;span class="n"&gt;BytesMut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;: &lt;span class="kp"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AsyncRead&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Unpin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Send&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-&amp;gt; &lt;span class="nc"&gt;std&lt;/span&gt;::&lt;span class="n"&gt;io&lt;/span&gt;::&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Reborrow it as AsyncReadExt::read_buf expects&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Sized self.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes_read&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_buf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;bytes_read&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(());&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Set up futures for reading from stdout and stderr.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_fut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stdout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;read_all_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stdout&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;pin!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout_fut&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_fut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stderr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;read_all_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child_stderr&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;pin!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr_fut&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;Some notes about the code above:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;On being polled, the futures repeatedly read standard output and standard error into their&#10;corresponding buffers in 4 KiB chunks. They only exit when 0 bytes are read, which corresponds&#10;to the handle being closed.&lt;/li&gt;&#10;&lt;li&gt;The code needs to maintain &lt;code&gt;stdout_done&lt;/code&gt; and &lt;code&gt;stderr_done&lt;/code&gt; flags: this is similar to&#10;&lt;code&gt;signals_done&lt;/code&gt; above, except this time it&amp;rsquo;s necessary because async blocks cannot be polled once&#10;completed: if done so, they panic with the message &amp;ldquo;&lt;code&gt;async fn&lt;/code&gt; resumed after completion&amp;rdquo;.&lt;/li&gt;&#10;&lt;li&gt;The futures borrow data from the stack. This is possible thanks to async Rust&amp;rsquo;s integration with the&#10;borrow checker.&lt;/li&gt;&#10;&lt;li&gt;The futures are &lt;a href="https://docs.rs/tokio/1/tokio/macro.pin.html"&gt;&lt;em&gt;pinned&lt;/em&gt; to the stack&lt;/a&gt;, which means&#10;that they cannot be moved. This is a &lt;a href="https://rust-lang.github.io/async-book/04_pinning/01_chapter.html"&gt;fundamental&#10;part&lt;/a&gt; of how zero-overhead&#10;async Rust works. An async block must be pinned before it can be polled.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Remember how we talked about heterogenous selects in part 1 of this post? This is where we start&#10;using them. Notice how &lt;code&gt;crossbeam_channel::select!&lt;/code&gt; only works on crossbeam channels. On the other&#10;hand, Tokio&amp;rsquo;s select works on arbitrary, heterogenous sources of asynchronicity.&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/nextest-runner-tokio.png" alt="A flowchart to show how nextest&amp;#39;s Tokio-based runner loop executes tests. See below for text description." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;How nextest&amp;rsquo;s Tokio-based runner loop executes tests.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Here&amp;rsquo;s how nextest now waits on tests:&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;Setup:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Start the test process, which sets up a future that completes when the test exits.&lt;/li&gt;&#10;&lt;li&gt;Set up a timer for the slow timeout.&lt;/li&gt;&#10;&lt;li&gt;Set up futures to read from standard output and standard error.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Then, select across the futures listed above in a loop, using Tokio&amp;rsquo;s ability to handle heterogenous&#10;selects with &lt;code&gt;tokio::select!&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;If data is available via the standard output or standard error futures:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Read data into the corresponding buffer.&lt;/li&gt;&#10;&lt;li&gt;Loop back and select again.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;If the timer fires:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;If the terminate timeout is hit, terminate the test and exit the loop.&lt;/li&gt;&#10;&lt;li&gt;If not, mark as slow, loop back and select again.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;If the process exits:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Exit the execution loop.&lt;/li&gt;&#10;&lt;li&gt;Proceed to the leak detection loop below.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/nextest-rs/nextest/blob/8703af50b649fc882e601d4eb01f7c2c104c0395/nextest-runner/src/runner.rs#L533-L570"&gt;Here&amp;rsquo;s the code&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// res is an std::io::Result&amp;lt;ExitStatus&amp;gt;.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_fut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_fut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// The test finished executing.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_none&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;is_slow&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout_hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... bubble up the slow event to the reporter&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;slow_timeout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;timeout_hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;terminate_after&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ... attempt to terminate the slow test&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ExecutionResult&lt;/span&gt;::&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;And then, how does nextest detect leaked handles? After the main loop ends, nextest just runs another little loop&#10;for that:&lt;/p&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/nextest-runner-tokio-leaks.png" alt="A flowchart to show how nextest&amp;#39;s Tokio-based runner loop detects leaked handles in tests. See below for text description." /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;How nextest&amp;rsquo;s Tokio-based runner loop detects leaked handles in tests.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;ol&gt;&#10;&lt;li&gt;Set up a short (by default 100ms) leak timer.&lt;/li&gt;&#10;&lt;li&gt;Select across the leak timer and the standard output and error futures from before, in a loop.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;If data is available via the standard output or standard error futures:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Read data into the corresponding buffer.&lt;/li&gt;&#10;&lt;li&gt;Loop back, and select again.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;If both the standard output and standard error futures have completed:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Note that the test didn&amp;rsquo;t leak.&lt;/li&gt;&#10;&lt;li&gt;Conclude test execution.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;If the timer fires before both futures have completed:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Note that the test leaked handles.&lt;/li&gt;&#10;&lt;li&gt;Conclude test execution.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/nextest-rs/nextest/blob/8703af50b649fc882e601d4eb01f7c2c104c0395/nextest-runner/src/runner.rs#L582-L609"&gt;Here&amp;rsquo;s the code&lt;/a&gt;:&lt;/p&gt;&#10;&lt;details&gt;&#10; &lt;summary&gt;Click to expand code sample&lt;/summary&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Once the process is done executing, wait up to leak_timeout for the pipes to shut&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// down. Previously, this used to hang if spawned grandchildren inherited&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// stdout/stderr but didn&amp;#39;t shut down properly. Now, nextest detects those cases and&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// marks them as leaked.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;leaked&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="n"&gt;time&lt;/span&gt;::&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leak_timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;tokio&lt;/span&gt;::&lt;span class="fm"&gt;select!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_fut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_fut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout_done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stderr_done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// stdout and/or stderr haven&amp;#39;t completed yet. In this case, break&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// the loop and mark this as leaked.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/details&gt;&#10;&lt;p&gt;That&amp;rsquo;s it! We&amp;rsquo;ve replaced all of that unwieldy code with a couple of declarative &lt;code&gt;select!&lt;/code&gt; loops.&lt;/p&gt;&#10;&lt;div class="framed"&gt;&#10;&lt;h3 id="if-it-compiles-does-it-work"&gt;If it compiles, does it work?&lt;/h3&gt;&#10;&lt;p&gt;One of the promises of Rust&amp;rsquo;s type system is that it&amp;rsquo;ll catch most kinds of bugs before you ever run&#10;the program. In my experience, this promise is &lt;em&gt;mostly&lt;/em&gt; upheld, even when dealing with async code.&#10;For a few compile-time issues like pinning, it was easy to find&#10;&lt;a href="https://tokio.rs/tokio/tutorial/select#resuming-an-async-operation"&gt;solutions&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;I ran into two runtime issues:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A panic with the message &amp;ldquo;&lt;code&gt;async fn&lt;/code&gt; resumed after completion&amp;rdquo;. The solution to this&#10;was &lt;a href="https://tokio.rs/tokio/tutorial/select#modifying-a-branch"&gt;easy to find&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;The main loop that &lt;a href="./posts/nextest-and-tokio/#cutting-the-knot"&gt;selected across&lt;/a&gt; the test event and signal event channels&#10;hung during longer test runs; this was caused by a &lt;a href="https://docs.rs/tokio/latest/tokio/macro.select.html#fairness"&gt;&lt;code&gt;biased&lt;/code&gt;&#10;statement&lt;/a&gt;. Removing the &lt;code&gt;biased&lt;/code&gt;&#10;fixed it, but I mostly found that out by trial and error. I&amp;rsquo;m not sure how to debug this kind of&#10;issue in a principled manner.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;&lt;h2 id="4-conclusion"&gt;4. Conclusion&lt;/h2&gt;&#10;&lt;p&gt;There&amp;rsquo;s a bit of a meme in the Rust world: that if you can &amp;ldquo;just use&amp;rdquo; threads, &lt;a href="https://news.ycombinator.com/item?id=31601973"&gt;you&#10;should&lt;/a&gt;; and that async code is really only &amp;ldquo;worth&#10;it&amp;rdquo; for network services that &lt;a href="https://www.reddit.com/r/rust/comments/v6rx10/comment/ibh2nwz"&gt;need to be massively&#10;concurrent&lt;/a&gt;. (I don&amp;rsquo;t mean to pick on&#10;the individual authors here. It&amp;rsquo;s a general view in the Rust community.)&lt;/p&gt;&#10;&lt;p&gt;While I agree that async Rust isn&amp;rsquo;t &lt;em&gt;easy&lt;/em&gt;, I believe nextest&amp;rsquo;s experience using tokio directly&#10;challenges that general view. The point of async, even more than the concurrency, is to make it&#10;easy to operate across arbitrary sources of asynchronicity. This is exactly what nextest needs to&#10;do. Any Rust program that is I/O-bound, or any program that needs to do heterogenous selects,&#10;should strongly consider using an async runtime like Tokio.&lt;/p&gt;&#10;&lt;h3 id="thanks"&gt;Thanks&lt;/h3&gt;&#10;&lt;p&gt;Thanks to &lt;a href="https://twitter.com/munin"&gt;Fiona&lt;/a&gt;, &lt;a href="https://twitter.com/inanna_malick"&gt;Inanna&lt;/a&gt;,&#10;&lt;a href="https://twitter.com/mycoliza/"&gt;Eliza&lt;/a&gt;, &lt;a href="https://twitter.com/bmwill_"&gt;Brandon&lt;/a&gt;,&#10;&lt;a href="https://twitter.com/PredragGruevski"&gt;Predrag&lt;/a&gt;, and &lt;a href="https://twitter.com/mgattozzi"&gt;Michael&lt;/a&gt; for&#10;reviewing drafts of this article. And thank you for reading it to the end.&lt;/p&gt;&#10;&lt;p&gt;The flowcharts are available in &lt;a href="https://drive.google.com/drive/folders/1znKowkG_58v7oo4zoTxHPF0wBd6OkTyt?usp=sharing"&gt;this Google folder&lt;/a&gt;, and are licensed under &lt;a href="https://creativecommons.org/licenses/by/4.0/"&gt;CC BY 4.0&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h4 id="changelog"&gt;Changelog&lt;/h4&gt;&#10;&lt;p&gt;&lt;em&gt;2022-10-06:&lt;/em&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Add a note that platform-specific APIs like &lt;code&gt;signalfd&lt;/code&gt; and &lt;code&gt;timerfd&lt;/code&gt; can provide heterogenous selects to some extent, though not as much as Tokio can.&lt;/li&gt;&#10;&lt;li&gt;Updated &lt;code&gt;stdout_fut&lt;/code&gt; and &lt;code&gt;stderr_fut&lt;/code&gt; implementations to match changes in &lt;a href="https://github.com/nextest-rs/nextest/pull/560"&gt;#560&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;I&amp;rsquo;d forgotten to add Michael Gattozzi to the reviewers list. Fixed, sorry!&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;</description></item><item><title>Open and closed universes</title><link>https://sunshowers.io/posts/open-closed-universes/</link><pubDate>Mon, 02 Aug 2021 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/open-closed-universes/</guid><description>&lt;figure class="center" &gt;&#10; &#10; &lt;img src="./images/open-closed-walnuts.jpg" alt="Depiction of walnuts, some cracked open and others not." /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Type systems are tools for modeling some aspect of reality. Some types need to represent one of several different choices.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Sometimes, all the choices may be known in advance and will likely never change&amp;mdash;this is often called a &lt;em&gt;closed universe&lt;/em&gt; of values.&lt;/li&gt;&#10;&lt;li&gt;Other times, the set of options will change over time, and the type needs to represent an &lt;em&gt;open universe&lt;/em&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;How should these different kinds of choices be modeled?&lt;/p&gt;</description></item><item><title>What I use, late 2020 edition</title><link>https://sunshowers.io/posts/what-i-use/</link><pubDate>Mon, 21 Dec 2020 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/what-i-use/</guid><description>&lt;p&gt;I&amp;rsquo;m going to try and write up a post about my digital tools at the end of every year. Here&amp;rsquo;s what I&amp;rsquo;m using in late 2020.&lt;/p&gt;&#10;&lt;h1 id="my-desktop"&gt;My desktop&lt;/h1&gt;&#10;&lt;p&gt;The computer I use for work and play is a desktop with an &lt;a href="https://www.amd.com/en/products/cpu/amd-ryzen-9-3900x"&gt;AMD Ryzen 9 3900x&lt;/a&gt; processor, a &lt;a href="https://www.samsung.com/semiconductor/minisite/ssd/product/consumer/970evoplus/"&gt;Samsung 970 EVO Plus&lt;/a&gt; 2TB NVMe drive, 64GB of RAM, and an &lt;a href="https://www.nvidia.com/en-us/geforce/graphics-cards/rtx-2060-super/"&gt;RTX 2060 Super&lt;/a&gt; graphics card. The case I use is an &lt;a href="https://www.nzxt.com/products/h1-matte-white"&gt;NZXT H1&lt;/a&gt;: it is small, stays cool and quiet under heavy loads, and looks &lt;em&gt;gorgeous&lt;/em&gt;. It also has a lot of future upgradability: if I need another few terabytes of storage in the future, I can easily pop the case open and add an SSD or NVMe drive.&lt;/p&gt;</description></item><item><title>The social consequences of type systems</title><link>https://sunshowers.io/posts/types/</link><pubDate>Sun, 27 Sep 2020 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/types/</guid><description>&lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/social-contract.jpg" alt="Title page of The Social Contract by Rousseau" /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;Title page of &lt;a href="https://en.wikipedia.org/wiki/The_Social_Contract"&gt;&lt;em&gt;The Social Contract&lt;/em&gt;&lt;/a&gt;, by Jean-Jacques Rousseau. Public domain.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Type systems&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt; are the wellspring of some of the most interesting work in computer science, and practitioners like myself deal with them everyday. This post collects some thoughts I&amp;rsquo;ve had about how type systems interact with the communities that use them.&lt;/p&gt;&#10;&lt;h1 id="types-as-communication"&gt;Types as communication&lt;/h1&gt;&#10;&lt;p&gt;Let&amp;rsquo;s say you write a simple Python function that concatenates two strings:&lt;/p&gt;</description></item><item><title>Thinking about dependencies</title><link>https://sunshowers.io/posts/dependencies/</link><pubDate>Sat, 22 Aug 2020 00:00:00 +0000</pubDate><guid>https://sunshowers.io/posts/dependencies/</guid><description>&lt;p&gt;People tend to have lots of opinions about software dependencies. Here are mine.&lt;/p&gt;&#10;&lt;h1 id="standing-on-the-shoulders-of-giants"&gt;Standing on the shoulders of giants&lt;/h1&gt;&#10;&#10; &lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/hands-united.png" /&gt;&#10; &#10; &#10; &lt;figcaption class="center" &gt;&lt;a href="https://pixabay.com/photos/hand-united-together-people-unity-1917895/"&gt;United&lt;/a&gt;, by truthseeker08 on Pixabay.&lt;/figcaption&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;At a high level, virtually all code in existence relies on tools and libraries written by other people. Even projects that don&amp;rsquo;t use any &lt;em&gt;library&lt;/em&gt; dependencies rely on tools written by other people:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;compilers, interpreters and runtimes&lt;/li&gt;&#10;&lt;li&gt;build systems&lt;/li&gt;&#10;&lt;li&gt;version control&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This is good. Depending on other people is how humans work.&lt;/p&gt;</description></item><item><title>About me</title><link>https://sunshowers.io/about/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://sunshowers.io/about/</guid><description>&lt;figure class="floatright" &gt;&#10; &#10; &lt;img src="./images/rain-headshot.jpg" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&#10;&lt;p&gt;Hello, and welcome to my little corner of the internet! 💕&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m Rain (like the weather 🌧) and my pronouns are &lt;em&gt;they/she&lt;/em&gt;. I&amp;rsquo;m a queer, trans, nonbinary immigrant based in Oakland, CA, US, and I build systems and tools for &lt;a href="https://oxide.computer"&gt;Oxide&lt;/a&gt; in &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; for a living.&lt;/p&gt;&#10;&lt;p&gt;This blog is where I express longform opinions that don&amp;rsquo;t quite fit in a social media thread.&lt;/p&gt;&#10;&lt;p&gt;Check out my &lt;a href="./work/"&gt;work&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h2 id="contact"&gt;Contact&lt;/h2&gt;&#10;&lt;p&gt;You can find me on:&lt;/p&gt;</description></item><item><title>Work</title><link>https://sunshowers.io/work/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://sunshowers.io/work/</guid><description>&lt;p&gt;I&amp;rsquo;m a software and systems engineer with a background building open source developer tools at scale. Because the &lt;a href="https://ckluis.com/the-marginal-cost-of-software-approaches-zero-7fda166f219f"&gt;marginal cost of software approaches zero&lt;/a&gt;, software tools act as incredibly powerful force multipliers—developer tools even more so. I thrive in teams that are passionate about building things of lasting value to society by solving hard problems, all while caring deeply about the craft and the experience.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; is the primary medium I&amp;rsquo;ve worked in since early 2017, and before that I used a mix of &lt;a href="https://www.python.org/"&gt;Python&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/C_%28programming_language%29"&gt;C&lt;/a&gt;, with a sprinkling of other languages like &lt;a href="https://en.wikipedia.org/wiki/C%2B%2B"&gt;C++&lt;/a&gt;, &lt;a href="https://hacklang.org/"&gt;Hack&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/JavaScript"&gt;JavaScript&lt;/a&gt;.&lt;/p&gt;</description></item></channel></rss>