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:

  1. FormBuilder is building the search form and checks if the current session has batch_form_state attribute
  2. Session::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.

Command icon 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

mogtofu33 created an issue. See original summary.

pdureau’s picture

pdureau’s picture

I am still reproducing after the merge of #3529064: Follow Core 11.3's HTMX integration. Let's keep it in beta2 scope

pdureau’s picture

pdureau’s picture

Not enough time left for beta3

pdureau’s picture

Title: SSE: fail with block with form » SSE: fail when a source is trying to start a session
Issue summary: View changes

I 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.

andres alvarez made their first commit to this issue’s fork.

andres alvarez’s picture

Status: Active » Needs review
andres alvarez’s picture

Confirmed the root cause. ApiSseController::sse() was calling $this->session->getId() inside the EventStreamResponse callback. 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 in ApiSseControllerTest that streams the response without crashing.

mogtofu33’s picture

Assigned: Unassigned » mogtofu33

Thanks @andres-alvarez, I added a little change in the approach while implementing AI Chatbot to act with SSE.

mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.