500 - internal error when using api to create issues : potential race condition

Summary

During multiple calls to POST /api/v4/projects/:id/issues, I get a "500 internal error" response

Steps to reproduce

make multiple posts creating issues to the api

What is the current bug behavior?

"500 internal error" response

What is the expected correct behavior?

a new issue created

Relevant logs and/or screenshots

using "gitlab-ctl tail", this error is shown

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_issues_on_project_id_and_iid" DETAIL: Key (project_id, iid)=(9, 10) already exists. : INSERT INTO "issues" ("description", "title", "state", "project_id", "created_at", "assignee_id", "author_id","relative_position", "iid", "title_html", "description_html", "updated_at", "time_estimate") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING "id"):

Results of GitLab environment info

System information
System:		
Proxy:		no
Current User:	git
Using RVM:	no
Ruby Version:	2.3.3p222
Gem Version:	2.6.6
Bundler Version:1.13.7
Rake Version:	10.5.0
Redis Version:	3.2.5
Git Version:	2.11.1
Sidekiq Version:4.2.7

GitLab information Version: 9.1.2-ee Revision: 6488043 Directory: /opt/gitlab/embedded/service/gitlab-rails DB Adapter: postgresql DB Version: 9.6.1 URL: https://git.dev.rtbdashboard.io HTTP Clone URL: https://git.dev.rtbdashboard.io/some-group/some-project.git SSH Clone URL: git@git.dev.rtbdashboard.io:some-group/some-project.git Elasticsearch: no Geo: no Using LDAP: no Using Omniauth: no

GitLab Shell Version: 5.0.2 Repository storage paths:

  • default: /var/opt/gitlab/git-data/repositories Hooks: /opt/gitlab/embedded/service/gitlab-shell/hooks Git: /opt/gitlab/embedded/bin/git

Results of GitLab application Check

Checking GitLab Shell ...

GitLab Shell version >= 5.0.2 ? ... OK (5.0.2) Repo base directory exists? default... yes Repo storage directories are symlinks? default... no Repo paths owned by git:git? default... no User id for git: 998. Groupd id for git: 998 Try fixing it: sudo chown -R git:git /var/opt/gitlab/git-data/repositories For more information see: doc/install/installation.md in section "GitLab Shell" Please fix the error above and rerun the checks. Repo paths access is drwxrws---? default... yes hooks directories in repos are links: ... 1/1 ... repository is empty 9/9 ... repository is empty Running /opt/gitlab/embedded/service/gitlab-shell/bin/check Check GitLab API access: OK Access to /var/opt/gitlab/.ssh/authorized_keys: OK Send ping to redis server: OK gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Checking Reply by email ...

Reply by email is disabled in config/gitlab.yml

Checking Reply by email ... Finished

Checking LDAP ...

LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab ...

Git configured with autocrlf=input? ... yes Database config exists? ... yes All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config outdated? ... no Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory setup correctly? ... skipped (no tmp uploads folder yet) Init script exists? ... skipped (omnibus-gitlab has no init script) Init script up-to-date? ... skipped (omnibus-gitlab has no init script) projects have namespace: ... 1/1 ... yes 9/9 ... yes Redis version >= 2.8.0? ... yes Ruby version >= 2.1.0 ? ... yes (2.3.3) Your git bin path is "/opt/gitlab/embedded/bin/git" Git version >= 2.7.3 ? ... yes (2.11.1) Active users: 8

Checking GitLab ... Finished

Possible fixes

https://gitlab.com/gitlab-org/gitlab-ee/blob/master/app/models/concerns/internal_id.rb

isn't this code finding the last issues record in the "project" , and then adding 1 to the found value ? If so, what happens if 2 processes are trying to create an issue at the same time ? They would both find the last record .. let's say 3 , add 1 and then both have an iid of 4, leading to the contstraint violation seen

Assignee Loading
Time tracking Loading