[PR #211·212·213] Server Verification Fixes: Gaps and Hangs When Login and Suspension Overlap
These three PRs fix problems from a server code review done on 2026-11-11. PR 211 fixed four problems in server part 2 (records and cycles), and PR 212 fixed six in server part 1 (login, social, invitations). PR 213 fixes one gap found while reviewing PR 212 that lay outside that PR, so I put them in one post.
All three ran on my Windows computer. For PR 211 and 212, a Claude Code session (Claude Sonnet 5.5) implemented, checked and opened the PR, and Codex (gpt-6.1-terra) wrote the tests. The implementation side did not edit the test files. For PR 213, a Claude Code session (Claude Opus 5.5) planned, implemented, checked and opened the PR, and Codex wrote the tests. Because this is login and security code, the lead session (Opus) was set to review PR 212 once more at the end, and PR 213 came out of that review.
There are no real passwords or tokens in this post. It describes only file and function names and behavior.
What could break
- PR 211, dates and units:
- Planned stocking date (4-1): It could be moved to the same day as, or later than, the planned shipping date. It now returns 400
cycle_dates_invalid. - Dates and times that do not exist (4-4): Values like
2026-02-30andT24:00were pushed to a different date and saved. Both new records and edits now return 400invalid_record.
- Planned stocking date (4-1): It could be moved to the same day as, or later than, the planned shipping date. It now returns 400
- PR 211, overlapping requests:
- Water-quality unit (4-2): If saving a record overlapped with a unit change on an item, the changed unit could be saved. Saving now reads the item rows with
FOR SHARE(a lock that keeps others from changing a row while it is read), so the unit change waits until the save finishes. - Clearing the planned stocking date (4-3): Overlapping with a record save could cause a 500. Record saving now takes the same module row lock as cycle changes before it picks the cycle.
- Water-quality unit (4-2): If saving a record overlapped with a unit change on an item, the changed unit could be saved. Saving now reads the item rows with
- PR 212, login overlapping recovery (5-1): If recovery (changing the password) or suspension slipped in between checking the password and creating the session, a login with the old password still got a session. Creating a session now takes a read lock on the user row, re-reads number, password hash and status, and refuses if any of them changed.
- PR 212, leftover linking data (5-2): Rows in the social-link table kept the Kakao/Apple identifier and the encrypted refresh key after use. They are now deleted as soon as they are used, and expired rows are removed by the existing hourly job. A migration (a script that changes the database structure or contents),
0004, deletes rows that had piled up; this cannot be undone. - PR 212, two more: Withdrawal did not clear the admin memo (5-3), and the request log hid only
/files/, so tokens stayed in/FILES/<token>(5-6). Lock order and Apple login are covered below. - PR 212, waiting on each other until it hangs (5-4): Accepting an invitation and linking a social account locked code row → user row, while reissuing a code locked user row → code row, so they waited on each other (500). Both now use user row → code row.
- PR 212, Apple first login (5-5): It did not check that the
sub(the user’s unique identifier) in the identity token matched thesubin theid_tokenof the code-exchange response. If they differ it now returns 401social_token_invalidand does not create the linking row. - PR 213, sessions created outside the lock: Social login read the status without a lock and then created the session. Invitation sign-up and adding a login method checked the status under a write lock but created the session after the transaction (a unit that handles several database operations as one batch) ended. If an admin suspension finished in that gap, the newly created session was not cut off, and access-token checks and refresh did not re-read the user status, so it could be used until it ended.
- PR 213, connection pool exhaustion: PR 212’s phone login called
issueApp(creates a session) inside a transaction, butissueAppsaved through a different database connection. Under concurrent requests the connection pool (a set of pre-opened database connections, 10 by default, with no wait limit) could run out and hang the whole server.
Choices and rejected alternatives
- 4-4 date check: One function,
parseMeasuredAt, is shared by new records and edits. It rebuilds the date and time from the text as UTC and compares, checking only that the value exists on the calendar. Per-time-zone round-trips were rejected, because calendar validity is all that is needed. - 4-3 lock order: module row → item rows (read) → cycle. I found no place that takes two locks in the opposite order, so a hang from waiting on each other should not be possible. The speed effect of serializing record saves in the same module is judged small, but it was not measured.
- 4-2 read scope: It reads the whole item table (about ten rows) with
FOR SHARE, not only the items in the body. The check already reads the whole table, so the code is smallest. - 5-1 read lock: The user row is locked with
FOR SHARE, notFOR UPDATE(a write lock). When the session row is inserted, the foreign-key check needsKEY SHAREon the user row, but that save runs on another connection (the session store), so a write lock would make it wait on itself. A read lock is compatible with that and still conflicts with recovery’s write lock. Moving session creation to the same connection was rejected at that point because it needed wide changes toSessionsService. - 5-1 re-comparing the number:
codex exec reviewpointed out that a login with the old number could still pass after the number changed. Claude confirmed it in the code and made the number re-compared under the lock too; Codex added a test and confirmed it fails without the fix. - 5-2 where to clean up: No new scheduled job; one line was added to the existing hourly job.
- 5-5 source: Claude confirmed that Apple’s official documentation shows
access_token,refresh_tokenandid_tokentogether in the example response for verifying an authorization code (checked 2026-11-12). Another page describing the response shape has no field descriptions, so that could not be confirmed. - PR 213, creating the session: When
issueAppreceives the transaction’s connection, it saves the session on that connection. All four paths (phone and social login, invitation sign-up, adding a method) pass it. Social login now works like phone login: it re-reads the user row with a read lock inside the transaction, checks the status, and creates the session on the same connection. Suspension takes a write lock on the user row, so it waits for the login transaction to end and then cuts off the session that was just created. - PR 213, rejected: Re-reading the user status on every access-token check and refresh would add a database query per request and is unnecessary once the lock closes the gap.
How it was checked
- PR 211: Codex wrote four groups of tests, and 11 failed before the fix. A code review caught two regressions in the 4-4 implementation (times without seconds like
T12:00Z, and years 0001–0099, were wrongly refused); after the fix, allow-tests were added. - PR 212: Codex wrote 17 tests plus one extra, and all failed before implementation for the intended reasons (the deadlock test produced a real 500). They passed afterward, and the deadlock test passed three times in a row.
- PR 213: Codex wrote 10 tests. Five race tests start an admin suspension at the moment a session is being created and check that no un-revoked session remains and that the access token gets 403
account_suspended. Five connection tests check that the four paths pass the transaction connection. Nine failed before the fix (the phone-login race test already passed), and afterward the 61 tests in the social, invitation and phone-login files passed. The fullnpm run verify(server 647, app 463, admin web 325) passed, andcodex exec reviewhad no findings.
At a glance
| Group | What was done | Key point |
|---|---|---|
| PR 211 | Four problems with dates, units and overlapping saves | Same module row lock sets the order |
| PR 212 | Login during recovery, link table cleanup, lock order, Apple sub |
Re-check number and status under the lock |
| PR 213 | Create the session inside the user row lock | Suspension also cuts off the session just created |
Who did what
- I: I ran all three jobs on my Windows computer. I left the edit path in PR 211 (below) in the PR description for my confirmation.
- Claude (Sonnet 5.5): Implemented and checked PR 211 and 212, and checked Apple’s documentation.
- Claude (Opus 5.5): Found the gap while reviewing PR 212, and planned, implemented and checked PR 213.
- Codex (
gpt-6.1-terra): Wrote the tests, and its review (codex exec review) caught the number re-comparison in 5-1 and the regressions in 4-4.
What was not checked
- The 4-3 concurrency test repeats 20 times and is probabilistic. A failure before the fix was confirmed, but it does not always produce the overlap.
- The item read in the edit path (
versions.service.ts) does not take the 4-2 lock, because the plan named only record saving. The same overlap is theoretically possible. - Migration
0004was run only against a development database. - Withdrawal and number change are blocked the same way, but PR 213 did not test them separately.
What I learned
- A gap between “check” and “create” has to be closed with a lock. If the place that reads the status and the place that creates the session are apart, a suspension or recovery slipping in between loses its effect.
- Locks need a single order. If two places take them in opposite order, they wait on each other and hang.
- Work inside one transaction should use one connection. Using another one can make it wait on itself or exhaust the connection pool.
- A new parser’s tests should include edge inputs (no seconds, year 0099). A code review caught what the full verification missed.
- Check the assumptions in a handed-over task description against the real code. The PR 213 description said PR 212 had probably already changed
issueAppto take the connection, but it had not.