Skip to content

Return 400 instead of 500 when two requests create the same email at once - #2527

Closed
w-rahul wants to merge 2 commits into
fastapi:masterfrom
w-rahul:fix-duplicate-email-500
Closed

w-rahul wants to merge 2 commits into
fastapi:masterfrom
w-rahul:fix-duplicate-email-500

Conversation

@w-rahul

@w-rahul w-rahul commented Nov 10, 2026

Copy link
Copy Markdown

Signup and create user check if the email is free, then insert. If two requests with the same email come in together, both pass the check. The unique index on user.email stops the second insert, and that request returns a 500.

One after the other: 200, then 400.
At the same time: 200 and 500.

No duplicate user gets created. Only the response is wrong.

This change catches IntegrityError around crud.create_user in both endpoints, rolls back, and returns the same 400 as the check. Two new tests cover it.

I found this with racetrap, a tool I am building that forces the order of SQL statements between two requests: https://github.com/w-rahul/racetrap
After this change it reports no races for these requests.

Signup and create user check the email, then insert.
Two requests with the same email can both pass the check.
The unique index stops the second insert, which answered 500.
Patch the email check to find nothing, so the insert hits
the unique index the way two requests at once do.
@YuriiMotov

Copy link
Copy Markdown
Member

Thanks for your interest and efforts, but please follow the rules of the repository.

We ask people to only open PRs when it's explicitly requested by maintainers.
See https://tiangolo.com/open-source/contributing/ and PR description template:

Please start with a GitHub Discussion.

Once a team member asks you to open a PR, create it and link the discussion here.

I will close this for now to keep things clean.
Thanks for understanding!

@YuriiMotov YuriiMotov closed this Nov 10, 2026
@w-rahul

w-rahul commented Nov 10, 2026

Copy link
Copy Markdown
Author

Sorry, I missed the rule about starting with a discussion. I will open one and link this PR there

@w-rahul

w-rahul commented Nov 10, 2026

Copy link
Copy Markdown
Author

#2528

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