25 of 30 requests failed, but all tests passed

Close-up of source code on a dark screen, illustrating Claude Opus 5.5 and Sonnet 5.5 coding workflows

My offline time tracker, a FastAPI app, had a strange problem. Now and then, a red banner said “An unexpected error occurred.” I refreshed, and it worked. The whole test suite, which ran on FastAPI TestClient, was green.

The bug was real, and no test in the suite could ever have found it.

Make "random" repeatable

An intermittent bug is usually a bug that depends on timing. So I stopped clicking and started firing requests in parallel at the real server, started with uvicorn:

  • 30 parallel GET /tags: 25 returned HTTP 500, 5 returned 200.
  • The same request, one at a time: 200, every time.

The frontend loads several things at once when a screen opens. That is why the error appeared “sometimes”.

The cause: one connection, two threads

Python’s sqlite3 has a safety check. By default, a connection can only be used in the thread that created it (check_same_thread=True).

In FastAPI, the database dependency and the route handler were both normal synchronous functions. The server runs each of them as a separate task in a thread pool, with no promise that they get the same thread. So the connection was often created in one thread and used in another. SQLite raised a ProgrammingError, and the catch-all error handler turned it into the 500 and the red banner.

Why the tests were blind

The tests used FastAPI’s TestClient. It runs a whole request inside one thread, so the dependency and the handler always share a thread. The exact request that failed under uvicorn returned 200 under TestClient.

It was not a missing test. With that test client, no test could fail.

The fix, and a test that can fail

  • check_same_thread=False. This is safe here because each request gets its own connection, and the threads use it one after the other, never at the same time.
  • A 30-second busy timeout, because real concurrency now showed real write contention.
  • WAL journal mode, so readers do not wait for writers.

After the fix: 30 parallel reads gave 30× 200 responses. 40 mixed parallel reads and writes gave only 200 and 201, with zero ProgrammingError and zero “database is locked”.

The regression test does not use TestClient. It starts the real server in a subprocess, on a free port, with a temporary database, and sends concurrent requests from a thread pool. I checked that it fails when I remove the fix. A regression test that passes without the fix proves nothing.

The twist: the fix exposed the next bug

Before the fix, most concurrent requests failed. After the fix, they really ran at the same time. That made a second problem reachable.

The Today screen made five or more separate SELECT queries: the settings, today’s entries, the running timer, recent categories and recent tags. In autocommit mode, a write could land between any two of them. The result could contradict itself, for example a running timer that did not match the list of entries.

The fix was to read everything inside one snapshot. The obvious choice was the existing transaction helper, but it uses BEGIN IMMEDIATE, which takes the write lock at the start. That would make every “Today” read wait for all the writers.

The correct choice was BEGIN DEFERRED: under WAL it takes no lock and gives a stable snapshot from the first read. The tests prove the difference. With IMMEDIATE, they fail after 62 seconds, because each blocked writer waits out the 30-second busy timeout.

The snapshot helper also refuses to start inside another transaction, with a clear error. SQLite has no nested transactions, and the default message is not helpful.

What I learned

  • A test client is not the server. If production runs requests in a thread pool, test at least once with the real server and real concurrency.
  • “Intermittent” often means “concurrent”. Make the bug repeatable with parallel requests before you change any code.
  • A fix can expose the next bug. When a bug blocked concurrency, it also hid every concurrency bug behind it.
  • Check that the regression test fails without the fix. Otherwise you only have a test that passes.

More information

Project page: Time Tracker App

Code: GitHub

Picture of Nelson Eiró

Nelson Eiró

Senior Data Engineer

LinkedIn

Add comment: