Receiving the following errors upon recent install.

RuntimeException: Failed to start the session because headers have already been sent by "/home/mysite/www/vendor/symfony/http-foundation/Response.php" at line 361. in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (line 145 of /home/mysite/www/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php) #0 /home/mysite/www/core/lib/Drupal/Core/Session/SessionManager.php(164): Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() #1 /home/mysite/www/core/lib/Drupal/Core/Session/SessionManager.php(195): Drupal\Core\Session\SessionManager->startNow() #2 /home/mysite/www/vendor/symfony/http-foundation/Session/Session.php(196): Drupal\Core\Session\SessionManager->save() #3 /home/mysite/www/core/lib/Drupal/Core/StackMiddleware/Session.php(60): Symfony\Component\HttpFoundation\Session\Session->save() #4 /home/mysite/www/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #5 /home/mysite/www/core/modules/ban/src/BanMiddleware.php(50): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #6 /home/mysite/www/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #7 /home/mysite/www/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #8 /home/mysite/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #9 /home/mysite/www/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #10 /home/mysite/www/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #11 {main}.

Issue fork search404-3087929

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

EurekaWeb created an issue. See original summary.

nicholasthompson’s picture

I'm getting the same thing on one of our sites.

Using curl, if I hit the homepage, its fine. If I hit `/search?terms=test`, it's fine. If I hit `/test` I get a 301 redirect and the location is `/search?terms=test`, however I now have 2 rows in Watchdog. One is recording the 404, the other is recording the exact stack trace reported above.

Commonly, this error is caused by a <?php tag having whitespace before it, or a closing one having whitespace after it...

nicholasthompson’s picture

Ah.. in search404Goto... Drop the `->send()` on the return:

  public function search404Goto($path = '') {
    // Set redirect response.
    $response = new RedirectResponse($path);
    if (\Drupal::config('search404.settings')->get('search404_redirect_301')) {
      $response->setStatusCode(301);
    }
    return $response;
  }

This stops the error and the redirect still works..

nicholasthompson’s picture

Status: Active » Needs review
StatusFileSize
new478 bytes

Patch attached

reevo’s picture

Patch in #4 resolves the issue for me. Thank you! :)

gmaka’s picture

StatusFileSize
new648 bytes

#4 patch fixed the error, but then wasn't redirecting me to my custom path.

ronald van belzen’s picture

Removing the send() causes the same issue as reported by gmaka in #6.

mohit_aghera’s picture

StatusFileSize
new553 bytes
new1.11 KB

Re-roll the last patch for 2.x branch.

robcarr’s picture

Patch #8 worked for me.

+1 for RTBC

anybody’s picture

Version: 8.x-1.x-dev » 2.x-dev
Priority: Normal » Major

Confirming this issue still exists in 2.x and popping up on all our projects. It fills up logs and leads to unexpected functionality. So setting the priority to major.

I'll have a look at the different patches and check, which one is correct.

anybody’s picture

So this is interesting. There are several approaches above.

There are also similar issues in other modules like:

In general

return $response;

would be correct instead of:

return $response->send();

to return in the controller. For example, see https://drupalsun.com/philipnorton42/2022/05/22/drupal-9-correct-way-red...

But that doesn't seem to work as I think this comes too late or is overwritten by a core redirect for 404?

There's also a discussion on exactly this topic:
https://drupal.stackexchange.com/questions/138697/what-function-method-c...

I tried

$response->send();
return;

but that leads to the same error as before.

I also tried the implementation from https://blog.ricovandevin.com/redirecting-to-a-specified-destination-in-... but doesn't really seem to make much sense here.

TL;DR:

The correct solution is #8:

    // Remove unwanted destination.
    $this->requestStack->getCurrentRequest()->query->remove('destination');
    return $response;

It redirects as expected and doesn't lead to any unwanted side-effects. I'll prepare a MR for 2.x with the code, so hopefully this can be merged asap!

anybody’s picture

MR!6 ready for review. Based on #6 and #8 for credits.

Could someone here be so nice to review this?

grevil’s picture

Status: Needs review » Reviewed & tested by the community

LGTM!

Works as expected and fixes the Problem! RTBC +1.

  • emilymathew committed 1c334493 on 2.x authored by Anybody
    Issue #3087929: Failed to start the session because headers have already...
emilymathew’s picture

Sorry for the late response. This is merged. Thank you...

As this is an urgent issue., released new version 2.1.2 with this change.

Thank you..

martijn de wit’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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