Skip to content

fix(features): return 400 instead of 500 on feature name race condition#7838

Open
narendraio wants to merge 1 commit into
Flagsmith:mainfrom
narendraio:fix/6617-feature-name-race-condition
Open

fix(features): return 400 instead of 500 on feature name race condition#7838
narendraio wants to merge 1 commit into
Flagsmith:mainfrom
narendraio:fix/6617-feature-name-race-condition

Conversation

@narendraio

Copy link
Copy Markdown

Description (replace the blank template with this):

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #6617

When two requests create a feature with the same name concurrently, both can
pass validate_name before either is persisted. The second insert then trips
the case-insensitive lowercase_feature_name unique constraint, raising an
unhandled IntegrityError that surfaces to the client as an HTTP 500.

This catches the IntegrityError during creation and re-raises it as a
validation error, so the client receives a clear 400 response consistent with
the existing duplicate-name validation. The duplicate-name message is extracted
into a shared constant so both code paths stay in sync.

How did you test this code?

Added a unit test (test_create_feature__name_race_condition__returns_400) that
simulates the race by letting validate_name pass while the database still
rejects the duplicate name, and asserts the endpoint responds with a 400 and the
expected error message. Verified locally against Postgres:

make test opts='tests/unit/features/test_unit_features_views.py -k name_race_condition'

When two requests create a feature with the same name concurrently, both
can pass `validate_name` before either is persisted. The second insert then
trips the case-insensitive `lowercase_feature_name` unique constraint,
raising an unhandled `IntegrityError` that surfaces as an HTTP 500.

Catch the `IntegrityError` during creation and re-raise it as a validation
error so the client receives a clear 400 response, consistent with the
existing duplicate-name validation.

Closes Flagsmith#6617
@narendraio narendraio requested a review from a team as a code owner June 19, 2026 18:00
@narendraio narendraio requested review from khvn26 and removed request for a team June 19, 2026 18:00
@vercel

vercel Bot commented Jun 19, 2026

Copy link
Copy Markdown

@narendraio is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the api Issue related to the REST API label Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race conditions are met with HTTP 500 by Core API

1 participant