ActiveRecord::RecordNotUnique : duplicate key value violates unique constraint "index_terraform_state_versions_on_state_id_and_version"

Summary

When refreshing Terraform state in the GitLab Terraform state backend, I run into intermittent 500 errors.

Steps to reproduce

  1. Initialize a new Terraform state
  2. Add a resource to the state
  3. Run terraform refresh (it sometimes works, maybe try a few times)

What is the current bug behavior?

$ terraform init     

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of gitlabhq/gitlab from the dependency lock file
- Installing gitlabhq/gitlab v3.3.0...
- Installed gitlabhq/gitlab v3.3.0 (signed by a HashiCorp partner, key ID BC097C3333027B14)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

$ terraform refresh
Empty or non-existent state file.

Refresh will do nothing. Refresh does not error or return an erroneous
exit status because many automation scripts use refresh, plan, then apply
and may not have a state file yet for the first run.

     
Warning: Empty or non-existent state

There are currently no resources tracked in the state, so there is nothing to
refresh.


Error: Failed to write state: Failed to upload state: POST https://git.example.net/api/v4/projects/70/terraform/state/namespace-customers?ID=96cb969a-0e96-2dc9-e581-e6c589122bd0 giving up after 3 attempts

$ terraform import gitlab_group.customers 23      
gitlab_group.customers: Importing from ID "23"...
gitlab_group.customers: Import prepared!
  Prepared gitlab_group for import
gitlab_group.customers: Refreshing state... [id=23]
Error writing state file: Failed to upload state: POST https://git.example.net/api/v4/projects/70/terraform/state/namespace-customers?ID=xxx giving up after 3 attempts

What is the expected correct behavior?

Terraform state should be stored.

Relevant logs and/or screenshots

*** /var/log/gitlab/production.log ***
Started POST "/api/v4/projects/70/terraform/state/namespace-customers?ID=xxx" for 10.0.0.65 at 2020-12-07 13:23:03 +0000
Processing by Gitlab::RequestForgeryProtection::Controller#index as HTML
  Parameters: {"version"=>4, "terraform_version"=>"0.14.0", "serial"=>0, "lineage"=>"f893afb6-d498-8a46-a9fb-af6ec425f2fa", "outputs"=>{}, "resources"=>[{"mode"=>"managed", "type"=>"gitlab_group", "name"=>"customers", "provider"=>"provider[\"registry.terraform.io/gitlabhq/gitlab\"]", "instances"=>[{"schema_version"=>0, "attributes"=>{"auto_devops_enabled"=>false, "description"=>"[FILTERED]", "emails_disabled"=>false, "full_name"=>"Customer Resources", "full_path"=>"customers", "id"=>"23", "lfs_enabled"=>true, "mentions_disabled"=>false, "name"=>"Customer Resources", "parent_id"=>0, "path"=>"customers", "project_creation_level"=>"maintainer", "request_access_enabled"=>false, "require_two_factor_authentication"=>false, "runners_token"=>"[FILTERED]", "share_with_group_lock"=>false, "subgroup_creation_level"=>"owner", "two_factor_grace_period"=>48, "visibility_level"=>"private", "web_url"=>"https://git.example.net/groups/customers"}, "sensitive_attributes"=>[], "private"=>"eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="}]}], "ID"=>"b41d6cdd-b650-fae0-3213-6a83ddfda132", "request_forgery_protection"=>{"version"=>4, "terraform_version"=>"0.14.0", "serial"=>0, "lineage"=>"f893afb6-d498-8a46-a9fb-af6ec425f2fa", "outputs"=>{}, "resources"=>[{"mode"=>"managed", "type"=>"gitlab_group", "name"=>"customers", "provider"=>"provider[\"registry.terraform.io/gitlabhq/gitlab\"]", "instances"=>[{"schema_version"=>0, "attributes"=>{"auto_devops_enabled"=>false, "description"=>"[FILTERED]", "emails_disabled"=>false, "full_name"=>"Customer Resources", "full_path"=>"customers", "id"=>"23", "lfs_enabled"=>true, "mentions_disabled"=>false, "name"=>"Customer Resources", "parent_id"=>0, "path"=>"customers", "project_creation_level"=>"maintainer", "request_access_enabled"=>false, "require_two_factor_authentication"=>false, "runners_token"=>"[FILTERED]", "share_with_group_lock"=>false, "subgroup_creation_level"=>"owner", "two_factor_grace_period"=>48, "visibility_level"=>"private", "web_url"=>"https://git.example.net/groups/customers"}, "sensitive_attributes"=>[], "private"=>"eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="}]}]}}
Can't verify CSRF token authenticity.
This CSRF token verification failure is handled internally by `GitLab::RequestForgeryProtection`
Unlike the logs may suggest, this does not result in an actual 422 response to the user
For API requests, the only effect is that `current_user` will be `nil` for the duration of the request
Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms | Elasticsearch: 0.0ms | Allocations: 325)

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_terraform_state_versions_on_state_id_and_version"
DETAIL:  Key (terraform_state_id, version)=(116, 0) already exists.
):
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:675:in `exec_params'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:675:in `block (2 levels) in exec_no_cache'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:674:in `block in exec_no_cache'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract_adapter.rb:721:in `block in log'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract_adapter.rb:712:in `log'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:673:in `exec_no_cache'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:654:in `execute_and_clear'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/marginalia-1.9.0/lib/marginalia.rb:94:in `execute_and_clear_with_marginalia'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `exec_query'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/database_statements.rb:135:in `exec_insert'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:132:in `exec_insert'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/database_statements.rb:166:in `insert'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/query_cache.rb:22:in `insert'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/persistence.rb:375:in `_insert_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/persistence.rb:932:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/counter_cache.rb:166:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/locking/optimistic.rb:70:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/attribute_methods/dirty.rb:211:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/callbacks.rb:331:in `block in _create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/callbacks.rb:101:in `run_callbacks'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/callbacks.rb:825:in `_run_create_callbacks'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/callbacks.rb:331:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/timestamp.rb:110:in `_create_record'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/persistence.rb:905:in `create_or_update'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/callbacks.rb:327:in `block in create_or_update'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/callbacks.rb:135:in `run_callbacks'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/callbacks.rb:825:in `_run_save_callbacks'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/callbacks.rb:327:in `create_or_update'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/timestamp.rb:128:in `create_or_update'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/persistence.rb:503:in `save!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/validations.rb:53:in `save!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:318:in `block in save!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/database_statements.rb:278:in `transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:212:in `transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:318:in `save!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/suppressor.rb:48:in `save!'
  /srv/gitlab/app/models/terraform/state.rb:104:in `create_new_version!'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `block in create_new_version!'
  /srv/gitlab/lib/gitlab/metrics/method_call.rb:27:in `measure'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `create_new_version!'
  /srv/gitlab/app/models/terraform/state.rb:60:in `update_file!'
  /srv/gitlab/lib/api/terraform/state.rb:56:in `block (4 levels) in <class:State>'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:22:in `block in handle_with_lock'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:63:in `block (2 levels) in retrieve_with_lock'
  /srv/gitlab/lib/gitlab/optimistic_locking.rb:11:in `block in retry_lock'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.3.3/lib/active_record/transactions.rb:212:in `transaction'
  /srv/gitlab/lib/gitlab/optimistic_locking.rb:10:in `retry_lock'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:63:in `block in retrieve_with_lock'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:63:in `tap'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:63:in `retrieve_with_lock'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `block in retrieve_with_lock'
  /srv/gitlab/lib/gitlab/metrics/method_call.rb:27:in `measure'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `retrieve_with_lock'
  /srv/gitlab/app/services/terraform/remote_state_handler.rb:19:in `handle_with_lock'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `block in handle_with_lock'
  /srv/gitlab/lib/gitlab/metrics/method_call.rb:27:in `measure'
  /srv/gitlab/lib/gitlab/metrics/instrumentation.rb:160:in `handle_with_lock'
  /srv/gitlab/lib/api/terraform/state.rb:55:in `block (3 levels) in <class:State>'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:59:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:59:in `block (2 levels) in generate_api_method'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/notifications.rb:182:in `instrument'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:58:in `block in generate_api_method'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:341:in `execute'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:267:in `block in run'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/notifications.rb:180:in `block in instrument'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/notifications.rb:180:in `instrument'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:247:in `run'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:322:in `block in build_stack'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:35:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:35:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/ee/lib/gitlab/middleware/ip_restrictor.rb:14:in `block in call'
  /srv/gitlab/ee/lib/gitlab/ip_address_state.rb:10:in `with'
  /srv/gitlab/ee/lib/gitlab/middleware/ip_restrictor.rb:13:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:35:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/lib/api/api_guard.rb:208:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:35:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-oauth2-1.16.0/lib/rack/oauth2/server/resource.rb:20:in `_call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-oauth2-1.16.0/lib/rack/oauth2/server/resource/bearer.rb:8:in `_call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-oauth2-1.16.0/lib/rack/oauth2/server/abstract/handler.rb:17:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/error.rb:39:in `block in call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/error.rb:38:in `catch'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/error.rb:38:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape_logging-1.8.3/lib/grape_logging/middleware/request_logger.rb:60:in `block in call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape_logging-1.8.3/lib/grape_logging/middleware/request_logger.rb:58:in `catch'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape_logging-1.8.3/lib/grape_logging/middleware/request_logger.rb:58:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/middleware/base.rb:28:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:231:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/endpoint.rb:225:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router/route.rb:58:in `exec'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:116:in `process_route'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:72:in `block in identity'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:91:in `transaction'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:70:in `identity'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:55:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:132:in `with_optimization'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/router.rb:54:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/api/instance.rb:167:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/api/instance.rb:71:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/grape-1.4.0/lib/grape/api/instance.rb:66:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/routing/mapper.rb:48:in `serve'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/journey/router.rb:49:in `block in serve'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/journey/router.rb:32:in `each'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/journey/router.rb:32:in `serve'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/routing/route_set.rb:834:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-1.9.0/lib/omniauth/strategy.rb:420:in `call_app!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-saml-1.10.0/lib/omniauth/strategies/saml.rb:86:in `other_phase'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-1.9.0/lib/omniauth/strategy.rb:190:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-1.9.0/lib/omniauth/strategy.rb:169:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-1.9.0/lib/omniauth/strategy.rb:192:in `call!'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/omniauth-1.9.0/lib/omniauth/strategy.rb:169:in `call'
  /srv/gitlab/lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
  /srv/gitlab/lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
  /srv/gitlab/lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
  /srv/gitlab/lib/gitlab/metrics/transaction.rb:61:in `run'
  /srv/gitlab/lib/gitlab/metrics/rack_middleware.rb:16:in `call'
  /srv/gitlab/lib/gitlab/request_profiler/middleware.rb:17:in `call'
  /srv/gitlab/lib/gitlab/jira/middleware.rb:19:in `call'
  /srv/gitlab/lib/gitlab/middleware/go.rb:20:in `call'
  /srv/gitlab/lib/gitlab/etag_caching/middleware.rb:21:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/batch-loader-1.4.0/lib/batch_loader/middleware.rb:11:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-attack-6.3.0/lib/rack/attack.rb:97:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/apollo_upload_server-2.0.2/lib/apollo_upload_server/middleware.rb:20:in `call'
  /srv/gitlab/lib/gitlab/middleware/multipart.rb:234:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-attack-6.3.0/lib/rack/attack.rb:111:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-cors-1.0.6/lib/rack/cors.rb:98:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:40:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/http/content_security_policy.rb:18:in `call'
  /srv/gitlab/lib/gitlab/middleware/read_only/controller.rb:50:in `call'
  /srv/gitlab/lib/gitlab/middleware/read_only.rb:18:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/cookies.rb:648:in `call'
  /srv/gitlab/lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/callbacks.rb:101:in `run_callbacks'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
  /srv/gitlab/lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
  /srv/gitlab/lib/gitlab/middleware/basic_health_check.rb:25:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/rack/logger.rb:37:in `call_app'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/rack/logger.rb:26:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/tagged_logging.rb:80:in `block in tagged'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/tagged_logging.rb:28:in `tagged'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.3/lib/active_support/tagged_logging.rb:80:in `tagged'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/rack/logger.rb:26:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
  /srv/gitlab/lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
  /srv/gitlab/lib/gitlab/middleware/request_context.rb:23:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/request_store-1.5.0/lib/request_store/middleware.rb:19:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-timeout-0.5.2/lib/rack/timeout/core.rb:123:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-timeout-0.5.2/lib/rack/timeout/support/timeout.rb:19:in `timeout'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-timeout-0.5.2/lib/rack/timeout/core.rb:122:in `call'
  /srv/gitlab/config/initializers/fix_local_cache_middleware.rb:9:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/host_authorization.rb:76:in `call'
  /srv/gitlab/lib/gitlab/metrics/requests_rack_middleware.rb:76:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-labkit-0.13.1/lib/labkit/middleware/rack.rb:19:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-labkit-0.13.1/lib/labkit/context.rb:32:in `with_context'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-labkit-0.13.1/lib/labkit/middleware/rack.rb:18:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.3.3/lib/action_dispatch/middleware/request_id.rb:27:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/sentry-raven-3.0.4/lib/raven/integrations/rack.rb:51:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/engine.rb:527:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/railtie.rb:190:in `public_send'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.3/lib/rails/railtie.rb:190:in `method_missing'
  /srv/gitlab/lib/gitlab/middleware/release_env.rb:12:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/urlmap.rb:74:in `block in call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `each'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/urlmap.rb:58:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-puma-4.3.5.gitlab.3/lib/puma/configuration.rb:228:in `call'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-puma-4.3.5.gitlab.3/lib/puma/server.rb:722:in `handle_request'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-puma-4.3.5.gitlab.3/lib/puma/server.rb:481:in `process_client'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-puma-4.3.5.gitlab.3/lib/puma/server.rb:335:in `block in run'
  /srv/gitlab/vendor/bundle/ruby/2.7.0/gems/gitlab-puma-4.3.5.gitlab.3/lib/puma/thread_pool.rb:138:in `block in spawn_thread'

Results of GitLab environment info

System information
System:		
Proxy:		https_proxy: http://proxy.example.net:3128
		http_proxy: http://proxy.example.net:3128
		no_proxy: 169.254.0.0/16,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,127.0.0.0/8,localhost,.default,.svc,.cluster.local,.cluster.local.,.example.net,.gitlab-prod
Current User:	git
Using RVM:	no
Ruby Version:	2.7.2p137
Gem Version:	3.1.4
Bundler Version:2.1.4
Rake Version:	13.0.1
Redis Version:	unknown
Git Version:	unknown
Sidekiq Version:5.2.9
Go Version:	unknown

GitLab information
Version:	13.6.1-ee
Revision:	a0e59de39b3
Directory:	/srv/gitlab
DB Adapter:	PostgreSQL
DB Version:	12.4
URL:		https://git.example.net
HTTP Clone URL:	https://git.example.net/some-group/some-project.git
SSH Clone URL:	git@git.example.net:some-group/some-project.git
Elasticsearch:	no
Geo:		no
Using LDAP:	yes
Using Omniauth:	yes
Omniauth Providers: saml

GitLab Shell
Version:	13.13.0
Repository storage paths:
- default: 	/var/opt/gitlab/repo
GitLab Shell path:		/home/git/gitlab-shell
Git:		/usr/bin/git

Results of GitLab application Check

Using the Cloud Native Helm Chart

Possible fixes

?