Problem/Motivation
When adding search block in an SSE session with 2 users, the other user's SSE stream breaks with this error printed in the reponse: "RuntimeException: Failed to start the session because headers have already been sent by EventStreamResponse" at line 94 in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start()
What is happening:
FormBuilderis building the search form and checks if the current session hasbatch_form_stateattributeSession::has()initialize a session storage, but a SSE session already exists.
Backtrace:
Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage-;start() (Line: 116)
Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (Line: 138)
Drupal\Core\Session\SessionManager->startNow() (Line: 103)
Drupal\Core\Session\SessionManager->start() (Line: 282)
...
Symfony\Component\HttpFoundation\Session\Session->has() (Line: 269)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 239)
Drupal\Core\Form\FormBuilder->getForm() (Line: 72)
Drupal\search\Plugin\Block\SearchBlock->build() (Line: 172)
Drupal\ui_patterns\Plugin\UiPatterns\Source\BlockSource->getPropValue() (Line: 81)
...
Proposed resolution
I tried to start a "normal" session in the SSE controller before building the streamed response — not inside the callback:
public function sse(InstanceInterface $display_builder_instance): EventStreamResponse {
$builder_id = (string) $display_builder_instance->id();
+ \Drupal::service('session')->start();
return new EventStreamResponse(function () use ($builder_id) {
$sessionId = $this->session->getId();But it didn't work.
Issue fork display_builder-3546354
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
pdureau commentedComment #3
pdureau commentedI am still reproducing after the merge of #3529064: Follow Core 11.3's HTMX integration. Let's keep it in beta2 scope
Comment #4
pdureau commentedComment #5
pdureau commentedNot enough time left for beta3
Comment #6
pdureau commentedI am still reproducing it, but it is not related to SearchBlock rendering a form (we have many source plugins rendering forms, it is fine), but related to session management. I will update the title and description.
Comment #9
andres alvarez commentedComment #10
andres alvarez commentedConfirmed the root cause.
ApiSseController::sse()was calling$this->session->getId()inside theEventStreamResponsecallback. Once the response starts streaming, headers are already sent, so any source plugin rendered inside the loop that implicitly starts a session crashes with "headers already sent".The fix moves
getId()outside the closure so the session is guaranteed to start before Symfony sends SSE headers. Also added a regression test inApiSseControllerTestthat streams the response without crashing.Comment #11
mogtofu33 commentedThanks @andres-alvarez, I added a little change in the approach while implementing AI Chatbot to act with SSE.
Comment #12
mogtofu33 commented