Free-threaded Python won its A/B and didn't ship: the bugs it found in raggio

Table of Contents
Recommendation four from part 1’s midnight answer was “evaluate free-threaded Python”. The GIL, the global interpreter lock, lets one thread run Python code at a time; CPython 3.13 added an experimental build without it, and 3.14 made it a supported one. On 20 cores, hybrid search with 8 queries in flight ran at 13.8 queries per second against 8.6 serial, so there was a case. This part is about what the evaluation found, which was mostly bugs, and why its win didn’t ship.
First, the bugs that were already there
Before any free-threading number, the DGX probe hammered raggio over HTTP with 16 clients using a collection’s own key, on the normal Python 3.12. 5 to 7% of the requests failed: 500s with sqlite3.InterfaceError, one that got back a row from somebody else’s statement, and 401s on a valid key. The cause was one SQLite connection for the catalog, the database that knows which collections exist and their keys, shared by every thread without a lock. Two threads are enough to confuse it. Searches racing a DELETE of their collection got hundreds of 500s per run, and a collection stopping mid-ingest could close its connections under a write still in flight.
Plan B fixed those first, before any performance work, because a race that returns the wrong row would have made every A/B after it suspect: a lock around the catalog, a stop that waits for the in-flight write, and a search racing a delete answering 404, never 500. Re-run on the DGX: zero failures. On the free-threaded build, by the way, the same hammer had failed 12 to 15 times less often. Nobody investigated why; it doesn’t fix the bug.
The A/B
Plan G built the image on CPython 3.14.8t, with turbovec compiled from source because nobody ships a free-threaded wheel for it, and ran four sessions on one boot: 3.12, 3.14t, 3.12, 3.14t, each at 8 and then 16 queries in flight, on the code with plans B to F in. Beyond the noise band, in both pairs:
| Row | 3.12 | 3.14t |
|---|---|---|
| Hybrid throughput, 8 in flight | 36.2 q/s | 40.4 q/s |
| Hybrid p99, 8 in flight | 465 ms | 356 ms |
| IVF throughput, 8 in flight | 330 q/s | 412 q/s |
| IVF CPU per query | 10.1 ms | 11.8 ms |
| Memory under load, flat | 1,505 MB | 1,530 MB |
A win, by the rule written before the run: hybrid throughput better in both pairs, with the p99 not worse. The verdict was still Hold, by the same rule: turbovec publishes no free-threaded wheel, and a turbovec I compile myself never becomes raggio’s default. The build argument and a non-blocking CI job on 3.14t stay, and the A/B runs again when a wheel exists. Nothing ships on 3.14t.
The four sessions, A1 B1 A2 B2, for the rows the decision rule reads; each row has its own scale. A row counts only if B beats A in both pairs and neither A1 against A2 nor B1 against B2 moves beyond the noise band.
A lock that spins forty times
The CI job found what free-threading costs on a busy machine. On the free-threaded build, a thread that blocks on a held threading.Lock spins up to 40 times before it parks, and each spin calls sched_yield, “let someone else run”. The spin never rereads the lock, so a release during the spin is noticed only after all 40 yields. On an idle machine each yield returns in microseconds. When every core has a runnable thread, each yield hands the core away for a time slice, and one blocked hand-off costs tens of slices.
A two-thread ping-pong with no raggio code in it took 0.01 ms per round trip on 3.12, at any load. On 3.14t it took 0.01 ms until the busy processes reached the core count, then 111 ms on Linux and 599 on Windows. An LD_PRELOAD shim that makes sched_yield return at once brought it back to 2 ms, with the same 16,000 calls per run, so the cost is the yield, not the loop. PYTHON_GIL=1 doesn’t help: the spin count is compiled in, and the spinning thread holds the GIL through its 40 yields, so it doubles to 222 ms. raggio met it as two tests that ran 13 to 24 times slower under load and blew a 5-second budget, and as ingest tests 20 to 60 times slower. CPython knows: a fix was added for 3.15 and reverted after throughput regressions of up to 6x, and the issue on replacing the yield is open. My part is a comment with the repro, still a draft.
Top: what a blocked threading.Lock does on the free-threaded build, 40 yields before it parks. Bottom: the measured round trip of a two-thread ping-pong on the DGX, pinned to 4 cores, as busy processes are added, on 3.12, on 3.14t, and on 3.14t with the GIL forced back on. Drag the slider.
A bug from the first release
One 3.14t suite run failed one test, and the test’s name was lost in the summary line. The final review went looking for candidates and found one in code the plan never touched. BM25’s inverse document frequency used a document count cached before some deletes against the live row count after them. When the cached count exceeded the live one, math.log got a negative argument and raised. Over HTTP, that’s a 500 on any text or hybrid search with a near-universal word, and it has been in raggio since the first release. It reproduces on 3.12 with three deletes in the right order; the stress tests just never produced that order there, 0 failures in 34 runs, while on 3.14t under load 8 of 24 did. The fix is one line, df = min(df, total), the bound FTS5 itself guarantees.
A red 3.14t job would have read as “free threading failed”. A new runtime’s first failures are often old bugs it makes easier to hit, so trace them before blaming the runtime.
A crash inside CPython
Then CI segfaulted, the first crash of the whole program, in a test that stops a collection under load. The cause is in CPython’s sqlite3 module, not in SQLite or in raggio. Connection.close() clears the connection’s statement cache before closing the database, and takes no lock. execute() on another thread, on a cache miss, drops the GIL while SQLite prepares the statement, and a close in that window frees the cache under the reader, which touches it again when the prepare returns. Closing during a running statement is safe, SQLite serializes that. Closing during the prepare isn’t.
raggio reaches it while serving: a collection gets stopped by the eviction rule from the first post, by the idle timeout and by DELETE, all while requests may be reading it. A repro with 8 reader threads crashed 3 of 60 runs on 3.12 and 46 of 60 on 3.14t, which has no GIL between the check and the cache call. The fix: every read on a per-thread connection runs inside that connection’s lock, and closing interrupts each reader and waits for its lock, up to 5 seconds. It costs under half a microsecond per read. A CPython issue with a repro that uses only the standard library is drafted, not filed yet.
The two threads step by step: a reader on a cache miss, a closer clearing the cache. Toggle the fix to see the reader hold its connection’s lock and the closer wait for it.
The failing test that didn’t fail
One last one, because it’s about tests. Plan G’s fourth task turned off the statement cache on free-threaded builds, because on 3.14.7t a connection closed from another thread stayed alive until the owning thread ran Python again, and kept the database files open. The task’s first step, the failing test, didn’t fail: CPython 3.14.8 had shipped two days earlier with exactly that fix. The task stays as a defence for older interpreters, and its failing step now runs on 3.14.7t. A failing test is a claim about the interpreter as well as the code, and a patch release can turn it green.
Where this leaves raggio
That’s the series. The numbers are in part 1’s first figure, and the loop from part 1 is what I’d keep: research on the cheap machine, measure on the real one, write the gate before the run, and let a second agent try to break everything the first one wrote.
Open items: the CPython issue and comment, turbovec’s 2-query tile, and a free-threading A/B that waits for a wheel. And a note to self, in writing so I can’t dodge it: next time I publish a benchmark number, I write down what state the machine was in.