Repository navigation
Let only one of two simultaneous first registrations become admin - #286
Merged
Merged
Conversation
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.
This was referenced Oct 13, 2026
danielplohmann
added a commit
that referenced
this pull request
Oct 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The first finding in #284. Whether a registration is the first one is decided before the view runs (
mcritweb/views/authentication.py:151), andregister()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 /registerBEGIN IMMEDIATE(authentication.py:233) and asksis_first_user()again. The password hash stays outside the lock, so it is held only for the check and the two writes.ServerInfo.saveToDb()takescommit=True(mcritweb/db.py:213). Registration passesFalse(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./registerwithFIRST_USER_TAKEN(authentication.py:44,:238) and no account. The redirect matters: within that requestg.first_useris 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.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_adminposts 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_adminmakes 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_registrationis 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=Falseeach 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
McritClientopens arequests.Session(), which the stub intests/testSubmitMetadata.pydid 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.