Skip to content

Let only one of two simultaneous first registrations become admin - #286

Merged
danielplohmann merged 2 commits into
masterfrom
fix/first-user-registration-race
Oct 16, 2026
Merged

danielplohmann merged 2 commits into
masterfrom
fix/first-user-registration-race

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

The first finding in #284. Whether a registration is the first one is decided before the view runs (mcritweb/views/authentication.py:151), and register() hashes the password (:216) before it writes anything. Two registrations on an instance nobody has set up yet could both pass the check. Both were saved as admin, and the server settings (backend URL, server token, registration token) were whichever request wrote last. Two threads posting together in the test client got two admins in 6 of 6 runs on master (1cdc195).

Change.

sequenceDiagram
    participant A as first request
    participant DB as SQLite
    participant B as second request
    A->>DB: BEGIN IMMEDIATE
    B->>DB: BEGIN IMMEDIATE (waits)
    A->>DB: user table empty? yes
    A->>DB: server settings, not committed
    A->>DB: admin row, COMMIT
    DB-->>B: lock
    B->>DB: user table empty? no
    Note over B: nothing written, redirected to /register
Loading
  • The first-user branch takes SQLite's write lock with BEGIN IMMEDIATE (authentication.py:233) and asks is_first_user() again. The password hash stays outside the lock, so it is held only for the check and the two writes.
  • ServerInfo.saveToDb() takes commit=True (mcritweb/db.py:213). Registration passes False (authentication.py:241), so the server settings commit together with the admin row (:252). Committed on their own, they would end the transaction and let the second request in before the admin row exists. The admin page's call keeps the default.
  • The second request is redirected to /register with FIRST_USER_TAKEN (authentication.py:44, :238) and no account. The redirect matters: within that request g.first_user is still set, so rendering there would show the setup form again, without the registration token field the instance may now require. The next request shows the ordinary form. It does not fall back to a pending account either: the setup form has no field to enter a registration token, only one to set it.
  • Nothing rolls back explicitly. A transaction that does not reach the commit ends with the request, when close_db() (db.py:633) closes the connection and SQLite discards it.

Verification. In the test client only. The live testbed already has users, so this path cannot be reached there without resetting its database.

Tests. In tests/testRegistration.py:

  • test_two_first_registrations_make_one_admin posts two first-user forms from two threads. Both are held in the password hash until both have got there, so both are past the first check before either writes. The check the view makes again is held for up to a second, so that a re-check without the lock is answered "empty" in both threads. It asserts one user, an admin, and the server URL from the admin's own form. The other request is redirected to /register, which then shows the message and the ordinary form with its registration token field, set by the admin's form.
  • test_the_server_settings_are_kept_only_with_their_admin makes the admin row fail to save and checks that the server settings are not kept either.
  • test_the_instance_can_still_be_set_up_after_a_failed_first_registration is a guard: a failed write of the settings does not leave the lock behind. It passes on master too.

The first two fail on master. Removing the re-check, the lock, or commit=False each fails one of them. The race test passed 20 runs in a row on this branch.

The second commit is the test fix from #285 for mcrit 1.15.0, which CI now installs: its McritClient opens a requests.Session(), which the stub in tests/testSubmitMetadata.py did not have. It is a no-op once #285 is merged.

Relation to open PRs. #262 switches the database to WAL and writes out the 5 s busy timeout that is already Python's default, so the second request waits for this lock the same way with or without it. A trial merge (git merge-tree) of this branch with each of #262, #263, #264, #266, #276, #277, #281, #254 and #283 on its own is clean; #265 and #160 already conflict with master.

Whether a registration is the first one was decided in a before_request
hook, and register() hashed the password before writing anything, so two
registrations on an instance nobody had set up could both pass the check.
Both were saved as admin, and the server settings were whichever request
wrote last. In the test client, two threads posting together got two
admins in 6 of 6 runs.

The first-user branch now takes SQLite's write lock with BEGIN IMMEDIATE,
asks again whether the user table is empty, and writes the server
settings without committing them, so they commit together with the admin
row. The second request is told the instance was just set up and that no
user was created. It is not turned into a pending account instead: the
first-user form has no registration token field, and the instance may
require one now.
mcrit 1.15.0, which CI now installs, makes McritClient send through a
requests.Session() it opens in its constructor. The test swaps the
client's requests module for a stub with get and post only, so creating
the client raised AttributeError and all eight tests in the file
errored. The stub now hands itself back as the session. Same change as
in #285; it works with 1.13 and 1.15.
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.

2 participants