Skip to content

Use proper error message when session write fails #20807 #21421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
Expand Down Expand Up @@ -223,7 +224,28 @@ public function regenerate($destroy = false, $lifetime = null)
*/
public function save()
{
session_write_close();
// Register custom error handler to catch a possible failure warning during session write
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
throw new ContextErrorException($errstr, $errno, E_WARNING, $errfile, $errline, $errcontext);
}, E_WARNING);

try {
session_write_close();
restore_error_handler();
} catch (ContextErrorException $e) {
// The default PHP error message is not very helpful, as it does not give any information on the current save handler.
// Therefore, we catch this error and trigger a warning with a better error message
$handler = $this->getSaveHandler();
if ($handler instanceof SessionHandlerProxy) {
$handler = $handler->getHandler();
}

restore_error_handler();
trigger_error(sprintf(
'session_write_close(): Failed to write session data with %s handler',
get_class($handler)
), E_USER_WARNING);
}

if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
// This condition matches only PHP 5.3 with internal save handlers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function __construct(\SessionHandlerInterface $handler)
$this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user';
}

/**
* @return \SessionHandlerInterface
*/
public function getHandler()
{
return $this->handler;
}

// \SessionHandlerInterface

/**
Expand Down