diff --git a/app/services/katello/rhsm_fact_parser.rb b/app/services/katello/rhsm_fact_parser.rb index 3a96c05fc77..f2f63a7bf85 100644 --- a/app/services/katello/rhsm_fact_parser.rb +++ b/app/services/katello/rhsm_fact_parser.rb @@ -73,7 +73,13 @@ def operatingsystem os_attributes[:name] = "CentOS" end - ::Operatingsystem.find_or_create_by(os_attributes) + os = ::Operatingsystem.find_by(os_attributes) + if os.blank? + created = ::Operatingsystem.create_or_find_by(os_attributes) + created.errors.any? ? ::Operatingsystem.find_by(os_attributes) : created + else + os + end end end diff --git a/test/unit/katello/rhsm_fact_parser_test.rb b/test/unit/katello/rhsm_fact_parser_test.rb index 2cf08502cfe..a5cb78e1117 100644 --- a/test/unit/katello/rhsm_fact_parser_test.rb +++ b/test/unit/katello/rhsm_fact_parser_test.rb @@ -223,5 +223,18 @@ def test_uname_architecture assert 'i386', parser.architecture.name end + + def test_operatingsystem_race_condition_handling + existing_os = ::Operatingsystem.create(name: 'RedHat', major: '9', minor: '') + ::Operatingsystem.expects(:find_by).twice.returns(nil) + @facts['distribution.name'] = 'Red Hat Enterprise Linux' + @facts['distribution.version'] = '9' + @facts['distribution.id'] = 'Nine' + + assert_nothing_raised do + parser.operatingsystem + existing_os.destroy + end + end end end