Commit f1543604 authored by Stan Hu's avatar Stan Hu

Fix error 500s creating projects concurrently

If multiple projects within a group are created concurrently, a
validation error due to duplicate ProjectAuthorization entries could be
triggered. This can happen due to a race condition: if the user's or
group's authorizations get refreshed before the
`Projects::CreateService` does this, we can get a database unique key
violation.

To fix this, we use `safe_find_or_create_by!` to ensure that a duplicate
entry doesn't get inserted.

This fixes a regression introduced in GitLab 13.4 via
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/42335.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/233862
parent 07274bf4
......@@ -67,7 +67,7 @@ module Projects
@project
rescue ActiveRecord::RecordInvalid => e
message = "Unable to save #{e.record.type}: #{e.record.errors.full_messages.join(", ")} "
message = "Unable to save #{e.inspect}: #{e.record.errors.full_messages.join(", ")}"
fail(error: message)
rescue => e
@project.errors.add(:base, e.message) if @project
......@@ -122,8 +122,9 @@ module Projects
only_concrete_membership: true)
if group_access_level > GroupMember::NO_ACCESS
current_user.project_authorizations.create!(project: @project,
access_level: group_access_level)
current_user.project_authorizations.safe_find_or_create_by!(
project: @project,
access_level: group_access_level)
end
if Feature.enabled?(:specialized_project_authorization_workers)
......
---
title: Fix error 500s creating projects concurrently
merge_request: 48571
author:
type: fixed
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment