Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: fix the 500 for racy creation attempts #3415

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import flask

from flask_restx import Namespace, Resource
from sqlalchemy.exc import IntegrityError

from coprs.views.apiv3_ns import (
get_copr,
Expand All @@ -15,7 +16,7 @@
editable_copr,
)
from coprs.views.apiv3_ns.json2form import get_form_compatible_data, get_input_dict
from coprs import db, models, forms, db_session_scope
from coprs import app, db, models, forms, db_session_scope
from coprs.views.misc import api_login_required
from coprs.views.apiv3_ns import rename_fields_helper, api, query_to_parameters
from coprs.views.apiv3_ns.schema.schemas import (
Expand Down Expand Up @@ -228,13 +229,14 @@ def post(self, ownername, exist_ok=False):
if form.bootstrap.data is not None:
bootstrap = form.bootstrap.data

projectname = form.name.data.strip()
try:

def _form_field_repos(form_field):
return " ".join(form_field.data.split())

copr = CoprsLogic.add(
name=form.name.data.strip(),
name=projectname,
repos=_form_field_repos(form.repos),
user=user,
selected_chroots=form.selected_chroots,
Expand Down Expand Up @@ -265,6 +267,16 @@ def _form_field_repos(form_field):
storage=form.storage.data,
)
db.session.commit()
except IntegrityError as ierr:
app.log.debug("Racy attempt to create %s/%s", ownername, projectname)
db.session.rollback()
if exist_ok:
copr = get_copr(ownername, projectname)
return to_dict(copr)
raise DuplicateException(
Fixed Show fixed Hide fixed
f"Copr '{ownername}/{projectname}' has not been created "
"(race condition)"
) from ierr
except (
DuplicateException,
NonAdminCannotCreatePersistentProject,
Expand Down