Skip to content
C.W.K.
Lesson 04 of 05 · published

Standard Library Only

~13 min · python, stdlib, compatibility, testing, monotonic-clock

Level 0Loose File
0 XP0/43 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

The Price of Being the Platform Binary

The last lesson put Delta on Apple's /usr/bin/python3, because that interpreter is allowed through the local network gate. That choice has a bill attached. Apple's python3 is Python 3.9, and you cannot pip install into it the way you would into an environment you own. So Delta is written for Python 3.9 with the standard library only. No requests, no aiohttp, no FastAPI, no third-party TLS. Everything a data channel needs, Delta builds from parts that ship with the interpreter:

NeedStandard library piece
Two HTTP listeners, one thread per connectionhttp.server.HTTPServer with socketserver.ThreadingMixIn
Keep-alive client connections to each peerhttp.client.HTTPConnection, pooled by Delta itself
TLS on the shared LAN, with a pinned certificatessl
Tickets, cache index, hash records, jobssqlite3
Per-chunk and whole-file checksumshashlib (SHA-256)
Constant-time token comparisonhmac.compare_digest

The few family helpers Delta needs (reading the registry and port ledger, finding interfaces, judging free disk space, knowing which folders macOS guards) are vendored as single standard-library files from the family's shared kit, not installed. The installer's only dependency check is one line: can /usr/bin/python3 import ssl, sqlite3 and http.server? If not, install the Xcode Command Line Tools.

The LaunchAgent even starts the interpreter defensively: /usr/bin/python3 -B -E -s -m delta serve. -B writes no bytecode files beside the code, -E ignores every PYTHON* environment variable, and -s leaves the user's own site-packages off the path. Whatever someone installed for themselves cannot leak into the family's data channel.

How a 3.10 Construct Is Kept Out

A rule like "stay 3.9-compatible" is easy to break by accident. You write a match statement, an isinstance(x, int | str), a zip(..., strict=True), and it runs fine on your 3.12. So Delta does not rely on discipline. Its test suite runs in a named Conda environment (cwk-delta, Python 3.12 and pytest), but the end-to-end tests do not import Delta into that interpreter. They start two real daemons on /usr/bin/python3, each with its own fake home, state folder, ports and fleet label, and talk to them over loopback. Syntax that 3.9 lacks, such as a match statement, stops the module from importing, so the daemon never starts and every test fails. A call that 3.9 lacks, such as isinstance(x, int | str) or zip(..., strict=True), is different: it raises only when that line runs, so the suite catches it when a test drives the path that reaches it. That is why the end-to-end tests walk the real verbs, not just the start-up.

Note what is allowed. Delta's code has from __future__ import annotations at the top of every module but the package's two tiny entry files, so it can write dict | None in a function signature: under that import, annotations are stored as strings and never evaluated, so 3.9 never sees the |. The same | inside an isinstance call is evaluated, and 3.9 would reject it. The test that matters is not "does it look modern" but "does the real interpreter run it".

The Clock That Starts at Zero

Running on a different interpreter surfaces differences nobody documents. Delta's first day found one. On Apple's python3, time.monotonic() starts near zero when the process starts. On the Conda python3.12 most of the family uses, it reports something like the machine's uptime, often hundreds of thousands of seconds. Both are correct: a monotonic clock promises only that it never goes backward, not where it starts.

The bug came from a common idiom. A memo is initialized as (0.0, value), meaning "last checked at time zero", and refreshed when monotonic() - checked_at exceeds a limit. On a clock that starts at uptime, zero is ancient and the first call refreshes. On a clock that starts near zero, zero is a moment ago, and the memo looks fresh. In Delta that meant the identity document advertised no LAN addresses for the first 30 seconds after a start, and the tailnet roster refused callers for the first 5 seconds. The fix is a sentinel that is older than any clock reading: float("-inf"). Every "last checked" field in Delta now starts there.

The lesson generalizes: whenever you subtract a stored time from a clock, ask what the stored value means before the first real reading. If the answer is "zero", you are assuming where the clock starts.

Code

The sentinel that is always old, as Delta's roster uses it (trimmed excerpt)·python
# delta/identity.py, the tailnet roster's memo (trimmed excerpt)
class Roster:
    REFRESH_SECONDS = 60.0
    MISS_REFRESH_SECONDS = 5.0   # also the pause between attempts while the roster cannot be read

    def __init__(self):
        self._loaded_at = float('-inf')  # monotonic() starts near 0 in Apple's python3
        self._last_try = float('-inf')

    def _ensure(self, *, miss=False):
        now = time.monotonic()
        stale = now - self._loaded_at > self.REFRESH_SECONDS
        if not (stale or miss) or now - self._last_try < self.MISS_REFRESH_SECONDS:
            return
        never = self._loaded_at == float('-inf')
        ...
Run it under both interpreters: where the clock starts, and what a zero sentinel does·python
# Run with /usr/bin/python3 and with any other python3, and compare.
import sys
import time

REFRESH_AFTER = 30.0
started = time.monotonic()
print(sys.version.split()[0], sys.executable)
print('monotonic() at start:', round(started, 3))

zero_memo = 0.0
inf_memo = float('-inf')
print('zero sentinel looks fresh:', started - zero_memo < REFRESH_AFTER)
print('-inf sentinel looks fresh:', started - inf_memo < REFRESH_AFTER)
Check that every module's syntax compiles on 3.9: compileall with Apple's python3·bash
# A fast syntax check of a package against the interpreter it must run on.
# (Delta's suite goes further and starts real daemons on /usr/bin/python3.)
/usr/bin/python3 --version
/usr/bin/python3 -B -c 'import compileall, sys; sys.exit(not compileall.compile_dir("delta", quiet=1, legacy=False, force=True, optimize=0, workers=1))' \
  && echo 'every module compiles on this interpreter'

External links

Exercise

Pick a small tool you wrote that uses a third-party HTTP library and rewrite its network layer with http.client alone. Then run its tests under the oldest Python you might ship to and record every construct that broke. Finally, grep your own code for time memos initialized to 0 or 0.0 and decide which ones assume where the clock starts.
Hint
rg -n "= \(?0(\.0)?," --type py is a crude but useful first pass for tuple memos. For a quick 3.9 check without installing anything on a Mac, /usr/bin/python3 -m py_compile yourfile.py rejects 3.10-only syntax such as match.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.