Problem/Motivation
We are getting the following error messages every time a user is logging in:
Warning: session_destroy(): Trying to destroy uninitialized session in Drupal\Core\Session\SessionManager->destroy() (Zeile 255 in /var/www/web/core/lib/Drupal/Core/Session/SessionManager.php)
#0 /var/www/web/core/includes/bootstrap.inc(312): _drupal_error_handler_real(2, 'session_destroy...', '/var/www/drupal...', 255)
#1 [internal function]: _drupal_error_handler(2, 'session_destroy...', '/var/www/drupal...', 255, Array)
#2 /var/www/web/core/lib/Drupal/Core/Session/SessionManager.php(255): session_destroy()
#3 /var/www/web/modules/contrib/legal/legal.module(482): Drupal\Core\Session\SessionManager->destroy()
#4 [internal function]: legal_user_login(Object(Drupal\user\Entity\User))
#5 /var/www/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('legal_user_logi...', Array)
#6 /var/www/web/core/modules/user/user.module(466): Drupal\Core\Extension\ModuleHandler->invokeAll('user_login', Array)
#7 /var/www/web/core/modules/user/src/Form/UserLoginForm.php(156): user_login_finalize(Object(Drupal\user\Entity\User))
#8 [internal function]: Drupal\user\Form\UserLoginForm->submitForm(Array, Object(Drupal\Core\Form\FormState))
#9 /var/www/web/core/lib/Drupal/Core/Form/FormSubmitter.php(113): call_user_func_array(Array, Array)
#10 /var/www/web/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#11 /var/www/web/core/lib/Drupal/Core/Form/FormBuilder.php(593): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#12 /var/www/web/core/lib/Drupal/Core/Form/FormBuilder.php(321): Drupal\Core\Form\FormBuilder->processForm('user_login_form', Array, Object(Drupal\Core\Form\FormState))
#13 /var/www/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\user\Form\UserLoginForm), Object(Drupal\Core\Form\FormState))
#14 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#15 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#16 /var/www/web/core/lib/Drupal/Core/Render/Renderer.php(578): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#17 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#18 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#19 /var/www/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#20 /var/www/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#21 /var/www/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /var/www/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/www/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/www/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/www/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/www/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /var/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /var/www/web/core/lib/Drupal/Core/DrupalKernel.php(717): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /var/www/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#30 {main}
The problem is, that in such cases, there is no session yet. Still, the code is calling \Drupal::service('session_manager')->destroy();
which throws that error.
As a solution, we could either check with session_status()
before calling this or we could make sure, a session got started before calling it. The latter would make sure that the remaining code from the service gets called too, but since there is only cookie related stuff which will not be relevant as long as no session for started, the former approach is certainly better.
I'll post an MR in a minute.