<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Debugging · Jared Lynskey</title>
    <link>https://jared.lynskey.co.nz/en/tags/debugging/</link>
    <description>Short stories of value on software engineering and engineering leadership — performance reviews, hiring and scaling teams, CI/CD, DevOps monitoring and agentic AI. Written from Seoul.</description>
    <generator>Hugo</generator>
    <language>en</language>
    <copyright>© 2026 Jared Lynskey</copyright>
    <lastBuildDate>Sat, 07 Nov 2026 00:00:00 +1300</lastBuildDate>
    <atom:link href="https://jared.lynskey.co.nz/en/tags/debugging/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Cloudflare 504 Errors That Aren&#39;t Real: Early Hints and HTTP 103</title>
      <link>https://jared.lynskey.co.nz/en/posts/2026/cloudflare-504-early-hints/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/en/posts/2026/cloudflare-504-early-hints/</guid>
      <pubDate>Wed, 04 Nov 2026 00:00:00 +1300</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Cloudflare</category>
      <category>Early Hints</category>
      <category>HTTP 103</category>
      <category>504 Gateway Timeout</category>
      <category>Web Performance</category>
      <category>Preload</category>
      <category>Observability</category>
      <category>Debugging</category>
      <description>Thousands of Cloudflare 504 errors, and not one reached a user: they were Early Hints cache misses. How HTTP 103 works, how to tell a fake 504 from a real one, and the header that fixed it.</description>
      <content:encoded><![CDATA[<p>Cloudflare&rsquo;s dashboard said a quarter of my requests were 504s. Not one of them reached a user: they were cache misses in Cloudflare&rsquo;s Early Hints feature, logged as 504 with <code>ClientRequestSource = earlyHintsCache</code> and <code>OriginResponseStatus = 0</code>. Here&rsquo;s how to tell them from real 504s, and the one header that turns the misses into hits.</p>
<p>I first met them in September, when <a href="https://jared.lynskey.co.nz/en/posts/2026/2026-09-09-scale-to-zero-504s/" >a Cloudflare dashboard said 22% of my requests were failing</a> while every health check stayed green; I switched Early Hints off and moved on. In November the same zone showed another 2,600 504s in a day, with Early Hints on again, and this time I wanted to know exactly what they are and whether the right fix was to turn the feature off a second time or to make it do its job. The zone fronts one Fly.io origin serving a Django API, an Expo web app and a Hugo marketing site.</p>

<h2 class="relative group">The short answer
    <div id="the-short-answer" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-short-answer" aria-label="Anchor">#</a>
    </span>
    
</h2>
<ul>
<li>Those 504s are written by Cloudflare&rsquo;s Early Hints cache, not by your visitors. In Cloudflare&rsquo;s logs they carry <code>ClientRequestSource = earlyHintsCache</code>.</li>
<li>A 504 there means a cache miss: Cloudflare had no hints stored for that URL. A hit is logged as a 2xx.</li>
<li>They never reach your origin. <code>OriginResponseStatus</code> is <code>0</code>.</li>
<li>Early Hints runs on the <code>Link</code> HTTP response header. <code>&lt;link rel=&quot;preload&quot;&gt;</code> tags in your HTML don&rsquo;t feed it.</li>
<li>Send the header, and those misses turn into hits and real <code>103 Early Hints</code> responses on the pages that have something to preload.</li>
</ul>
<p>Three more things worth knowing up front. Hint only the render-critical CSS, scripts and fonts, and keep <code>crossorigin</code> on the fonts. Verify with <code>curl -sv --http2</code>: the <code>103</code> should arrive before the <code>200</code>. And keep <code>earlyHintsCache</code> out of your alerting whatever else you decide; if you&rsquo;ll never send the header, turn the feature off rather than paying for it in log noise.</p>

<h2 class="relative group">How to tell a fake Cloudflare 504 from a real one
    <div id="how-to-tell-a-fake-cloudflare-504-from-a-real-one" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#how-to-tell-a-fake-cloudflare-504-from-a-real-one" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>If you use Logpush or Instant Logs, look at two fields before anything else. <code>ClientRequestSource</code> is <code>eyeball</code> for a real visitor&rsquo;s request and <code>earlyHintsCache</code> for the Early Hints lookups. <code>OriginResponseStatus</code> is <code>0</code> when the origin was never contacted.</p>
<p>In the GraphQL Analytics API the same split is the <code>requestSource</code> dimension. This is the query I now run before believing any 5xx spike:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-graphql" data-lang="graphql"><span class="line"><span class="cl"><span class="kd">query</span><span class="w"> </span><span class="p">(</span><span class="nv">$zone</span><span class="p">:</span><span class="w"> </span><span class="nc">String</span><span class="p">!,</span><span class="w"> </span><span class="nv">$since</span><span class="p">:</span><span class="w"> </span><span class="nc">Time</span><span class="p">!)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="py">viewer</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="py">zones</span><span class="p">(</span><span class="py">filter</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="py">zoneTag</span><span class="p">:</span><span class="w"> </span><span class="nv">$zone</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="py">httpRequestsAdaptiveGroups</span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="py">limit</span><span class="p">:</span><span class="w"> </span><span class="nc">20</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="py">filter</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="py">datetime_geq</span><span class="p">:</span><span class="w"> </span><span class="nv">$since</span><span class="p">,</span><span class="w"> </span><span class="nc">edgeResponseStatus</span><span class="p">:</span><span class="w"> </span><span class="nc">504</span><span class="w"> </span><span class="p">}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="py">orderBy</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="py">count_DESC</span><span class="p">]</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="py">count</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="py">dimensions</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="py">requestSource</span><span class="w"> </span><span class="py">userAgent</span><span class="w"> </span><span class="py">clientRequestPath</span><span class="w"> </span><span class="p">}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="p">}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="p">}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
<p>Grouping by user agent tells the same story. In September the Early Hints lookups showed up as <code>bastion early hints</code> and <code>nginx-ssl early hints</code>. In November they were <code>fl2 early hints</code> and <code>nginx-ssl early hints</code> (FL2 is the newer generation of Cloudflare&rsquo;s proxy). Across 24 hours, all 2,631 of the zone&rsquo;s 504s came from those user agents. None came from a browser, the app, curl or a search crawler.</p>
<p>A real 504 that a visitor actually saw almost always came from your side of Cloudflare. A proxy or load balancer in front of your application gave up waiting: nginx, a cloud load balancer, or a platform proxy waiting for a cold machine to boot. Cloudflare&rsquo;s own timeouts use different codes. <strong>522</strong> means it couldn&rsquo;t connect to your origin, and <strong>524</strong> means it connected but got no response within 125 seconds (the default proxy read timeout on plans below Enterprise).</p>
<p>So if you landed here searching for how to raise Cloudflare&rsquo;s timeout because of 504s: the timeout behind a 504 is almost never Cloudflare&rsquo;s. Check <code>proxy_read_timeout</code> in nginx, your load balancer&rsquo;s idle timeout, or how long your platform takes to wake a machine. A useful clue: if requests fail at a round number well under 125 seconds, such as 30 or 60, it&rsquo;s your own proxy&rsquo;s limit, not Cloudflare&rsquo;s. (Only Enterprise plans can raise Cloudflare&rsquo;s 125-second limit, up to 6,000 seconds, and Cloudflare itself treats that as a stopgap.)</p>

<h2 class="relative group">What those 504s actually are
    <div id="what-those-504s-actually-are" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-those-504s-actually-are" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>On a zone with Early Hints enabled, every request for an HTML page or an extensionless URL (which, on an API-heavy zone, is most API calls) gets an extra internal lookup: Cloudflare checking its own cache for a set of hints to send. That lookup is logged as its own line, with <code>ClientRequestSource = earlyHintsCache</code>. It isn&rsquo;t an end-user request, and it is never sent to your origin.</p>
<p>The status on that line is the result of the cache lookup, not an error:</p>
<ul>
<li>2xx is a hit. Cloudflare had <code>Link</code> headers stored for that URL and sent them to the browser as a <code>103</code>. On my zone, hits show up as both <code>200</code> and <code>204</code>.</li>
<li>504 is a miss. Nothing was stored, so there was nothing to send early.</li>
</ul>
<p>If your origin never sends the header Early Hints runs on, every lookup is a miss and every request writes a 504 line. In the three hours before I changed anything, the zone logged 173 Early Hints lookups and all 173 were misses. Since the feature had been switched on, it had not served a single hint.</p>
<p>One correction to my September post: I described these as probes that &ldquo;get no response from the Fly origin&rdquo;. That isn&rsquo;t quite right. They never go to the origin at all. The 504 records a cache miss, not a timeout.</p>
<p>Cloudflare documents this, briefly, on its <a href="https://developers.cloudflare.com/cache/advanced-configuration/early-hints/"  target="_blank" rel="noreferrer">Early Hints page</a>: an Early Hints lookup logged with <code>ClientRequestSource: earlyHintsCache</code> is &ldquo;expected and benign&rdquo;, a 200 is a cache hit and a 504 a miss, and Log Explorer hides them with <code>ClientRequestSource != &quot;earlyHintsCache&quot;</code>. Cache Analytics already leaves them out. The HTTP traffic chart that scared me doesn&rsquo;t.</p>

<h2 class="relative group">These fake 504s talked me into a server upgrade
    <div id="these-fake-504s-talked-me-into-a-server-upgrade" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#these-fake-504s-talked-me-into-a-server-upgrade" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>This is why the split matters. On 3 November, the day before I started on this post, Cloudflare logged 3,292 504s against about 13,300 visitor requests: an apparent error rate of 25%. The API runs on Fly.io&rsquo;s <code>shared-cpu-1x</code> machines, which are burst-credited. They get a small guaranteed share of a CPU and spend credits to run faster, and a load test in August had shown them throttling to roughly 20 to 30 requests a second under sustained load. &ldquo;A quarter of requests timing out at peak&rdquo; looked exactly like that throttling, so I moved the API to <code>shared-cpu-2x</code>.</p>
<p>Every one of those 504s was an Early Hints cache miss. Here is what the <code>requestSource</code> split and the origin&rsquo;s own logs showed:</p>
<ul>
<li>Every Cloudflare 5xx from 28 October to 4 November was an <code>earlyHintsCache</code> lookup, apart from about one 525 a day (the one I traced came from a security scanner).</li>
<li>The origin&rsquo;s access logs for 24 October to 4 November had no 5xx responses at all, and one request slower than 10 seconds.</li>
<li>Fly&rsquo;s metrics never showed more than about 7 requests a second, and the machine never used more than a fifth of one CPU core. It never got near the throttling ceiling.</li>
</ul>
<p>The bigger machines fixed nothing, because nothing was broken. The API is back on <code>shared-cpu-1x</code>.</p>
<p>Don&rsquo;t make capacity decisions from a 5xx chart you haven&rsquo;t split by request source. Check it against your origin&rsquo;s own logs as well: if Cloudflare reports a quarter of requests failing and your server logged no errors, find out what Cloudflare is counting before you change the server.</p>
<p><em>Updated 5 November 2026: the first version of this section said the API had really fallen over on <code>shared-cpu-1x</code> that week. It hadn&rsquo;t. The numbers above are what I found when I checked.</em></p>

<h2 class="relative group">Is HTTP 103 an error?
    <div id="is-http-103-an-error" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#is-http-103-an-error" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>No. Status codes in the 1xx range are informational: interim responses a server can send before the real one. <code>103 Early Hints</code>, defined in RFC 8297, carries <code>Link</code> headers so the browser can start fetching critical CSS, JavaScript and fonts, or open connections to other origins, while the server is still building the page. The final <code>200</code> follows on the same request.</p>
<p>Support is uneven but safe. Chromium-based browsers (Chrome since version 103, and Edge) and current Firefox act on both <code>preload</code> and <code>preconnect</code> hints. Safari (17 and later) uses only <code>preconnect</code>. Browsers process <code>103</code> only over HTTP/2 and HTTP/3. A browser that ignores it simply waits for the <code>200</code> as it always did, so a <code>103</code> is a head start for most visitors and a no-op for the rest.</p>

<h2 class="relative group">Why mine was empty: the Link header vs <code>&lt;link rel=preload&gt;</code>
    <div id="why-mine-was-empty-the-link-header-vs-link-relpreload" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#why-mine-was-empty-the-link-header-vs-link-relpreload" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>This is the part that cost me the most time, because it looks like it should already work.</p>
<p>Early Hints caches exactly one thing: the HTTP <strong><code>Link</code> response header</strong> with a <code>rel=preload</code> or <code>rel=preconnect</code> relation. Cloudflare reads it from your normal responses (status <code>200</code>, <code>301</code> or <code>302</code>, on HTML or extensionless URLs), stores it, and replays it to the next visitor as a <code>103</code> before your full response is ready.</p>
<p>My pages already had preloads, but as tags in the HTML <code>&lt;head&gt;</code>:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">&#34;preload&#34;</span> <span class="na">href</span><span class="o">=</span><span class="s">&#34;/_expo/static/css/global-7d77bd.css&#34;</span> <span class="na">as</span><span class="o">=</span><span class="s">&#34;style&#34;</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">&#34;preload&#34;</span> <span class="na">href</span><span class="o">=</span><span class="s">&#34;/_expo/static/js/web/entry-fd7fa4.js&#34;</span> <span class="na">as</span><span class="o">=</span><span class="s">&#34;script&#34;</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">&#34;preload&#34;</span> <span class="na">href</span><span class="o">=</span><span class="s">&#34;/fonts/manrope-latin.woff2&#34;</span> <span class="na">as</span><span class="o">=</span><span class="s">&#34;font&#34;</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;font/woff2&#34;</span> <span class="na">crossorigin</span><span class="p">&gt;</span></span></span></code></pre></div></div>
<p>Those tags still do something: the browser acts on them. But it only sees them after it has received the HTML and started parsing it, which is exactly the moment Early Hints exists to beat. The Expo export and my Hugo theme both write these tags for you, and neither sets the header. So the feature was enabled, the preloads were declared, and the two never met.</p>

<h2 class="relative group">The fix: send the header you already have the data for
    <div id="the-fix-send-the-header-you-already-have-the-data-for" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-fix-send-the-header-you-already-have-the-data-for" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The cleanest fix doesn&rsquo;t involve maintaining a hand-written list of critical assets that goes stale every time a bundle hash changes. Each page already declares what it wants preloaded, in those tags. So mirror them into a <code>Link</code> header when you serve the page. The standard library&rsquo;s <code>html.parser</code> copes with quoted and unquoted attributes (Hugo&rsquo;s minifier strips the quotes), so there&rsquo;s no regex to get wrong:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">from</span> <span class="nn">html.parser</span> <span class="kn">import</span> <span class="n">HTMLParser</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">PreloadLinks</span><span class="p">(</span><span class="n">HTMLParser</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="bp">self</span><span class="o">.</span><span class="n">hints</span> <span class="o">=</span> <span class="p">[]</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="nf">handle_starttag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="n">a</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">tag</span> <span class="o">!=</span> <span class="s2">&#34;link&#34;</span> <span class="ow">or</span> <span class="s2">&#34;preload&#34;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&#34;rel&#34;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&#34;&#34;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&#34;href&#34;</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span>
</span></span><span class="line"><span class="cl">        <span class="n">hint</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&#34;&lt;</span><span class="si">{</span><span class="n">a</span><span class="p">[</span><span class="s1">&#39;href&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">&gt;; rel=preload&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&#34;as&#34;</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">            <span class="n">hint</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&#34;; as=</span><span class="si">{</span><span class="n">a</span><span class="p">[</span><span class="s1">&#39;as&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&#34;as&#34;</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&#34;font&#34;</span> <span class="ow">or</span> <span class="s2">&#34;crossorigin&#34;</span> <span class="ow">in</span> <span class="n">a</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="n">hint</span> <span class="o">+=</span> <span class="s2">&#34;; crossorigin&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="bp">self</span><span class="o">.</span><span class="n">hints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">hint</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">link_header</span><span class="p">(</span><span class="n">html</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">limit</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">12</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span> <span class="o">|</span> <span class="kc">None</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">parser</span> <span class="o">=</span> <span class="n">PreloadLinks</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="s2">&#34;, &#34;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">dict</span><span class="o">.</span><span class="n">fromkeys</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">hints</span><span class="p">))[:</span><span class="n">limit</span><span class="p">])</span> <span class="ow">or</span> <span class="kc">None</span></span></span></code></pre></div></div>
<p>And on the HTML response, in Django:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="s2">&#34;text/html&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">link</span> <span class="o">:=</span> <span class="n">link_header</span><span class="p">(</span><span class="n">html</span><span class="p">))</span> <span class="ow">and</span> <span class="s2">&#34;Link&#34;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">response</span><span class="p">[</span><span class="s2">&#34;Link&#34;</span><span class="p">]</span> <span class="o">=</span> <span class="n">link</span></span></span></code></pre></div></div>
<p>One detail bit me, and it&rsquo;s worth keeping: keep <code>crossorigin</code> on fonts. A font preload without it is treated as a different request, so the font gets downloaded twice and the page gets slower, not faster.</p>
<p>You don&rsquo;t write the <code>103</code> yourself. There&rsquo;s no ASGI, Granian or uvicorn interim-response code to add. Cloudflare builds the <code>103</code> from your header; your only job is to send the header.</p>

<h2 class="relative group">On other stacks
    <div id="on-other-stacks" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#on-other-stacks" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The rule is the same everywhere: put a <code>Link</code> header on the HTML response and let Cloudflare do the rest.</p>
<ul>
<li>
<p>nginx, on the location that serves your HTML:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-nginx" data-lang="nginx"><span class="line"><span class="cl"><span class="k">location</span> <span class="p">=</span> <span class="s">/</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kn">add_header</span> <span class="s">Link</span> <span class="s">&#34;&lt;/css/app.css&gt;</span><span class="p">;</span> <span class="kn">rel=preload</span><span class="p">;</span> <span class="kn">as=style&#34;</span> <span class="s">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">add_header</span> <span class="s">Link</span> <span class="s">&#34;&lt;/fonts/inter.woff2&gt;</span><span class="p">;</span> <span class="kn">rel=preload</span><span class="p">;</span> <span class="kn">as=font</span><span class="p">;</span> <span class="kn">crossorigin&#34;</span> <span class="s">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
</li>
<li>
<p>Laravel with Vite already has middleware for this, <code>AddLinkHeadersForPreloadedAssets</code>, which turns Vite&rsquo;s preloads into <code>Link</code> headers. Two issues reported against it are the same traps as above: its <code>modulepreload</code> hints have gone out without <code>crossorigin</code>, so browsers can discard them and download the script twice, and on large pages the header has grown past Apache&rsquo;s roughly 8 KB header limit. Check both on your version.</p>
</li>
<li>
<p>Anything else: set one <code>Link</code> header, comma-separated, or several <code>Link</code> headers. Either works.</p>
</li>
</ul>
<p>What to hint is where people overdo it. Hint the render-critical CSS, the entry script and the one or two fonts above the fold, not every chunk. A long list makes the browser fetch things it doesn&rsquo;t need yet, ahead of things it does. I cap mine at twelve, and only because the Expo export already curates its preload list.</p>
<p>If you&rsquo;re not behind Cloudflare, or your CDN doesn&rsquo;t do Early Hints, the <code>Link</code> header still helps a little: the browser can start those fetches as soon as the response headers arrive, before it parses the body. But no <code>103</code> is sent unless your server or CDN emits one.</p>

<h2 class="relative group">How to check it&rsquo;s working
    <div id="how-to-check-its-working" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#how-to-check-its-working" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p><code>curl</code> shows the <code>103</code> directly. Ask for HTTP/2 and keep only the status lines and <code>Link</code> headers:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ curl -sv --http2 -o /dev/null https://example.com/register 2&gt;&amp;1 | grep -E &#39;^&lt; (HTTP|link)&#39;
</span></span><span class="line"><span class="cl">&lt; HTTP/2 103
</span></span><span class="line"><span class="cl">&lt; link: &lt;/_expo/static/css/reset-f5d396.css&gt;; as=style; rel=preload, …
</span></span><span class="line"><span class="cl">&lt; HTTP/2 200
</span></span><span class="line"><span class="cl">&lt; link: &lt;/_expo/static/css/reset-f5d396.css&gt;; rel=preload; as=style, …</span></span></code></pre></div></div>
<p>The <code>103</code> arrives first, then the <code>200</code>. Cloudflare reorders the attributes in the hint, which is harmless. Don&rsquo;t expect every request to get a <code>103</code>. Two of my first three did. The first visit after a change only stores the header, and Cloudflare skips the <code>103</code> when the final response headers are already available.</p>
<p>Then go back to the analytics query above and filter on <code>requestSource: &quot;earlyHintsCache&quot;</code>, grouped by status and path. On the pages you fixed, misses should give way to hits.</p>

<h2 class="relative group">What changed, and the caveat
    <div id="what-changed-and-the-caveat" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-changed-and-the-caveat" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Minutes after the header went live, the zone logged its first Early Hints hits ever, and <code>curl</code> showed real <code>103</code> responses on the web pages. One header fixed both problems: the feature started working, and a slice of the &ldquo;504s&rdquo; went away.</p>
<p>The caveat is that a JSON API has nothing to preload. The API endpoints keep missing and keep writing <code>504</code> lines, and on an API-heavy zone that&rsquo;s still most of the volume. You have three sensible options. Send the header on the pages that benefit and accept that API paths keep logging misses; the feature works, and the charts are quieter but not silent. Exclude <code>earlyHintsCache</code> from your 5xx alerting and dashboards whatever else you decide, because an internal cache lookup should never page anyone. Or turn Early Hints off (Speed → Settings → Content Optimization in the dashboard, or the zone&rsquo;s <code>early_hints</code> setting in the API) if you&rsquo;ll never send <code>Link</code> headers; a single-page app whose HTML is tiny may get little from it, and then every lookup stops and the noise goes with it.</p>
<p>I kept it on. The web pages now get a head start, the API&rsquo;s misses are filtered out of everything that alerts, and that&rsquo;s what I should have done in September instead of switching the feature off.</p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/cloudflare-504-early-hints/featured.jpg" medium="image" type="image/jpeg" />
    </item>
    <item>
      <title>Playwright &#34;Test timeout of 30000ms exceeded.&#34;: Find the Wait That Never Finished</title>
      <link>https://jared.lynskey.co.nz/en/posts/2026/2026-10-22-playwright-timeout-30000ms/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/en/posts/2026/2026-10-22-playwright-timeout-30000ms/</guid>
      <pubDate>Thu, 22 Oct 2026 00:00:00 +1300</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Playwright</category>
      <category>UI Testing</category>
      <category>Testing Best Practices</category>
      <category>Debugging</category>
      <description>Usually one step waited for an element or URL that never came, because Playwright actions have no timeout of their own. Find that step; set actionTimeout.</description>
      <content:encoded><![CDATA[<p><code>Test timeout of 30000ms exceeded.</code> means a Playwright test ran past its 30-second budget. Thirty seconds is Playwright Test&rsquo;s default, and the budget covers the test body, its <code>beforeEach</code> hooks and its fixture setup together. The message names the test rather than the step because clicks, fills and navigations have no timeout of their own by default. They wait as long as it takes, and the test timeout is simply the first clock to run out.</p>
<p>So the error is rarely about speed. Usually one step was waiting for something that was never going to happen. Here are the causes in the order I&rsquo;d check them, with the fix for each:</p>
<ol>
<li>A locator matches nothing, so a <code>click()</code> or <code>fill()</code> waits until the test is out of time. A renamed test ID, changed text or a dialog in the way is the usual reason. Fix the locator.</li>
<li>A navigation or <code>waitForURL()</code> is waiting for a page that never comes, because a redirect, a login screen or a first-run screen took the app somewhere else. Fix what you&rsquo;re waiting for.</li>
<li>Setup really is slow: a dev server compiling on first request, a backend waking from scale-to-zero, a full UI login in every <code>beforeEach</code>. Fix the environment, or do the setup once.</li>
<li>The test is simply long. Then, and only then, give that test more time with <code>test.slow()</code> or <code>test.setTimeout()</code>.</li>
</ol>
<p>Whichever it is, the first job is finding the step that was still running when the time ran out. The second is setting <code>actionTimeout</code>, so the next failure names the step for you.</p>
<p>Two lines in <code>use</code> do it:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">defineConfig</span> <span class="p">}</span> <span class="kr">from</span> <span class="s2">&#34;@playwright/test&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="k">default</span> <span class="nx">defineConfig</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">  <span class="nx">timeout</span>: <span class="kt">30_000</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nx">expect</span><span class="o">:</span> <span class="p">{</span> <span class="nx">timeout</span>: <span class="kt">5_000</span> <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="nx">use</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">actionTimeout</span>: <span class="kt">10_000</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nx">navigationTimeout</span>: <span class="kt">15_000</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nx">trace</span><span class="o">:</span> <span class="s2">&#34;retain-on-failure&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl"><span class="p">});</span></span></span></code></pre></div></div>

<h2 class="relative group">What the 30 seconds covers
    <div id="what-the-30-seconds-covers" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-the-30-seconds-covers" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The test timeout is one budget, shared by fixture setup, <code>beforeEach</code> hooks and the test function. When the test finishes, <code>afterEach</code> hooks and fixture teardown get a second budget of the same size. <code>beforeAll</code> and <code>afterAll</code> get the same value as well, but they don&rsquo;t share it with any test, and they fail with their own message.</p>
<p>Inside that budget, only assertions have a clock of their own. <code>await expect(locator).toBeVisible()</code> gives up after 5 seconds by default and tells you exactly what it was looking for. Actions and navigations don&rsquo;t: <code>actionTimeout</code> and <code>navigationTimeout</code> default to no limit at all. That is the whole reason this error is so vague. A click on an element that will never exist doesn&rsquo;t fail. It waits, politely, until something else ends the test.</p>
<p>Here is every timeout that can end a run, and what each one prints. The messages are copied from Playwright 1.58 and 1.63, the oldest and newest versions in my projects.</p>
<table>
	<thead>
			<tr>
					<th>Timeout</th>
					<th>Default</th>
					<th>Where to set it</th>
					<th>What it prints</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Test (body, <code>beforeEach</code>, fixture setup)</td>
					<td>30,000 ms</td>
					<td><code>timeout</code> in the config, <code>--timeout</code>, <code>test.setTimeout()</code>, <code>test.slow()</code></td>
					<td><code>Test timeout of 30000ms exceeded.</code></td>
			</tr>
			<tr>
					<td><code>beforeAll</code> / <code>afterAll</code></td>
					<td>same as the test timeout</td>
					<td><code>test.setTimeout()</code> inside the hook</td>
					<td><code>&quot;beforeAll&quot; hook timeout of 30000ms exceeded.</code></td>
			</tr>
			<tr>
					<td>Assertion (<code>expect</code>)</td>
					<td>5,000 ms</td>
					<td><code>expect: { timeout }</code> in the config, <code>{ timeout }</code> on the assertion</td>
					<td><code>expect(locator).toBeVisible() failed</code> … <code>Timeout: 5000ms</code></td>
			</tr>
			<tr>
					<td>Action (<code>click</code>, <code>fill</code>)</td>
					<td>none</td>
					<td><code>use: { actionTimeout }</code>, <code>{ timeout }</code> on the action</td>
					<td><code>TimeoutError: locator.click: Timeout 10000ms exceeded.</code></td>
			</tr>
			<tr>
					<td>Navigation (<code>goto</code>, <code>waitForURL</code>)</td>
					<td>none</td>
					<td><code>use: { navigationTimeout }</code>, <code>{ timeout }</code> on the call</td>
					<td><code>TimeoutError: page.waitForURL: Timeout 15000ms exceeded.</code></td>
			</tr>
			<tr>
					<td>Whole run</td>
					<td>none</td>
					<td><code>globalTimeout</code> in the config</td>
					<td><code>Timed out waiting 3600s for the test suite to run</code></td>
			</tr>
			<tr>
					<td><code>webServer</code> start-up</td>
					<td>60,000 ms</td>
					<td><code>webServer: { timeout }</code></td>
					<td><code>Timed out waiting 60000ms from config.webServer.</code></td>
			</tr>
	</tbody>
</table>
<p>The action, navigation and whole-run rows have no default to print, so those numbers are whatever you set.</p>

<h2 class="relative group">Five tests and a renamed test ID
    <div id="five-tests-and-a-renamed-test-id" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#five-tests-and-a-renamed-test-id" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>In February, five tests in the suite for my estimating app failed in one run, all the same way. The suite drives the web version of the app, served by its dev server, on one worker, against a freshly seeded local backend. Here&rsquo;s the first of the five, trimmed:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">  1) [default] › tests/example.spec.ts:50:7 › Templates › should edit a template ───
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    Test timeout of 30000ms exceeded.
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    Error: locator.click: Target page, context or browser has been closed
</span></span><span class="line"><span class="cl">    Call log:
</span></span><span class="line"><span class="cl">      - waiting for locator(&#39;[data-testid=&#34;template-item&#34;]&#39;).filter({ hasText: &#39;E2E Edit Template&#39; }).locator(&#39;[data-testid=&#34;edit-template&#34;]&#39;)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">      57 |       .locator(&#39;[data-testid=&#34;template-item&#34;]&#39;)
</span></span><span class="line"><span class="cl">      58 |       .filter({ hasText: &#34;E2E Edit Template&#34; });
</span></span><span class="line"><span class="cl">    &gt; 59 |     await item.locator(&#39;[data-testid=&#34;edit-template&#34;]&#39;).click();
</span></span><span class="line"><span class="cl">         |                                                         ^
</span></span><span class="line"><span class="cl">      60 |     await page.waitForTimeout(500);</span></span></code></pre></div></div>
<p>The <code>Target page, context or browser has been closed</code> line reads like the browser falling over. It isn&rsquo;t. It&rsquo;s what an action says when the page is closed underneath it: the 30 seconds ran out, Playwright tore the test down, and the click that was still waiting reported the page it had lost. With the built-in <code>page</code> fixture, that line would have repeated the timeout instead, as <code>Error: locator.click: Test timeout of 30000ms exceeded.</code> These tests open their own browser context in <code>beforeAll</code>, so the click got the generic version.</p>
<p>The useful line is the call log. The click was waiting for <code>[data-testid=&quot;template-item&quot;]</code>. Ten hours earlier I had changed the row component&rsquo;s test ID from <code>template-item</code> to <code>template-item-${name}</code>, so the end-to-end tests on the native app could pick out one row by name. An attribute selector with <code>=</code> matches the whole value, so the web tests were now asking for an element that existed nowhere on the page, and with no action timeout that click was always going to wait out the test. Five tests at 30 seconds each is two and a half minutes, in a run that took 3.8.</p>
<p>What I added first was a retry. Within half an hour the config had <code>retries: process.env.CI ? 2 : 1</code>, with a comment that still says it&rsquo;s there &ldquo;for flaky browser crashes&rdquo;. On the next run, four of the same five failed again, retry included, and the run took 6.4 minutes instead of 3.8. These describe blocks run in serial mode, where a failure skips the rest of the group and a retry starts the group again from the top, so every bad click now waited out its 30 seconds twice. A retry can&rsquo;t rescue a test that fails the same way every time. It just bills you twice.</p>
<p>The fix was a prefix match:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">item</span> <span class="o">=</span> <span class="nx">page</span>
</span></span><span class="line"><span class="cl">  <span class="p">.</span><span class="nx">locator</span><span class="p">(</span><span class="s1">&#39;[data-testid^=&#34;template-item-&#34;]&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="p">.</span><span class="nx">filter</span><span class="p">({</span> <span class="nx">hasText</span><span class="o">:</span> <span class="s2">&#34;E2E Edit Template&#34;</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">item</span><span class="p">.</span><span class="nx">locator</span><span class="p">(</span><span class="s1">&#39;[data-testid=&#34;edit-template&#34;]&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">();</span></span></span></code></pre></div></div>
<p>The wider lesson is that a test ID is a contract, and I had two test suites depending on this one without either knowing about the other. Playwright&rsquo;s best-practice guide recommends user-facing locators such as <code>getByRole()</code> over CSS selectors like mine, so that tests survive DOM changes a user would never notice. If you&rsquo;re setting up Playwright for the first time, <a href="https://jared.lynskey.co.nz/en/posts/2024/2024-06-09-playwright-tests/" >Visual Regression Testing with Playwright for Small Teams</a> is my case for starting with screenshot comparisons.</p>

<h2 class="relative group">Find the step that hung
    <div id="find-the-step-that-hung" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#find-the-step-that-hung" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Read the call log first. It belongs to the step that was still running when the time ran out, and a <code>waiting for locator(...)</code> line with nothing after it means the locator never matched anything.</p>
<p>If that isn&rsquo;t enough, open the trace. My two app suites, one for the estimating app and one for a marketplace and community app I build, both use <code>trace: &quot;on-first-retry&quot;</code>, which only records on a retry, so it needs <code>retries</code> set to at least 1. Without retries, use <code>&quot;retain-on-failure&quot;</code>; for a one-off run, pass <code>--trace on</code>. Then open the trace directly, or from the HTML report:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">npx playwright show-trace test-results/&lt;test-folder&gt;/trace.zip
</span></span><span class="line"><span class="cl">npx playwright show-report</span></span></code></pre></div></div>
<p>The action that never finished is the one carrying the error, near the end of the list. Select it and look at the snapshot: that&rsquo;s the page as it was while Playwright waited, and you can inspect it for the element you expected. In a case like mine, the row is sitting right there with an ID the test isn&rsquo;t asking for.</p>
<p>Recent versions also write an <code>error-context.md</code> beside the failure screenshot, with a text snapshot of the page at the moment it failed. For a missing element, that&rsquo;s often enough on its own.</p>
<p>One trap: <code>--debug</code> opens the Inspector and also sets <code>--timeout=0</code>, along with <code>--max-failures=1</code>, <code>--headed</code> and <code>--workers=1</code>. A test that passes under <code>--debug</code> hasn&rsquo;t told you anything about the 30 seconds.</p>

<h2 class="relative group">Give every action a ceiling
    <div id="give-every-action-a-ceiling" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#give-every-action-a-ceiling" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>None of my configs set an action timeout, which is how one missing element got to spend 30 seconds, five times over. With the config at the top of this post, the same broken locator fails like this, while the test still has time left:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">    TimeoutError: locator.click: Timeout 10000ms exceeded.
</span></span><span class="line"><span class="cl">    Call log:
</span></span><span class="line"><span class="cl">      - waiting for locator(&#39;[data-testid=&#34;template-item&#34;]&#39;).filter({ hasText: &#39;E2E Edit Template&#39; }).locator(&#39;[data-testid=&#34;edit-template&#34;]&#39;)</span></span></code></pre></div></div>
<p>That&rsquo;s the step, the action and how long it waited. I&rsquo;d keep both numbers well under the test timeout, so a single stuck step can&rsquo;t use up the whole budget and hide behind the vaguer message. In February the 30 seconds was never the problem. It was the only clock on that click, so it was the one that went off.</p>

<h2 class="relative group">Raise the timeout for long tests, not stuck ones
    <div id="raise-the-timeout-for-long-tests-not-stuck-ones" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#raise-the-timeout-for-long-tests-not-stuck-ones" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Raising the timeout is the right fix when a test does real work that takes a while. It&rsquo;s the wrong one when a step is waiting for something that won&rsquo;t happen, because a bigger number only makes the failure slower. The trace usually settles which you have. A steady run of actions that each finished, just a lot of them, is a long test. One action sitting on <code>waiting for</code> until the end is a stuck one.</p>
<p>For the long ones, add time where the length is rather than across the whole suite. The marketplace app&rsquo;s suite has one smoke test that walks through the chat screen taking screenshots as it goes, and it gets three minutes:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="nx">test</span><span class="p">(</span><span class="s2">&#34;chat: open, send a message, go back&#34;</span><span class="p">,</span> <span class="kr">async</span> <span class="p">({</span> <span class="nx">page</span> <span class="p">})</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">test</span><span class="p">.</span><span class="nx">setTimeout</span><span class="p">(</span><span class="mi">180</span><span class="nx">_000</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// ...
</span></span></span><span class="line"><span class="cl"><span class="p">});</span></span></span></code></pre></div></div>
<p>Called inside a <code>describe</code>, <code>test.setTimeout()</code> applies to every test in the group (the documented form is now <code>test.describe.configure({ timeout: 60_000 })</code>), which is how the job-cost test in the estimating app&rsquo;s suite gets its minute. <code>test.slow()</code> triples whatever the timeout is, and can take a condition. In a <code>beforeEach</code>, <code>testInfo.setTimeout()</code> extends every test that runs the hook:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="nx">test</span><span class="p">(</span><span class="s2">&#34;slow in WebKit&#34;</span><span class="p">,</span> <span class="kr">async</span> <span class="p">({</span> <span class="nx">page</span><span class="p">,</span> <span class="nx">browserName</span> <span class="p">})</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">test</span><span class="p">.</span><span class="nx">slow</span><span class="p">(</span><span class="nx">browserName</span> <span class="o">===</span> <span class="s2">&#34;webkit&#34;</span><span class="p">,</span> <span class="s2">&#34;This page is slow in WebKit&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// ...
</span></span></span><span class="line"><span class="cl"><span class="p">});</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nx">test</span><span class="p">.</span><span class="nx">beforeEach</span><span class="p">(</span><span class="kr">async</span> <span class="p">({</span> <span class="nx">page</span> <span class="p">},</span> <span class="nx">testInfo</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">testInfo</span><span class="p">.</span><span class="nx">setTimeout</span><span class="p">(</span><span class="nx">testInfo</span><span class="p">.</span><span class="nx">timeout</span> <span class="o">+</span> <span class="mi">30</span><span class="nx">_000</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">});</span></span></span></code></pre></div></div>
<p>Fixed sleeps are the other thing that eats the budget, quietly, on every run. The estimating app&rsquo;s suite has more than a hundred <code>page.waitForTimeout()</code> calls, in nearly every spec file. The edit test above sleeps for three seconds before it even reaches the click, and no amount of sleeping would have saved it. Playwright&rsquo;s API docs are blunt about this: &ldquo;Never wait for timeout in production. Tests that wait for time are inherently flaky.&rdquo; A web-first assertion already waits as long as it needs to, and no longer:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="c1">// before
</span></span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">page</span><span class="p">.</span><span class="nx">locator</span><span class="p">(</span><span class="s1">&#39;[data-testid=&#34;save-template&#34;]&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">page</span><span class="p">.</span><span class="nx">waitForTimeout</span><span class="p">(</span><span class="mi">1500</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">expect</span><span class="p">(</span><span class="nx">page</span><span class="p">.</span><span class="nx">getByText</span><span class="p">(</span><span class="s2">&#34;E2E Edited Template&#34;</span><span class="p">)).</span><span class="nx">toBeVisible</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// after
</span></span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">page</span><span class="p">.</span><span class="nx">locator</span><span class="p">(</span><span class="s1">&#39;[data-testid=&#34;save-template&#34;]&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="k">await</span> <span class="nx">expect</span><span class="p">(</span><span class="nx">page</span><span class="p">.</span><span class="nx">getByText</span><span class="p">(</span><span class="s2">&#34;E2E Edited Template&#34;</span><span class="p">)).</span><span class="nx">toBeVisible</span><span class="p">();</span></span></span></code></pre></div></div>
<p>Assertion timeouts need the same care. Both app suites raise <code>expect.timeout</code> to 15 seconds, and both run against a dev server rather than a production build. That buys slack, but two slow assertions in a row can spend the whole 30 seconds between them, and then the test timeout fires first and you&rsquo;re back to the vague message.</p>

<h2 class="relative group">When the clock isn&rsquo;t the test&rsquo;s
    <div id="when-the-clock-isnt-the-tests" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#when-the-clock-isnt-the-tests" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>If the message mentions <code>config.webServer</code>, no test has run yet. Playwright starts your <code>webServer</code> command and waits for its <code>url</code> to answer before running anything, on a separate timeout of 60 seconds by default. Both app suites give the dev server two minutes:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ts" data-lang="ts"><span class="line"><span class="cl"><span class="nx">webServer</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nx">command</span><span class="o">:</span> <span class="s2">&#34;npm run web -- --port 8081&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nx">url</span><span class="o">:</span> <span class="s2">&#34;http://localhost:8081&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nx">reuseExistingServer</span>: <span class="kt">false</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nx">timeout</span>: <span class="kt">120_000</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="p">},</span></span></span></code></pre></div></div>
<p>If your tests run against a deployed environment instead, find out how long its first request takes. When I finally measured mine, a machine starting from scale-to-zero on my host took 25 to 33 seconds to serve anything, which is most or all of a test&rsquo;s budget before the first click. That story is in <a href="https://jared.lynskey.co.nz/en/posts/2026/2026-09-09-scale-to-zero-504s/" >Fly.io Cold Starts with Scale to Zero: From 33 Seconds to 3</a>.</p>

<h2 class="relative group">If it says &ldquo;No tests found&rdquo; instead
    <div id="if-it-says-no-tests-found-instead" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#if-it-says-no-tests-found-instead" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p><code>No tests found</code> sounds related, but it isn&rsquo;t a timeout: Playwright looked and found nothing to run, usually because the spec sits outside <code>testDir</code> or the command-line argument (a regular expression matched against file paths) doesn&rsquo;t match it. <code>npx playwright test --list</code> shows what it collected without running anything; if your spec isn&rsquo;t there, it was never going to time out.</p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/2026-10-22-playwright-timeout-30000ms/featured.jpg" medium="image" type="image/jpeg" />
    </item>
    <item>
      <title>Fly.io Cold Starts with Scale to Zero: From 33 Seconds to 3</title>
      <link>https://jared.lynskey.co.nz/en/posts/2026/2026-09-09-scale-to-zero-504s/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/en/posts/2026/2026-09-09-scale-to-zero-504s/</guid>
      <pubDate>Wed, 09 Sep 2026 00:00:00 +1200</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Fly.io</category>
      <category>Django</category>
      <category>DevOps</category>
      <category>Cloudflare</category>
      <category>Performance</category>
      <category>Cold Starts</category>
      <category>Redis</category>
      <category>Debugging</category>
      <description>Why my Fly.io machines took 25 to 33 seconds to cold start with auto_stop_machines, and how a release lock, compiled bytecode and suspend got wake-ups down to about 3 seconds.</description>
      <content:encoded><![CDATA[<p><strong>Update, 22 October 2026:</strong> the headline number in this post is wrong. On the day I published it I grouped the zone&rsquo;s 504s by user agent, and all 7,344 of them between 5 and 9 September came from Cloudflare&rsquo;s own Early Hints probes (<code>bastion early hints</code> and <code>nginx-ssl early hints</code>). Those get no response from the Fly origin, are logged as 504s, and mirror real request paths, so they look exactly like user failures. No real client (the app, browsers, curl, search crawlers) got a 504 in that window. I turned Early Hints off the same day: the origin sends no preload headers, so it was doing nothing useful. The cold starts below were real, and so are the fixes, but the 22% was never what my users saw. Before you treat a Cloudflare 5xx rate as an outage, check which user agents are getting them.</p>
<p><strong>Update, 4 November 2026:</strong> one more correction. Those 504s aren&rsquo;t probes the origin fails to answer. They&rsquo;re Cloudflare&rsquo;s own Early Hints cache lookups, which never reach the origin at all, and a 504 there only means nothing was cached for that URL. Turning Early Hints off wasn&rsquo;t the best fix either: sending a <code>Link</code> response header made the feature work and turned those misses into hits. The full story is in <a href="https://jared.lynskey.co.nz/en/posts/2026/cloudflare-504-early-hints/" >Cloudflare 504 errors that aren&rsquo;t real: Early Hints and HTTP 103</a>.</p>
<p>With <code>auto_stop_machines</code> on, any request that lands on a stopped Fly.io machine waits for a full boot, and mine took 25 to 33 seconds because the entrypoint ran Django migrations and <code>collectstatic</code> on every machine start, and nothing had ever cached the bytecode. Running that release work once per image (a Redis lock keyed on <code>FLY_IMAGE_REF</code>) and adding <code>compileall</code> to the build got a cold boot down to about nine seconds. Switching <code>auto_stop_machines</code> from <code>'stop'</code> to <code>'suspend'</code>, which meant dropping a swap file Fly won&rsquo;t suspend with, got wake-ups to about three.</p>
<p>I went looking for a traffic spike on my Cloudflare dashboard and found something much worse underneath it: 22% of every request my app served in a day had failed. No alert had fired. Every health check was green. The logs looked perfectly healthy, because the failing requests never reached the application at all.</p>
<p>The cause turned out to be my own container startup script, doing sensible work at exactly the wrong time.</p>

<h2 class="relative group">The spike was a red herring
    <div id="the-spike-was-a-red-herring" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-spike-was-a-red-herring" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The thing I set out to explain was one hour that took about 4,900 requests against a median of 121. Forty-two unique visitors. That is 110 requests per visitor, which is not how humans browse.</p>
<p>It was a scanner. A single bot walking through invented subdomains, asking each one for secrets:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">api-stage-asia.example.com/.env.local
</span></span><span class="line"><span class="cl">api-us-east-1-demo.example.com/.env
</span></span><span class="line"><span class="cl">api-eu-uat.example.com/wp-config.php~
</span></span><span class="line"><span class="cl">app-test-us-east-1.example.com/actuator/configprops
</span></span><span class="line"><span class="cl">prod-api-use2.example.com/.env.local
</span></span><span class="line"><span class="cl">relay.example.com/.env.production</span></span></code></pre></div></div>
<p>Thirty or so hostnames, every <code>.env</code> variant you can think of, plus a Java framework&rsquo;s actuator endpoints on a Django app. All of them got a 301 to my canonical host and nothing was served. Boring, and genuinely harmless.</p>
<p>But while I had the analytics open, I grouped the same day by status code. That is where the real problem was.</p>
<table>
	<thead>
			<tr>
					<th>Status</th>
					<th>Requests in 24h</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Total</td>
					<td>11,350</td>
			</tr>
			<tr>
					<td>504 Gateway Timeout</td>
					<td>2,474</td>
			</tr>
	</tbody>
</table>
<p>Twenty-two percent. And not spread evenly across the site, but concentrated exactly where it hurts:</p>
<table>
	<thead>
			<tr>
					<th>Endpoint</th>
					<th>504s</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>notifications unread count</td>
					<td>621</td>
			</tr>
			<tr>
					<td>notifications list</td>
					<td>614</td>
			</tr>
			<tr>
					<td>messages unread count</td>
					<td>469</td>
			</tr>
			<tr>
					<td>feed cursor</td>
					<td>210</td>
			</tr>
	</tbody>
</table>
<p>Those are the endpoints my mobile app polls every 30 seconds. So this was not an abstract error-rate number. It was every user&rsquo;s phone, quietly failing to load their unread badge, all day, for weeks probably.</p>

<h2 class="relative group">Why the logs looked fine
    <div id="why-the-logs-looked-fine" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#why-the-logs-looked-fine" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>My first instinct was that the app had become slow. It had not. I pulled the application&rsquo;s own request lines for one of the worst hours out of the log archive:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">[2026-09-08 01:00:29] &#34;GET /api/notifications/unread/&#34; 304 61.615
</span></span><span class="line"><span class="cl">[2026-09-08 01:00:29] &#34;GET /api/notifications/&#34;        304 63.568
</span></span><span class="line"><span class="cl">[2026-09-08 01:00:59] &#34;GET /api/notifications/unread/&#34; 304 58.742
</span></span><span class="line"><span class="cl">[2026-09-08 01:00:59] &#34;GET /api/notifications/&#34;        304 105.438</span></span></code></pre></div></div>
<p>Forty-four to a hundred milliseconds, 304s all the way down. The origin was fast.</p>
<p>That is the important clue, and it is worth stating as a rule: <strong>if your edge reports errors your origin has never heard of, the requests are dying before they arrive.</strong> Stop reading application logs and start looking at what sits in front of them.</p>
<p>In my case that is Fly.io&rsquo;s proxy, and the answer was in how my machines are scheduled.</p>

<h2 class="relative group">Scale to zero is a promise about money, not latency
    <div id="scale-to-zero-is-a-promise-about-money-not-latency" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#scale-to-zero-is-a-promise-about-money-not-latency" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>My config looks like this, and on the face of it it is reasonable:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="line"><span class="cl"><span class="p">[</span><span class="nx">http_service</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">  <span class="nx">auto_stop_machines</span> <span class="p">=</span> <span class="s1">&#39;stop&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="nx">auto_start_machines</span> <span class="p">=</span> <span class="kc">true</span>
</span></span><span class="line"><span class="cl">  <span class="nx">min_machines_running</span> <span class="p">=</span> <span class="mi">1</span></span></span></code></pre></div></div>
<p>Idle machines stop. Traffic starts them again. You pay for what you use. For a small app with bursty traffic this is exactly what you want, and it is why I had ten machines defined but almost always one running.</p>
<p>The bill it hides is latency. When a request arrives for a stopped machine, somebody has to wait for that machine to boot. If booting takes longer than the proxy&rsquo;s patience, that somebody gets a 504.</p>
<p>So: how long did my machines take to boot? I had never measured. The log archive knows, because the startup script announces each phase, and every line carries the machine id:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">WITH</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="p">(</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">SELECT</span><span class="w"> </span><span class="n">instance</span><span class="p">,</span><span class="w"> </span><span class="k">timestamp</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">         </span><span class="k">CASE</span><span class="w"> </span><span class="k">WHEN</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="k">ILIKE</span><span class="w"> </span><span class="s1">&#39;%Starting app server%&#39;</span><span class="w"> </span><span class="k">THEN</span><span class="w"> </span><span class="s1">&#39;begin&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">              </span><span class="k">WHEN</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="k">ILIKE</span><span class="w"> </span><span class="s1">&#39;%Migrations complete%&#39;</span><span class="w"> </span><span class="k">THEN</span><span class="w"> </span><span class="s1">&#39;migrated&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">              </span><span class="k">WHEN</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="k">LIKE</span><span class="w">  </span><span class="s1">&#39;%&#34;GET /healthz%&#39;</span><span class="w">       </span><span class="k">THEN</span><span class="w"> </span><span class="s1">&#39;serving&#39;</span><span class="w"> </span><span class="k">END</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">phase</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">FROM</span><span class="w"> </span><span class="n">logs</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">WHERE</span><span class="w"> </span><span class="n">app</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;myapp&#39;</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="k">timestamp</span><span class="p">::</span><span class="nb">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;2026-09-08&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">)</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="k">SELECT</span><span class="w"> </span><span class="n">instance</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">       </span><span class="k">min</span><span class="p">(</span><span class="k">timestamp</span><span class="p">)</span><span class="w"> </span><span class="n">FILTER</span><span class="w"> </span><span class="p">(</span><span class="k">WHERE</span><span class="w"> </span><span class="n">phase</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;begin&#39;</span><span class="p">)</span><span class="w">    </span><span class="k">AS</span><span class="w"> </span><span class="n">started</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">       </span><span class="k">min</span><span class="p">(</span><span class="k">timestamp</span><span class="p">)</span><span class="w"> </span><span class="n">FILTER</span><span class="w"> </span><span class="p">(</span><span class="k">WHERE</span><span class="w"> </span><span class="n">phase</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;migrated&#39;</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">migrated</span><span class="p">,</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">       </span><span class="k">min</span><span class="p">(</span><span class="k">timestamp</span><span class="p">)</span><span class="w"> </span><span class="n">FILTER</span><span class="w"> </span><span class="p">(</span><span class="k">WHERE</span><span class="w"> </span><span class="n">phase</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;serving&#39;</span><span class="p">)</span><span class="w">  </span><span class="k">AS</span><span class="w"> </span><span class="n">first_serve</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="k">FROM</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">GROUP</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">instance</span></span></span></code></pre></div></div>
<p>The answer, consistently, across every machine:</p>
<table>
	<thead>
			<tr>
					<th>Phase</th>
					<th>Duration</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Boot to migrations finished</td>
					<td>10 to 13 seconds</td>
			</tr>
			<tr>
					<td>Migrations to first served request</td>
					<td>8 to 19 seconds</td>
			</tr>
			<tr>
					<td><strong>Total</strong></td>
					<td><strong>25 to 33 seconds</strong></td>
			</tr>
	</tbody>
</table>
<p>Then I counted how often that happened:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sql" data-lang="sql"><span class="line"><span class="cl"><span class="k">SELECT</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">starts</span><span class="p">,</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="k">DISTINCT</span><span class="w"> </span><span class="n">instance</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">machines</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="k">FROM</span><span class="w"> </span><span class="n">logs</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="k">WHERE</span><span class="w"> </span><span class="n">app</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;myapp&#39;</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="k">timestamp</span><span class="p">::</span><span class="nb">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;2026-09-08&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="k">AND</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="k">ILIKE</span><span class="w"> </span><span class="s1">&#39;%Starting app server%&#39;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="c1">-- starts: 92, machines: 10</span></span></span></code></pre></div></div>
<p>Ninety-two cold starts in one day. Each one a half-minute window in which anything routed to that machine timed out. Twenty-two percent stopped looking mysterious.</p>

<h2 class="relative group">The cause was in my own entrypoint
    <div id="the-cause-was-in-my-own-entrypoint" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-cause-was-in-my-own-entrypoint" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Here is what every single machine ran before it would answer a request:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># entrypoint.sh, run on every machine start</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Running migrations...&#34;</span>
</span></span><span class="line"><span class="cl">python manage.py migrate --noinput
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Running collectstatic in background...&#34;</span>
</span></span><span class="line"><span class="cl">python manage.py collectstatic --noinput <span class="p">&amp;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">exec</span> <span class="s2">&#34;</span><span class="nv">$@</span><span class="s2">&#34;</span>    <span class="c1"># then start the app server</span></span></span></code></pre></div></div>
<p>Read that again with &ldquo;92 times a day&rdquo; in mind.</p>
<p><code>migrate</code> is a full Django boot: import every model, connect to Postgres, query the migrations table, decide there is nothing to do. On a shared CPU that is ten seconds to accomplish nothing. <code>collectstatic</code> is a second full Django boot that then uploads static files to object storage, competing for the same single core that the web server is trying to start on. The app server is the third boot.</p>
<p>Three Python interpreters, two of them doing work that had already been done, every time a machine woke up.</p>
<p>The reason it was written that way is good, and it is probably why yours looks similar: migrations have to run somewhere, Fly&rsquo;s <code>release_command</code> had hung on me before, and the entrypoint is the one place guaranteed to execute on deploy. It is a correct place to put deploy work. It is just not a correct place to put it <em>per machine start</em>, and once your machines stop and start on their own, those are no longer the same event.</p>
<p>That is the whole bug, and I think it is a common one:</p>
<blockquote><p>Work that belongs to a <strong>release</strong> was being run once per <strong>machine start</strong>. Scale-to-zero turned one event into ninety-two.</p>
</blockquote>
<h2 class="relative group">The fix: claim the release once
    <div id="the-fix-claim-the-release-once" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-fix-claim-the-release-once" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Migrations do not need to run on the eighth machine to boot a given image. They need to run once for that image, and everyone else needs to know it has happened.</p>
<p>That is a lock with a name that changes per deploy. Fly hands you the name for free in <code>FLY_IMAGE_REF</code>, and I already had Redis. The whole thing is a small script that runs before Django exists, so the common answer costs milliseconds rather than a framework boot:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1"># release_gate.py: runs before Django exists. Exit code 0 means &#34;prepare this release&#34;.</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">time</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">PREPARE</span><span class="p">,</span> <span class="n">SKIP</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
</span></span><span class="line"><span class="cl"><span class="n">LOCK_TTL</span><span class="p">,</span> <span class="n">WAIT</span><span class="p">,</span> <span class="n">POLL</span> <span class="o">=</span> <span class="mi">300</span><span class="p">,</span> <span class="mi">240</span><span class="p">,</span> <span class="mi">2</span>
</span></span><span class="line"><span class="cl"><span class="n">_sleep</span><span class="p">,</span> <span class="n">_monotonic</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">monotonic</span>    <span class="c1"># aliases, so tests can replace them</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">release_id</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;&#34;&#34;Something that changes exactly when the deployed image does.&#34;&#34;&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">&#34;FLY_IMAGE_REF&#34;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">&#34;FLY_MACHINE_VERSION&#34;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&#34;&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">claim</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">release</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">client</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">release</span><span class="p">:</span>   <span class="c1"># no Redis, or no idea which release: behave as before</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">PREPARE</span>
</span></span><span class="line"><span class="cl">    <span class="n">done_key</span><span class="p">,</span> <span class="n">lock_key</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&#34;release:</span><span class="si">{</span><span class="n">release</span><span class="si">}</span><span class="s2">:done&#34;</span><span class="p">,</span> <span class="sa">f</span><span class="s2">&#34;release:</span><span class="si">{</span><span class="n">release</span><span class="si">}</span><span class="s2">:lock&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">done_key</span><span class="p">):</span>            <span class="c1"># somebody already did it for this image</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">SKIP</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">owner</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s2">&#34;FLY_MACHINE_ID&#34;</span><span class="p">,</span> <span class="s2">&#34;unknown&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">client</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">lock_key</span><span class="p">,</span> <span class="n">owner</span><span class="p">,</span> <span class="n">nx</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">ex</span><span class="o">=</span><span class="n">LOCK_TTL</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">PREPARE</span>                  <span class="c1"># we won the race; we do the work</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># Someone else is preparing. Wait for them, because serving requests</span>
</span></span><span class="line"><span class="cl">    <span class="c1"># against a half-migrated schema is worse than starting slowly.</span>
</span></span><span class="line"><span class="cl">    <span class="n">deadline</span> <span class="o">=</span> <span class="n">_monotonic</span><span class="p">()</span> <span class="o">+</span> <span class="n">WAIT</span>
</span></span><span class="line"><span class="cl">    <span class="k">while</span> <span class="n">_monotonic</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">deadline</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="n">_sleep</span><span class="p">(</span><span class="n">POLL</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">client</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">done_key</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="n">SKIP</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">client</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">lock_key</span><span class="p">,</span> <span class="n">owner</span><span class="p">,</span> <span class="n">nx</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">ex</span><span class="o">=</span><span class="n">LOCK_TTL</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="n">PREPARE</span>              <span class="c1"># the owner died and its lock expired; take over</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="n">PREPARE</span>                      <span class="c1"># waited long enough; do it ourselves</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># ... plus `done` (sets done_key) and a main() that treats any Redis error as PREPARE</span></span></span></code></pre></div></div>
<p>The entrypoint becomes an <code>if</code>:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="k">if</span> python3 release_gate.py claim<span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">    python manage.py migrate --noinput
</span></span><span class="line"><span class="cl">    python manage.py collectstatic --noinput <span class="p">&amp;</span>
</span></span><span class="line"><span class="cl">    python3 release_gate.py <span class="k">done</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">exec</span> <span class="s2">&#34;</span><span class="nv">$@</span><span class="s2">&#34;</span></span></span></code></pre></div></div>

<h2 class="relative group">Every failure has to end in &ldquo;prepare it anyway&rdquo;
    <div id="every-failure-has-to-end-in-prepare-it-anyway" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#every-failure-has-to-end-in-prepare-it-anyway" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>This is the part worth copying, more than the code.</p>
<p>A gate like this sits between your users and a schema migration. If it is wrong in the direction of <em>skipping</em>, you serve requests against a database that has not been migrated, which is a genuinely bad outage. If it is wrong in the direction of <em>preparing</em>, you run an idempotent no-op migration twice and waste ten seconds.</p>
<p>Those outcomes are not symmetrical, so the code must not treat them as such. Every uncertain path returns <code>PREPARE</code>:</p>
<ul>
<li><strong>No Redis, or Redis unreachable.</strong> Prepare. This is exactly the old behaviour, so a cache outage degrades to &ldquo;slow&rdquo;, not &ldquo;broken&rdquo;.</li>
<li><strong>No image reference in the environment.</strong> Prepare. I do not know what release this is, so I cannot know whether it is ready.</li>
<li><strong>The lock&rsquo;s owner died mid-migration.</strong> Its lock expires, the next waiter takes it over and prepares.</li>
<li><strong>Waited longer than the timeout.</strong> Prepare anyway. A machine that never serves is worse than a duplicated migration.</li>
</ul>
<p>There is one deliberate asymmetry in the other direction: a machine that finds another machine actively holding the lock <em>waits</em> rather than serving. Starting slowly during a deploy is fine. Answering queries against a half-applied schema is not.</p>
<p>Watch out for one trap if you build this on a cache wrapper that swallows connection errors, as I first did. A wrapper like that returns <code>False</code> from <code>add()</code> when Redis is unreachable, which reads identically to &ldquo;someone else holds the lock&rdquo; and would have made every machine skip its migrations. That is precisely the dangerous direction. Distinguish the two by reading the lock back: a lock nobody holds means the cache is broken, not that you lost a race.</p>

<h2 class="relative group">Testing it without a Redis
    <div id="testing-it-without-a-redis" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#testing-it-without-a-redis" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The logic is worth unit testing, because the interesting paths are the ones you cannot reproduce by hand. A fake with two methods covers it:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">FakeRedis</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;&#34;&#34;Enough of a Redis client for the gate: get, and set with nx/ex.&#34;&#34;&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">nx</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">ex</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="n">nx</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="kc">None</span>
</span></span><span class="line"><span class="cl">        <span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="kc">True</span></span></span></code></pre></div></div>
<p>Then drive the awkward cases by making time pass on your terms. This is why the gate calls module-level <code>_sleep</code> and <code>_monotonic</code> aliases rather than <code>time.sleep</code> directly, so a test can replace them without patching the <code>time</code> module for every other thread in the process:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">test_a_waiter_takes_over_when_the_owner_disappears</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="n">redis</span> <span class="o">=</span> <span class="n">FakeRedis</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">release_gate</span><span class="o">.</span><span class="n">claim</span><span class="p">(</span><span class="n">redis</span><span class="p">,</span> <span class="s2">&#34;img-1&#34;</span><span class="p">),</span> <span class="n">release_gate</span><span class="o">.</span><span class="n">PREPARE</span><span class="p">)</span>   <span class="c1"># the owner takes the lock</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">def</span> <span class="nf">owner_dies</span><span class="p">(</span><span class="n">_seconds</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="n">redis</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&#34;release:img-1:lock&#34;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>                              <span class="c1"># its lock expired</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">object</span><span class="p">(</span><span class="n">release_gate</span><span class="p">,</span> <span class="s2">&#34;_sleep&#34;</span><span class="p">,</span> <span class="n">owner_dies</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">release_gate</span><span class="o">.</span><span class="n">claim</span><span class="p">(</span><span class="n">redis</span><span class="p">,</span> <span class="s2">&#34;img-1&#34;</span><span class="p">),</span> <span class="n">release_gate</span><span class="o">.</span><span class="n">PREPARE</span><span class="p">)</span></span></span></code></pre></div></div>

<h2 class="relative group">Did it work? Partly
    <div id="did-it-work-partly" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#did-it-work-partly" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Deploy, stop a machine, start it again, watch the clock. The startup now announces its decision and gets out of the way:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">15:23:34  Starting app server...
</span></span><span class="line"><span class="cl">15:23:36  release gate: this release is already prepared, starting straight away
</span></span><span class="line"><span class="cl">15:23:44  &#34;GET /healthz HTTP/1.1&#34; 200</span></span></code></pre></div></div>
<p>Ten seconds, and a second machine measured eleven. Against 25 to 33 before, two thirds of the window is gone, and the gate itself costs two seconds because it never imports Django.</p>
<p>But ten seconds is still ten seconds. A request landing in the first of them still fails. So I went looking for the rest.</p>

<h2 class="relative group">The bytecode nobody was caching
    <div id="the-bytecode-nobody-was-caching" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-bytecode-nobody-was-caching" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The image sets a line that appears in more or less every Python Dockerfile ever written:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-dockerfile" data-lang="dockerfile"><span class="line"><span class="cl"><span class="k">ENV</span> <span class="nv">PYTHONDONTWRITEBYTECODE</span><span class="o">=</span><span class="m">1</span></span></span></code></pre></div></div>
<p>It is good advice. A container should not be scribbling <code>.pyc</code> files into a layer at runtime. What I had never thought through is the other half of the bargain: if the runtime never writes bytecode, and the build never writes it either, then nothing is ever cached, and every process start compiles the entire dependency tree from source.</p>
<p>I counted:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">site-packages .py  files : 5,707
</span></span><span class="line"><span class="cl">site-packages .pyc files : 0</span></span></code></pre></div></div>
<p>Django, the REST framework, every library, recompiled on each of those 92 daily starts. Measured on a production machine:</p>
<table>
	<thead>
			<tr>
					<th><code>django.setup()</code></th>
					<th>Time</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>As shipped</td>
					<td>5.15s</td>
			</tr>
			<tr>
					<td>After <code>compileall</code></td>
					<td>3.17s</td>
			</tr>
	</tbody>
</table>
<p>The fix is one line, and <code>compileall</code> writes explicitly so it works despite the env var:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-dockerfile" data-lang="dockerfile"><span class="line"><span class="cl"><span class="k">RUN</span> python -m compileall -q /app/.venv/lib /app/myapp <span class="o">||</span> true</span></span></code></pre></div></div>
<p>Sixteen seconds of build time, once, in a cached layer.</p>
<p>While I was there I found something quieter. <code>.dockerignore</code> said:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">__pycache__/
</span></span><span class="line"><span class="cl">*.pyc</span></span></code></pre></div></div>
<p>Docker matches those against the whole relative path, not each path component, so an unanchored pattern only ever matches at the root of the build context. Every nested <code>__pycache__</code> was shipping. The image contained 218 <code>.pyc</code> files compiled by Python 3.15 on a laptop, riding along inside an image whose interpreter is 3.14 and quietly ignoring all of them. The patterns want to be <code>**/__pycache__/</code> and <code>**/*.pyc</code>.</p>
<p>Honest result: end to end this bought about a second, not two. Ten seconds became nine. The isolated benchmark oversold it, as isolated benchmarks do.</p>

<h2 class="relative group">Suspend, and the one-line setting that was blocking it
    <div id="suspend-and-the-one-line-setting-that-was-blocking-it" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#suspend-and-the-one-line-setting-that-was-blocking-it" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>At nine seconds I had run out of work to remove. What was left was one unavoidable boot: Python starting, Django importing, the WSGI app coming up on a shared core.</p>
<p>So stop booting. Fly can snapshot a machine&rsquo;s memory instead of shutting it down:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="line"><span class="cl"><span class="p">[</span><span class="nx">http_service</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">  <span class="nx">auto_stop_machines</span> <span class="p">=</span> <span class="s1">&#39;suspend&#39;</span>   <span class="c"># was &#39;stop&#39;</span></span></span></code></pre></div></div>
<p>I went to test it and Fly refused, with the most useful error message of the whole exercise:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">failed to suspend VM: failed_precondition:
</span></span><span class="line"><span class="cl">Machines with swap cannot be suspended</span></span></code></pre></div></div>
<p>My config had this near the top, with a comment I had written myself and believed:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="line"><span class="cl"><span class="c"># Swap cushion so a memory spike swaps to disk instead of getting OOM-killed.</span>
</span></span><span class="line"><span class="cl"><span class="nx">swap_size_mb</span> <span class="p">=</span> <span class="mi">2048</span></span></span></code></pre></div></div>
<p>A reasonable precaution. Was it doing anything? Every machine said:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">MemTotal:   985220 kB
</span></span><span class="line"><span class="cl">SwapTotal: 2097148 kB
</span></span><span class="line"><span class="cl">SwapFree:  2097148 kB</span></span></code></pre></div></div>
<p>Not one page had ever been swapped. And the day&rsquo;s logs held no OOM kills — the eleven lines that matched &ldquo;oom&rdquo; turned out to be requests for a JavaScript bundle whose content hash happens to contain those three letters. Meanwhile the actual memory guard was somewhere else entirely, in the app server&rsquo;s own settings, which respawn any worker past 800MB long before it can reach the 1GB ceiling.</p>
<p>So the cushion was insurance against an event that had never happened, already covered by a different mechanism, and its premium was the ability to suspend. Out it went.</p>
<table>
	<thead>
			<tr>
					<th>Wake path</th>
					<th>Time to serve</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Cold boot, this morning</td>
					<td>25 to 33s</td>
			</tr>
			<tr>
					<td>Cold boot, after the gate and bytecode</td>
					<td>9 to 10s</td>
			</tr>
			<tr>
					<td><strong>Suspended resume</strong></td>
					<td><strong>2.5 to 3.1s</strong></td>
			</tr>
	</tbody>
</table>
<p>Three trials, all within half a second. Every endpoint answered 200 in 250 to 435 milliseconds immediately after waking.</p>

<h2 class="relative group">What happens to your sockets while the machine sleeps
    <div id="what-happens-to-your-sockets-while-the-machine-sleeps" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-happens-to-your-sockets-while-the-machine-sleeps" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Nothing good, and there is no hook to fix it. Fly freezes the VM; the process is not signalled and cannot close anything on the way down. Whatever connections it held are still in its memory when it wakes, pointing at sockets the other end abandoned minutes ago.</p>
<p>You do not solve that going down. You solve it coming up, and mostly by having already made the right decisions:</p>
<ul>
<li><strong>Database connections are opened per request</strong> (<code>conn_max_age = 0</code>), so there is nothing long-lived to go stale.</li>
<li><strong>The Redis cache degrades instead of raising.</strong> A wrapper catches connection errors and returns a miss.</li>
</ul>
<p>I got to watch the second one earn its place. One resume logged exactly the failure you would expect:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">redis.exceptions.ConnectionError: Error while reading from fly-...</span></span></code></pre></div></div>
<p>and the request it happened inside returned 200 in 3.6 seconds, because a dead cache is a slow page, not an error page. Later resumes logged nothing at all.</p>
<p>If your cache client raises on a dead connection, suspend will convert every wake into a burst of 500s. Check that before you flip the setting, not after.</p>

<h2 class="relative group">The test that lied to me
    <div id="the-test-that-lied-to-me" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-test-that-lied-to-me" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>My first suspend measurement said the feature was a disaster: 502, after 30 seconds, twice.</p>
<p>I had pinned the request to a specific machine with <code>fly-force-instance-id</code>, which is a lovely header for load testing one instance and a terrible one for this. Forcing an instance bypasses the proxy logic that would have woken it. Fly told me so, if I had read it as an answer rather than an error:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">machine was recently stopped and is unavailable to service request</span></span></code></pre></div></div>
<p>The number that matters comes from the path real traffic takes. Forty concurrent requests through the edge, well past the soft limit of 12, so the proxy has to scale out onto suspended machines:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">status codes:  40 × 200
</span></span><span class="line"><span class="cl">slowest:       2.1s</span></span></code></pre></div></div>
<p>Two lessons, and the second one is the expensive one. Measure the path your users take, not the path that is convenient to instrument. And when your tooling refuses to do something, read the refusal: &ldquo;Machines with swap cannot be suspended&rdquo; was the sentence that unlocked this entire afternoon.</p>

<h2 class="relative group">What I would check in your app
    <div id="what-i-would-check-in-your-app" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-i-would-check-in-your-app" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>If you run anything on a platform that stops idle instances, these questions took me an afternoon and would have saved me weeks of quiet failure:</p>
<ol>
<li><strong>How long does a cold start take?</strong> Not how long the container takes to run, how long until it answers a real request. If you cannot answer in seconds, measure it before you need to.</li>
<li><strong>What does your entrypoint do per start that belongs to a deploy?</strong> Migrations, static file collection, cache warming, index building. All fine once. All expensive ninety-two times.</li>
<li><strong>Is anything caching your bytecode?</strong> <code>PYTHONDONTWRITEBYTECODE</code> with no <code>compileall</code> in the build means the answer is no.</li>
<li><strong>Can you suspend instead of stop?</strong> And if not, what is stopping you? Mine was a swap file that had never been touched.</li>
<li><strong>Does your edge see errors your origin does not?</strong> Compare the two. A gap between them is not a reporting quirk, it is requests dying in the space between, and your application logs will never show it.</li>
</ol>
<p>The health checks were green the whole time, incidentally. They always are: a health check runs against a machine that is already up. It cannot report on the thirty seconds before it existed.</p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/2026-09-09-scale-to-zero-504s/featured.jpg" medium="image" type="image/jpeg" />
    </item>
    <item>
      <title>How to Debug an iOS App in Xcode: Breakpoints, LLDB, Instruments and the Simulator</title>
      <link>https://jared.lynskey.co.nz/en/posts/2026/2026-02-26-debugging-ios-mobile-apps/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/en/posts/2026/2026-02-26-debugging-ios-mobile-apps/</guid>
      <pubDate>Thu, 26 Feb 2026 00:00:00 +0000</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>iOS</category>
      <category>Xcode</category>
      <category>Debugging</category>
      <category>Swift</category>
      <category>iOS Simulator</category>
      <category>Mobile Development</category>
      <description>How to debug an iOS app in Xcode: conditional and symbolic breakpoints, the LLDB commands worth knowing, the view and memory debuggers, Instruments, the sanitizers, crash logs, and what the simulator misses that a real iPhone catches.</description>
      <content:encoded><![CDATA[<p>To debug an iOS app in Xcode, add an exception breakpoint so a crash stops on the line that threw, reproduce the bug, and inspect state in the LLDB console with <code>po</code>. Then match the tool to the symptom: the view debugger for layout problems, the memory graph and Instruments for leaks and slowness, and the Address and Thread Sanitizers for crashes that only happen sometimes. Finish on a real iPhone, because the simulator hides a whole class of bugs.</p>
<p>I spend way too much time debugging. Everyone does. These days most of mine happens in Xcode and the simulator, chasing bugs in my own app, and the thing that keeps striking me is how much debugging machinery Xcode ships with that hardly anyone touches. This is the workflow I&rsquo;ve settled into over the years, the parts that actually save time when something breaks.</p>

<h2 class="relative group">Breakpoints Are More Powerful Than You Think
    <div id="breakpoints-are-more-powerful-than-you-think" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#breakpoints-are-more-powerful-than-you-think" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Most people click the gutter to set a breakpoint and leave it at that. Right-click one, though, and there&rsquo;s a whole menu of options that changes how you debug.</p>

<h3 class="relative group">Conditional Breakpoints
    <div id="conditional-breakpoints" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#conditional-breakpoints" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Say you&rsquo;ve got a loop processing 1000 items and only item 347 misbehaves. Hitting the breakpoint 346 times first is a waste of an afternoon.</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="mf">0.</span><span class="p">.&lt;</span><span class="n">users</span><span class="p">.</span><span class="bp">count</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">processUser</span><span class="p">(</span><span class="n">users</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>  <span class="c1">// Set breakpoint here with condition: i == 347</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
<p>Right-click the breakpoint, add the condition <code>i == 347</code>, and Xcode only stops when it&rsquo;s true. Works just as well with things like <code>userId == &quot;abc123&quot;</code> or <code>error != nil</code>.</p>

<h3 class="relative group">Symbolic Breakpoints
    <div id="symbolic-breakpoints" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#symbolic-breakpoints" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>These are for when you don&rsquo;t know where a method is being called from. Open the breakpoint navigator (Cmd+8), click the + button, and choose &ldquo;Symbolic Breakpoint&rdquo;.</p>
<p>Want to see every time any view controller loads? Set the symbol to <code>viewDidLoad</code> and Xcode pauses on every single one. Too noisy? Narrow it down: <code>MyViewController.viewDidLoad</code>.</p>
<p>The ones I keep around permanently:</p>
<ul>
<li><code>UIViewAlertForUnsatisfiableConstraints</code> catches Auto Layout conflicts the moment they happen</li>
<li><code>objc_exception_throw</code> breaks on any Objective-C exception before the crash</li>
<li><code>malloc_error_break</code> finds memory allocation problems</li>
</ul>

<h3 class="relative group">Exception Breakpoints
    <div id="exception-breakpoints" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#exception-breakpoints" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>This should be the first thing you set up in any new project. Add one (breakpoint navigator, + → Exception Breakpoint) and Xcode pauses the moment something throws, instead of after the app has already died.</p>
<p>It&rsquo;s caught more nil-unwrapping bugs for me than I&rsquo;d like to admit, the kind that otherwise just show up as mysterious crashes in the console.</p>

<h3 class="relative group">Breakpoint Actions
    <div id="breakpoint-actions" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#breakpoint-actions" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Breakpoints don&rsquo;t have to stop execution at all. Edit one, click &ldquo;Add Action&rdquo;, and it can:</p>
<ul>
<li>Print variables without stopping: <code>&quot;User count: @(users.count)@&quot;</code></li>
<li>Run LLDB commands automatically</li>
<li>Execute shell scripts</li>
<li>Play a sound (I use this for rare code paths I want to know about)</li>
</ul>
<p>Tick &ldquo;Automatically continue after evaluating actions&rdquo; and the breakpoint becomes a logger that never interrupts you.</p>

<h2 class="relative group">LLDB: The Console Commands You&rsquo;ll Actually Use
    <div id="lldb-the-console-commands-youll-actually-use" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#lldb-the-console-commands-youll-actually-use" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>When you hit a breakpoint, the console at the bottom isn&rsquo;t just for reading crash logs. It&rsquo;s LLDB, and it&rsquo;s genuinely useful.</p>

<h3 class="relative group">The Basics
    <div id="the-basics" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-basics" aria-label="Anchor">#</a>
    </span>
    
</h3>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) po user
▿ User
  - id: &#34;123&#34;
  - name: &#34;John Doe&#34;
  - email: &#34;john@example.com&#34;</code></pre></div>
<p><code>po</code> prints objects in a readable format. That&rsquo;s 90% of my LLDB usage right there.</p>
<p>Need more detail? Try <code>p</code> instead:</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) p user.name
(String) $R0 = &#34;John Doe&#34;</code></pre></div>
<p>And to see every local variable at once:</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) frame variable</code></pre></div>

<h3 class="relative group">Changing Things at Runtime
    <div id="changing-things-at-runtime" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#changing-things-at-runtime" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>This is where LLDB earns its keep: you can modify variables without recompiling.</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) expr user.name = &#34;Jane Doe&#34;
(lldb) expr index = 0</code></pre></div>
<p>Found a bug and want to try a fix without rebuilding? Change the variable and continue. I use this constantly when narrowing down edge cases.</p>
<p>You can call methods too:</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) po self.refreshUI()
(lldb) expr navigationController?.popViewController(animated: true)</code></pre></div>

<h3 class="relative group">Navigation
    <div id="navigation" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#navigation" aria-label="Anchor">#</a>
    </span>
    
</h3>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) bt              # Show the call stack
(lldb) frame select 3  # Jump to a different stack frame
(lldb) continue        # Keep running (or just &#39;c&#39;)
(lldb) n               # Step over (next line)
(lldb) s               # Step into function</code></pre></div>

<h3 class="relative group">Watchpoints
    <div id="watchpoints" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#watchpoints" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Want to know when a variable changes? Set a watchpoint:</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) watchpoint set variable user.isLoggedIn</code></pre></div>
<p>Execution now pauses whenever that value changes, no matter where in your code it happens. Gold for tracking down unexpected state mutations.</p>

<h2 class="relative group">Console.app: The Hidden Debugging Tool
    <div id="consoleapp-the-hidden-debugging-tool" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#consoleapp-the-hidden-debugging-tool" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Xcode&rsquo;s console is fine until you need to see everything: system logs, crash reports, the lot. That&rsquo;s when I open Console.app.</p>
<p>If you&rsquo;re using OSLog (you should be), Console.app makes those logs searchable and filterable:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">import</span> <span class="nc">os</span><span class="p">.</span><span class="nc">log</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">logger</span> <span class="p">=</span> <span class="n">Logger</span><span class="p">(</span><span class="n">subsystem</span><span class="p">:</span> <span class="s">&#34;com.example.app&#34;</span><span class="p">,</span> <span class="n">category</span><span class="p">:</span> <span class="s">&#34;networking&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">logger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#34;Starting API request&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">logger</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&#34;Request URL: </span><span class="si">\(</span><span class="n">url</span><span class="p">.</span><span class="n">absoluteString</span><span class="si">)</span><span class="s">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">logger</span><span class="p">.</span><span class="n">error</span><span class="p">(</span><span class="s">&#34;Failed to decode: </span><span class="si">\(</span><span class="n">error</span><span class="p">.</span><span class="n">localizedDescription</span><span class="si">)</span><span class="s">&#34;</span><span class="p">)</span></span></span></code></pre></div></div>
<p>Then in Console.app:</p>
<ol>
<li>Select your simulator or connected device</li>
<li>Filter by your subsystem: <code>subsystem:com.example.app</code></li>
<li>Filter by level: <code>subsystem:com.example.app AND level:error</code></li>
</ol>
<p>You can save filter predicates for the sessions you run often. I&rsquo;ve got one for network requests, one for database operations, and one that shows nothing but errors and warnings.</p>

<h3 class="relative group">Why OSLog Over print()?
    <div id="why-oslog-over-print" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#why-oslog-over-print" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>OSLog beats scattering <code>print()</code> statements everywhere. It&rsquo;s structured, so you can filter by level and category. It&rsquo;s fast enough that logging won&rsquo;t slow the app down. It redacts sensitive data in production automatically, and the logs survive your app crashing.</p>
<p><code>print()</code> is still fine for quick throwaway checks. Anything you might want to look at later belongs in OSLog.</p>

<h3 class="relative group">Advanced Console.app Filtering
    <div id="advanced-consoleapp-filtering" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#advanced-consoleapp-filtering" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Console.app supports proper predicate queries. The ones I use regularly:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl"># All errors and faults in your app
</span></span><span class="line"><span class="cl">subsystem:com.example.app AND (level:error OR level:fault)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"># Network requests that failed
</span></span><span class="line"><span class="cl">subsystem:com.example.app AND category:networking AND eventMessage CONTAINS &#34;failed&#34;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"># Everything from the last 5 minutes
</span></span><span class="line"><span class="cl">subsystem:com.example.app AND timestamp &gt;= now(-5m)</span></span></code></pre></div></div>
<p>You can also export logs to attach to bug reports, which beats copy-pasting out of Xcode&rsquo;s console.</p>

<h2 class="relative group">View Debugging
    <div id="view-debugging" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#view-debugging" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>When the UI is broken and you can&rsquo;t see why, the view debugger is the fastest way out.</p>
<p>Run the app, navigate to the broken screen, and click the &ldquo;Debug View Hierarchy&rdquo; button in Xcode&rsquo;s debug bar (or press Cmd+Shift+D). You get a 3D exploded view of the whole hierarchy.</p>
<p>Rotate it around and things jump out:</p>
<ul>
<li>Views rendering behind other views</li>
<li>Views sitting way off-screen</li>
<li>Views with zero size</li>
<li>The full constraint chain for whatever you select</li>
</ul>
<p>I&rsquo;ve used it to find invisible buttons swallowing tap events, labels rendering at 0x0, and an image that was accidentally 10,000 pixels wide.</p>

<h3 class="relative group">Auto Layout Debugging
    <div id="auto-layout-debugging" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#auto-layout-debugging" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Purple warning icons in the view hierarchy mean constraint conflicts. Click one and Xcode shows exactly which constraints are fighting.</p>
<p>Give your constraints identifiers:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="n">heightConstraint</span><span class="p">.</span><span class="n">identifier</span> <span class="p">=</span> <span class="s">&#34;ProfileImageHeight&#34;</span></span></span></code></pre></div></div>
<p>When a constraint breaks, you&rsquo;ll see <code>&quot;ProfileImageHeight&quot;</code> in the error instead of a memory address, which makes the log actually readable.</p>
<p>From LLDB you can also print the constraint tree:</p>
<div class="highlight-wrapper"><pre tabindex="0"><code class="language-lldb" data-lang="lldb">(lldb) po view.hasAmbiguousLayout
(lldb) po view._autolayoutTrace()</code></pre></div>

<h2 class="relative group">Simulator Features That Save Time
    <div id="simulator-features-that-save-time" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#simulator-features-that-save-time" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">Slow Animations
    <div id="slow-animations" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#slow-animations" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Debug → Slow Animations runs everything at 1/10 speed. Perfect for seeing what&rsquo;s actually happening during a transition, or why an animation looks off.</p>

<h3 class="relative group">Simulate Memory Warnings
    <div id="simulate-memory-warnings" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#simulate-memory-warnings" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Debug → Simulate Memory Warning tests how your app copes with low memory. I&rsquo;ve found plenty of image caching bugs this way: everything works fine until the warning fires and suddenly all your images vanish.</p>

<h3 class="relative group">Network Link Conditioner
    <div id="network-link-conditioner" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#network-link-conditioner" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Xcode → Open Developer Tools → Network Link Conditioner</p>
<p>Simulate 3G, LTE, high packet loss, or complete offline mode. If an API request has ever worked fine on WiFi and timed out on cellular, this is how you reproduce it.</p>
<p>I keep a &ldquo;Bad Network&rdquo; profile with 500ms latency and 10% packet loss. It surfaces timeout bugs and loading-state problems you&rsquo;d never see on fast office WiFi.</p>

<h3 class="relative group">Status Bar Overrides
    <div id="status-bar-overrides" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#status-bar-overrides" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Right-click the status bar in the simulator and you can override the time, battery level, signal strength, and carrier. Handy for consistent screenshots, or for checking how the UI looks at different battery levels.</p>

<h3 class="relative group">Location Simulation
    <div id="location-simulation" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#location-simulation" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Debug → Location simulates different locations without leaving your desk: custom coordinates, city walks, freeway drives. Very useful for location-based features, or bugs that only show up in certain regions.</p>

<h2 class="relative group">Memory Debugging
    <div id="memory-debugging" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#memory-debugging" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">Debug Memory Graph
    <div id="debug-memory-graph" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#debug-memory-graph" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Click the Debug Memory Graph button (Cmd+Shift+M) and Xcode shows every object in memory, their relationships, and the part you actually care about: which ones are leaking.</p>
<p>Purple exclamation marks mean leaks. Click one and you can see the retain cycle.</p>
<p>The most common leak I see:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">class</span> <span class="nc">ViewController</span><span class="p">:</span> <span class="n">UIViewController</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">var</span> <span class="nv">onComplete</span><span class="p">:</span> <span class="p">(()</span> <span class="p">-&gt;</span> <span class="nb">Void</span><span class="p">)?</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="kd">func</span> <span class="nf">setupHandler</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">onComplete</span> <span class="p">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="kc">self</span><span class="p">.</span><span class="n">dismiss</span><span class="p">(</span><span class="n">animated</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>  <span class="c1">// ❌ Captures self strongly</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
<p>Fix it with weak self:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="n">onComplete</span> <span class="p">=</span> <span class="p">{</span> <span class="p">[</span><span class="kr">weak</span> <span class="kc">self</span><span class="p">]</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="kc">self</span><span class="p">?.</span><span class="n">dismiss</span><span class="p">(</span><span class="n">animated</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>  <span class="c1">// ✓ No retain cycle</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
<p>Delegates need to be weak too:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kr">weak</span> <span class="kd">var</span> <span class="nv">delegate</span><span class="p">:</span> <span class="n">ManagerDelegate</span><span class="p">?</span>  <span class="c1">// Not just &#39;var&#39;</span></span></span></code></pre></div></div>

<h3 class="relative group">Instruments
    <div id="instruments" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#instruments" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>For serious memory investigation, use Instruments (Product → Profile, or Cmd+I).</p>
<p>The Allocations instrument shows every object allocation, memory growth over time, which classes are using the most memory, and stack traces for where each allocation happened.</p>
<p>What I&rsquo;m usually looking for: memory that grows linearly over time (probably a leak), unexpectedly large allocations (did I just load all 1000 images at once?), and objects that should have been deallocated but weren&rsquo;t.</p>
<p>Mark generations (the little flag button) before and after an action to see what&rsquo;s persisting when it shouldn&rsquo;t.</p>

<h2 class="relative group">Performance Profiling
    <div id="performance-profiling" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#performance-profiling" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">Time Profiler
    <div id="time-profiler" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#time-profiler" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Product → Profile → Time Profiler. Record while doing the slow thing in your app, then read the call tree.</p>
<p>Sort by &ldquo;Self Weight&rdquo; to find the bottlenecks. If a function shows 40% self weight, that&rsquo;s where your time is going.</p>
<p>I once found a JSON parsing function being called 1000 times a second. Moved it to a background queue and the UI stopped stuttering.</p>

<h3 class="relative group">Main Thread Checker
    <div id="main-thread-checker" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#main-thread-checker" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Xcode automatically catches UI updates on background threads. If you see this:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Main Thread Checker: UI API called on a background thread: -[UILabel setText:]</span></span></code></pre></div></div>
<p>you did something like this:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="n">URLSession</span><span class="p">.</span><span class="n">shared</span><span class="p">.</span><span class="n">dataTask</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="n">data</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">error</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="kc">self</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="p">=</span> <span class="s">&#34;Loaded&#34;</span>  <span class="c1">// ❌ Crash!</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>
<p>Fix it:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="n">URLSession</span><span class="p">.</span><span class="n">shared</span><span class="p">.</span><span class="n">dataTask</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span> <span class="p">{</span> <span class="n">data</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">error</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="n">DispatchQueue</span><span class="p">.</span><span class="n">main</span><span class="p">.</span><span class="n">async</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kc">self</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">text</span> <span class="p">=</span> <span class="s">&#34;Loaded&#34;</span>  <span class="c1">// ✓</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div></div>

<h2 class="relative group">Runtime Diagnostics
    <div id="runtime-diagnostics" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#runtime-diagnostics" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Edit Scheme → Run → Diagnostics. There&rsquo;s a row of checkboxes in here that catch bugs you&rsquo;d never find by hand.</p>

<h3 class="relative group">Address Sanitizer
    <div id="address-sanitizer" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#address-sanitizer" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Finds memory corruption: use-after-free errors, buffer overflows, memory leaks. It makes the app 2-3x slower, but it catches bugs that are near impossible to track down any other way. I turn it on when chasing crashes that only happen sometimes.</p>

<h3 class="relative group">Thread Sanitizer
    <div id="thread-sanitizer" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#thread-sanitizer" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Catches data races and threading bugs. Enable it, run the app, and use it normally; if there&rsquo;s a race condition in there, Thread Sanitizer will find it.</p>
<p>You can&rsquo;t run Address Sanitizer and Thread Sanitizer at the same time, so I alternate between them when a crash is being weird.</p>

<h3 class="relative group">Zombie Objects
    <div id="zombie-objects" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#zombie-objects" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Catches messages sent to deallocated objects. With it enabled, Xcode tells you exactly when you&rsquo;re touching freed memory:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">*** -[MyViewController viewDidLoad]: message sent to deallocated instance 0x600001234000</span></span></code></pre></div></div>
<p>Don&rsquo;t leave it on permanently. It stops objects from being deallocated, so memory climbs forever. But for pinning down a specific use-after-free bug, it&rsquo;s great.</p>

<h2 class="relative group">Common Debugging Scenarios
    <div id="common-debugging-scenarios" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#common-debugging-scenarios" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">App Crashes on Launch
    <div id="app-crashes-on-launch" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#app-crashes-on-launch" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Add an exception breakpoint first; that usually catches it.</p>
<p>If not, the usual suspects are missing keys in Info.plist, force-unwrapped optionals in initialisation code, or dependency injection failing.</p>

<h3 class="relative group">UI Not Updating
    <div id="ui-not-updating" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#ui-not-updating" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Every. Single. Time. It&rsquo;s one of:</p>
<ol>
<li>The update isn&rsquo;t on the main thread</li>
<li>The outlet isn&rsquo;t connected</li>
<li>The view isn&rsquo;t actually visible (check with <code>po view.window</code> in LLDB; if it returns nil, the view isn&rsquo;t in the hierarchy)</li>
</ol>
<p>And for table or collection views: you forgot to call <code>reloadData()</code>.</p>

<h3 class="relative group">Memory Warnings Crashing the App
    <div id="memory-warnings-crashing-the-app" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#memory-warnings-crashing-the-app" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Use Instruments Allocations to see what&rsquo;s holding memory. It&rsquo;s usually images never released from a cache, view controllers that won&rsquo;t deallocate (retain cycle), or some array or dictionary growing forever.</p>
<p>Simulate memory warnings in the simulator to test your cleanup code.</p>

<h3 class="relative group">Mysterious Crashes
    <div id="mysterious-crashes" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#mysterious-crashes" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Turn on the sanitizers. Intermittent usually means threading, so run with Thread Sanitizer. Crashing inside system frameworks usually means memory corruption, so run with Address Sanitizer.</p>

<h3 class="relative group">Crashes You Can&rsquo;t Reproduce
    <div id="crashes-you-cant-reproduce" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#crashes-you-cant-reproduce" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>When the crash happened on someone else&rsquo;s phone, the report usually still exists. For TestFlight and App Store builds, Xcode&rsquo;s Organizer (Window → Organizer → Crashes) collects crash reports from users who share analytics, symbolicated if the dSYMs went up with the build. For a tester&rsquo;s phone, the reports are on the device itself under Settings → Privacy &amp; Security → Analytics &amp; Improvements → Analytics Data, as <code>.ips</code> files starting with the app&rsquo;s name, and they can AirDrop one over. Start from the crashed thread&rsquo;s stack trace, then work through the scenarios above.</p>

<h3 class="relative group">Debugging OTA Updates in the Simulator
    <div id="debugging-ota-updates-in-the-simulator" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#debugging-ota-updates-in-the-simulator" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>My app ships over-the-air updates, and the simulator is the easiest place to debug that whole flow.</p>
<p>I start by watching the network traffic. Console.app filtered to the app&rsquo;s subsystem shows the manifest requests and responses in real time, so you can see exactly which headers go out and what the server sends back.</p>
<p>Then I make the network bad on purpose. Network Link Conditioner shows you how the update flow copes with slow connections and packet loss. Does the app hang? Show a loading indicator? Fall back gracefully?</p>
<p>Around the update check itself, I log everything: when the check starts, when an update is available, when the download begins, and when the new bundle loads.</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">import</span> <span class="nc">os</span><span class="p">.</span><span class="nc">log</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">logger</span> <span class="p">=</span> <span class="n">Logger</span><span class="p">(</span><span class="n">subsystem</span><span class="p">:</span> <span class="s">&#34;com.example.app&#34;</span><span class="p">,</span> <span class="n">category</span><span class="p">:</span> <span class="s">&#34;updates&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">logger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#34;Checking for updates...&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">update</span> <span class="p">=</span> <span class="k">try</span> <span class="n">await</span> <span class="n">Updates</span><span class="p">.</span><span class="n">checkForUpdateAsync</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="n">logger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#34;Update available: </span><span class="si">\(</span><span class="n">update</span><span class="p">.</span><span class="n">isAvailable</span><span class="si">)</span><span class="s">&#34;</span><span class="p">)</span></span></span></code></pre></div></div>
<p>Finally, it&rsquo;s worth testing the manifest endpoint on its own. You can curl it with the same headers the app sends:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">curl -H <span class="s2">&#34;expo-protocol-version: 1&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">     -H <span class="s2">&#34;expo-platform: ios&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">     -H <span class="s2">&#34;expo-runtime-version: 1.0.0&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">     https://updates.example.com/manifest</span></span></code></pre></div></div>
<p>That confirms the server is returning the right data before you go digging into the client.</p>

<h2 class="relative group">Third-Party Tools
    <div id="third-party-tools" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#third-party-tools" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">Charles Proxy / Proxyman
    <div id="charles-proxy--proxyman" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#charles-proxy--proxyman" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>See all your network traffic, modify requests and responses, test error conditions. Essential for API debugging.</p>
<p>Proxyman has a nicer UI than Charles and feels more at home on macOS. I switched last year and haven&rsquo;t looked back.</p>

<h3 class="relative group">Reveal
    <div id="reveal" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#reveal" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Like Xcode&rsquo;s view debugger, but more capable. It works on physical devices without being attached to Xcode, which makes it great for chasing layout issues on testers&rsquo; devices.</p>

<h2 class="relative group">Simulator vs Real Device
    <div id="simulator-vs-real-device" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#simulator-vs-real-device" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Most of my debugging happens in the simulator because it&rsquo;s quick: no cable, no signing, no waiting for an install. But the simulator is an iOS build running on your Mac&rsquo;s processor, with your Mac&rsquo;s memory and your Mac&rsquo;s network connection, and some bugs only exist on a phone.</p>
<table>
	<thead>
			<tr>
					<th></th>
					<th>Simulator</th>
					<th>Real iPhone</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Speed</td>
					<td>Your Mac&rsquo;s CPU, usually faster than a phone</td>
					<td>Real numbers; profile performance here</td>
			</tr>
			<tr>
					<td>Memory</td>
					<td>No real limit, so you simulate memory warnings</td>
					<td>The system kills apps that use too much</td>
			</tr>
			<tr>
					<td>Network</td>
					<td>Your Mac&rsquo;s connection</td>
					<td>Cellular, weak signal, switching between Wi-Fi and mobile data</td>
			</tr>
			<tr>
					<td>Camera, Bluetooth, NFC</td>
					<td>Not available</td>
					<td>Available</td>
			</tr>
			<tr>
					<td>Push notifications</td>
					<td>Can be faked with <code>xcrun simctl push</code></td>
					<td>Real delivery, the way users get them</td>
			</tr>
			<tr>
					<td>Iteration</td>
					<td>Seconds</td>
					<td>Slower: signing, install, cable or Wi-Fi</td>
			</tr>
	</tbody>
</table>
<p>So I build and debug logic in the simulator, and before a release I run the app on the oldest iPhone I still support. Performance, memory pressure and anything that touches hardware haven&rsquo;t really been tested until they&rsquo;ve run on a device.</p>

<h3 class="relative group">Running on a Device
    <div id="running-on-a-device" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#running-on-a-device" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Plug the iPhone in, pick it as the run destination, and set a team under Signing &amp; Capabilities (a free Apple ID is enough for your own phone). Since iOS 16 the phone also needs Developer Mode turned on under Settings → Privacy &amp; Security, which restarts it the first time. Once it&rsquo;s on, the phone&rsquo;s own Settings → Developer menu has a Network Link Conditioner with the same bad-network profiles as the Mac&rsquo;s.</p>

<h3 class="relative group">Wireless Debugging
    <div id="wireless-debugging" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#wireless-debugging" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Plug the device in over USB once, go to Window → Devices and Simulators, tick &ldquo;Connect via network&rdquo;, then unplug. The device stays available in Xcode as long as you&rsquo;re on the same WiFi.</p>

<h3 class="relative group">Device Console
    <div id="device-console" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#device-console" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Window → Devices and Simulators → Open Console. This shows everything: system logs, app logs, crash reports. Far more detail than Xcode&rsquo;s own console.</p>
<p>When a tester tells me &ldquo;the app crashed&rdquo;, I always ask them to plug their device in so I can grab the console logs. There&rsquo;s often a system-level error sitting right there that explains the whole thing.</p>

<h2 class="relative group">What I Actually Do When Debugging
    <div id="what-i-actually-do-when-debugging" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-i-actually-do-when-debugging" aria-label="Anchor">#</a>
    </span>
    
</h2>
<ol>
<li>Add an exception breakpoint if there isn&rsquo;t one already</li>
<li>Reproduce the bug</li>
<li>Set a breakpoint near where I think it&rsquo;s going wrong</li>
<li>Check values with <code>po</code> in LLDB</li>
<li>Modify variables to test fixes without recompiling</li>
<li>Check the view hierarchy if it&rsquo;s UI-related</li>
<li>Profile with Instruments if it&rsquo;s performance-related</li>
<li>Turn on the sanitizers if it&rsquo;s memory or threading</li>
</ol>
<p>The trick is working backwards from the symptom. Crash? Exception breakpoint. Slow? Time Profiler. Memory? Instruments. Weird UI? View debugger.</p>

<h2 class="relative group">Tips I Wish I&rsquo;d Known Earlier
    <div id="tips-i-wish-id-known-earlier" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#tips-i-wish-id-known-earlier" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Use assertions. They catch logic errors before they turn into bugs:</p>
<div class="highlight-wrapper"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="bp">assert</span><span class="p">(</span><span class="n">users</span><span class="p">.</span><span class="bp">count</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s">&#34;Users array should never be empty here&#34;</span><span class="p">)</span></span></span></code></pre></div></div>
<p>Commit before you start debugging. Once you&rsquo;re changing things to test theories, you&rsquo;ll want a clean point to revert to.</p>
<p>Delete your print statements when you&rsquo;re done, or at least wrap them in <code>#if DEBUG</code>. Old debug logs make the next bug harder to see.</p>
<p>Learn LLDB properly. It&rsquo;s faster than rebuilding your app twenty times.</p>
<p>And remember the simulator is not a real phone. Bugs that only appear on device are almost always threading or memory. Or performance, because the simulator runs on your Mac&rsquo;s CPU rather than an actual iPhone chip.</p>

<h2 class="relative group">Tools I Keep Open
    <div id="tools-i-keep-open" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#tools-i-keep-open" aria-label="Anchor">#</a>
    </span>
    
</h2>
<ul>
<li>Xcode, obviously</li>
<li>Console.app filtered to my app&rsquo;s subsystem</li>
<li>Proxyman for network debugging</li>
<li>Instruments when things get serious</li>
</ul>
<hr>
<p>No single tool covers everything. The view debugger won&rsquo;t find a memory leak, and Instruments won&rsquo;t untangle an Auto Layout conflict, so most of the skill is matching the tool to the symptom. That part just comes with repetition: after enough retain cycles you start to smell them from the stack trace alone. Debugging never becomes fun exactly, but it does stop being miserable.</p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/2026-02-26-debugging-ios-mobile-apps/featured.jpg" medium="image" type="image/jpeg" />
    </item>
  </channel>
</rss>
