Skip to content

Give each client connection its own database connection - #11

Open
jpm wants to merge 2 commits into
masterfrom
fix/per-connection-backends
Open

jpm wants to merge 2 commits into
masterfrom
fix/per-connection-backends

Conversation

@jpm

@jpm jpm commented Nov 11, 2026

Copy link
Copy Markdown
Owner

The bug

The storage and auth backends were created once at startup, so every client used the same database connection. On POSIX each forked child inherited it, and on Windows every thread shared it. With two clients at once, their queries and results interleaved. A new test reproduced it with 6 simultaneous clients, and 5–6 of the 6 failed in every combination:

  • clients got each other's rows, e.g. ValueError: invalid literal for int() with base 10: 'Alice' where an article number was expected
  • SQL was built from another query's results (a syntax error near '1)
  • in threading mode, MySQLdb.ProgrammingError: This connection is already in use from another thread

The fix

  • The server holds factories. Each connection creates its own storage backend (in the forked child on POSIX), creates its auth backend on first use, and closes both when it ends.
  • If the database can't be reached, the client gets 400 service temporarily unavailable instead of a crash.
  • build_server() still connects once at startup, so a bad configuration fails immediately.
  • Startup moved into build_server() (first commit, no behavior change), so the tests build the server exactly as production does.

Testing

  • tests/integration/test_concurrency.py: 6 clients × 15 rounds at once, forking and threading, on MySQL and PostgreSQL. The server runs in a separate process, so a driver crash fails the test instead of killing pytest. It also checks the server log, to confirm all the requests reached the server.
    • Before the fix: fails in all 4 combinations. After: passes.
  • All integration tests now go through the per-connection path.
  • New unit tests cover a backend per connection (closed at the end), the 400 for an unreachable database, auth created only on login, and the startup check.
  • In Docker: 501 passed, 5 skipped. Locally: 384 passed.

jpm added 2 commits November 11, 2026 10:25
So that tests can build the server exactly as startup does. No change
in behavior.
The storage (and auth) backend was created once at startup, so every
client used the same database connection: on POSIX each forked child
inherited it, and on Windows every thread shared it. With two clients
at once, their queries and results interleaved -- clients got each
other's rows (e.g. an author name where an article number was
expected), SQL built from the wrong results, or MySQLdb's 'connection
is already in use from another thread'.

The server now holds factories, and each connection creates its own
storage backend (in the forked child on POSIX) and, on first use, its
own auth backend; both are closed when the connection ends. If the
database can't be reached, the client gets '400 service temporarily
unavailable'. build_server() still connects once at startup, so a bad
configuration fails immediately.

tests/integration/test_concurrency.py runs 6 clients at once against
the server as build_server() creates it, forking and threading, on
MySQL and PostgreSQL; before this change 5-6 of the 6 clients failed in
every combination.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant