DB Error: already exists | Same new user is saved twice
Recently, one of my CiviCRM installations (CiviCRM 5.74.3, Drupal 10.2.7) using samlauth declined authenticating users a certain user with the following error message:
Error encountered while processing SAML authentication response; details have been logged.
On the side of the IdP nothing seems at odd, but the Drupal log contains two entries for every failed (but actually valid) login attempt:
- user: Civi\Core\Exception\DBQueryException: DB Error: already exists in CRM_Core_Error::exceptionHandler() (line 974 of /var/www/[HOST]/vendor/civicrm/civicrm-core/CRM/Core/Error.php).
Backtrace
#0 /var/www/[HOST]/vendor/pear/pear-core-minimal/src/PEAR.php(945): CRM_Core_Error::exceptionHandler()
#1 /var/www/[HOST]/vendor/pear/db/DB.php(1000): PEAR_Error->__construct()
#2 /var/www/[HOST]/vendor/pear/pear-core-minimal/src/PEAR.php(575): DB_Error->__construct()
#3 /var/www/[HOST]/vendor/pear/pear-core-minimal/src/PEAR.php(223): PEAR::_raiseError()
#4 /var/www/[HOST]/vendor/pear/db/DB/common.php(1951): PEAR->__call()
#5 /var/www/[HOST]/vendor/pear/db/DB/mysqli.php(965): DB_common->raiseError()
#6 /var/www/[HOST]/vendor/pear/db/DB/mysqli.php(412): DB_mysqli->mysqliRaiseError()
#7 /var/www/[HOST]/vendor/pear/db/DB/common.php(1240): DB_mysqli->simpleQuery()
#8 /var/www/[HOST]/vendor/civicrm/civicrm-packages/DB/DataObject.php(2696): DB_common->query()
#9 /var/www/[HOST]/vendor/civicrm/civicrm-packages/DB/DataObject.php(1539): DB_DataObject->_query()
#10 /var/www/[HOST]/vendor/civicrm/civicrm-core/CRM/Core/DAO.php(733): DB_DataObject->update()
#11 /var/www/[HOST]/vendor/civicrm/civicrm-core/CRM/Core/BAO/UFMatch.php(40): CRM_Core_DAO->save()
#12 /var/www/[HOST]/vendor/civicrm/civicrm-core/CRM/Core/BAO/UFMatch.php(375): CRM_Core_BAO_UFMatch::create()
#13 /var/www/[HOST]/web/modules/contrib/civicrm/civicrm.user.inc(75): CRM_Core_BAO_UFMatch::updateContactEmail()
#14 [internal function]: civicrm_user_update()
#15 /var/www/[HOST]/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(409): call_user_func_array()
#16 /var/www/[HOST]/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(388): Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}()
#17 /var/www/[HOST]/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(416): Drupal\Core\Extension\ModuleHandler->invokeAllWith()
#18 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(215): Drupal\Core\Extension\ModuleHandler->invokeAll()
#19 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(900): Drupal\Core\Entity\EntityStorageBase->invokeHook()
#20 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(564): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook()
#21 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(781): Drupal\Core\Entity\EntityStorageBase->doPostSave()
#22 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(489): Drupal\Core\Entity\ContentEntityStorageBase->doPostSave()
#23 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save()
#24 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
#25 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(756): Drupal\Core\Entity\EntityBase->save()
#26 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(538): Drupal\samlauth\SamlService->synchronizeUserAttributes()
#27 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(365): Drupal\samlauth\SamlService->doLogin()
#28 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/SamlController.php(270): Drupal\samlauth\SamlService->acs()
#29 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(184): Drupal\samlauth\Controller\SamlController->Drupal\samlauth\Controller\{closure}()
#30 /var/www/[HOST]/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\samlauth\Controller\SamlController->Drupal\samlauth\Controller\{closure}()
#31 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#32 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(222): Drupal\samlauth\Controller\SamlController->executeInRenderContext()
#33 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/SamlController.php(273): Drupal\samlauth\Controller\SamlController->getTrustedRedirectResponse()
#34 [internal function]: Drupal\samlauth\Controller\SamlController->acs()
#35 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#36 /var/www/[HOST]/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#37 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#38 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#39 /var/www/[HOST]/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#40 /var/www/[HOST]/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#41 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#42 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#43 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#44 /var/www/[HOST]/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#45 /var/www/[HOST]/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#46 /var/www/[HOST]/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#47 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#48 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#49 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#50 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#51 /var/www/[HOST]/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#52 /var/www/[HOST]/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#53 {main}
- samlauth: Drupal\Core\Entity\EntityStorageException encountered while processing SAML authentication response: DB Error: already exists in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Backtrace
#0 /var/www/[HOST]/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
#1 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(756): Drupal\Core\Entity\EntityBase->save()
#2 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(538): Drupal\samlauth\SamlService->synchronizeUserAttributes()
#3 /var/www/[HOST]/web/modules/contrib/samlauth/src/SamlService.php(365): Drupal\samlauth\SamlService->doLogin()
#4 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/SamlController.php(270): Drupal\samlauth\SamlService->acs()
#5 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(184): Drupal\samlauth\Controller\SamlController->Drupal\samlauth\Controller\{closure}()
#6 /var/www/[HOST]/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\samlauth\Controller\SamlController->Drupal\samlauth\Controller\{closure}()
#7 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#8 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/ExecuteInRenderContextTrait.php(222): Drupal\samlauth\Controller\SamlController->executeInRenderContext()
#9 /var/www/[HOST]/web/modules/contrib/samlauth/src/Controller/SamlController.php(273): Drupal\samlauth\Controller\SamlController->getTrustedRedirectResponse()
#10 [internal function]: Drupal\samlauth\Controller\SamlController->acs()
#11 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#12 /var/www/[HOST]/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#13 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#14 /var/www/[HOST]/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#15 /var/www/[HOST]/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#16 /var/www/[HOST]/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#17 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#18 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#19 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#20 /var/www/[HOST]/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
#21 /var/www/[HOST]/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
#22 /var/www/[HOST]/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#23 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#24 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#25 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#26 /var/www/[HOST]/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#27 /var/www/[HOST]/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#28 /var/www/[HOST]/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#29 {main}
Only one of two very similar instances experiences this error, so I currently don't know how to reproduce it.
Do you have any hint or idea where I can look further to resolve this problem?
At samlauth's issue tracker where I reported this initially, it has been asked whether I "have an event subscriber for the SamlauthEvents::USER_SYNC event, that saves the user? (It shouldn't - it should wait until samlauth saves the user.)" Also, they say: "Line 816 in SqlContentEntityStorage.php suggests that the 'user' in your first error is actually the entity type. So it seems like 'it' is trying to save the same new user twice."
I'm now reporting it here because it seems more of a CiviCRM issue to me.