Skip to content

fix: handle registration commit errors with proper JSON response#224

Closed
jigangz wants to merge 1 commit intodataelement:mainfrom
jigangz:fix/second-user-registration-error
Closed

fix: handle registration commit errors with proper JSON response#224
jigangz wants to merge 1 commit intodataelement:mainfrom
jigangz:fix/second-user-registration-error

Conversation

@jigangz
Copy link
Copy Markdown

@jigangz jigangz commented Mar 29, 2026

Summary

Fix "Request failed" error when registering the second user account.

Root cause

The /auth/register endpoint only called db.commit() explicitly for the first user (platform admin). For subsequent registrations, the commit was deferred to the get_db() dependency's finally block. If that deferred commit hit an IntegrityError (or any other database error), FastAPI returned a raw 500 response with no JSON body — which the frontend displayed as a generic "Request failed" message.

Fix

  • Add explicit db.commit() for all registration paths
  • Catch IntegrityError and return a proper HTTP 409 JSON response with a descriptive error message
  • Remove redundant intermediate db.flush() calls — the single commit handles both User and Participant creation atomically
  • Unify first-user and non-first-user commit paths

Changes

  • backend/app/api/auth.py: unified commit + IntegrityError handling

Checklist

  • Tested locally
  • No unrelated changes included

Fixes #228

…aelement#223)

When registering the second user (non-admin), the register endpoint
relied on get_db()'s implicit commit after the function returned. If
that deferred commit hit a database constraint violation (IntegrityError),
FastAPI returned a raw 500 response with no JSON body. The frontend
parsed this as 'Request failed' with no useful error message.

Changes:
- Add explicit db.commit() for all registration paths (not just first user)
- Catch IntegrityError and return a proper 409 JSON response
- Remove redundant intermediate db.flush() calls — the single commit
  handles both User and Participant creation atomically
- First-user path: commit now happens before seed_default_agents() as
  before, just unified with the non-first-user path
@wisdomqin
Copy link
Copy Markdown
Contributor

Thank you for the detailed investigation and the well-written PR, @jigangz! We really appreciate you digging into the root cause and putting together a clean fix.

After reviewing the current codebase, the legacy /auth/register handler that this PR targets has since been refactored — registration now flows through a new path (/register/init + registration_service) that includes proper pre-checks for duplicate emails and usernames before any commit is attempted. This significantly reduces the likelihood of the race condition that would trigger the bare-500 path.

Given that the scenario is difficult to hit in practice and the underlying registration flow has been reworked, we are going to close this PR for now. That said, your analysis is valid and the defensive pattern you proposed (wrapping the commit in a try/except IntegrityError) is good practice — we will keep it in mind for a future hardening pass.

Thanks again for contributing!

@wisdomqin wisdomqin closed this Apr 3, 2026
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.

[Bug] Second user registration relies on implicit db.commit(), causing raw 500 on conflict

2 participants