Skip to content

[5.x]: session_start(): Unexpected end of serialized data #16859

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

Open
thupsi opened this issue Mar 10, 2025 · 7 comments
Open

[5.x]: session_start(): Unexpected end of serialized data #16859

thupsi opened this issue Mar 10, 2025 · 7 comments
Labels

Comments

@thupsi
Copy link

thupsi commented Mar 10, 2025

What happened?

Description

I have a project that I'm trying to upgrade from Craft 4 to Craft 5. In some of my templates I use both craft.app.session and the cookies plugin. I'm also fetching data from an external DB following this article. In those templates, I get the following error:

yii\base\ErrorException: session_start(): Unexpected end of serialized data in /var/www/html/vendor/yiisoft/yii2/web/Session.php:150
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/web/ErrorHandler.php(115): yii\base\ErrorHandler->handleError(8, 'session_start()...', '/var/www/html/v...', 150)
#1 [internal function]: craft\web\ErrorHandler->handleError(8, 'session_start()...', '/var/www/html/v...', 150)
#2 /var/www/html/vendor/yiisoft/yii2/web/Session.php(150): session_start()
#3 /var/www/html/vendor/yiisoft/yii2/web/Session.php(750): yii\web\Session->open()
#4 /var/www/html/vendor/craftcms/cms/src/helpers/Session.php(95): yii\web\Session->has('__duration')
#5 /var/www/html/vendor/craftcms/cms/src/web/User.php(528): craft\helpers\Session::has('__duration')
#6 /var/www/html/vendor/yiisoft/yii2/web/User.php(199): craft\web\User->renewAuthStatus()
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(455): yii\web\User->getIdentity()
#8 /var/www/html/vendor/craftcms/cms/src/web/Application.php(112): craft\web\Application->debugBootstrap()
#9 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#10 /var/www/html/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct(Array)
#11 [internal function]: yii\base\Application->__construct(Array)
#12 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs(Array)
#13 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#14 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#15 /var/www/html/vendor/craftcms/cms/src/Craft.php(71): yii\BaseYii::createObject(Array, Array)
#16 /var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php(343): Craft::createObject(Array)
#17 /var/www/html/vendor/craftcms/cms/bootstrap/web.php(40): require('/var/www/html/v...')
#18 /var/www/html/public/index.php(11): require('/var/www/html/v...')
#19 {main}

Notes:

  • If I reload the page it works.
  • When I re-reload it errors again.
  • If I clear the cache with ctrl+F5 it seems to work for some time... until it doesn't.
  • When devMode is disabled the error is hidden and the page loads just fine.

This is probably my fault because I'm doing some weird things in my templates, so — for now — don't lose too much time looking into this, I'd just like a nudge in the right direction. Do you have any obvious pointers based on the above?

I 'll come back to you with more details to help you reproduce if I'm unable to solve by myself, or I'm more confident it's a Craft bug.

Craft CMS version

5.6.11

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@thupsi thupsi added the bug label Mar 10, 2025
@thupsi
Copy link
Author

thupsi commented Mar 11, 2025

Update: Solved this by switching to Redis for session storage. Feel free to close if you think it's not something that's likely to affect others.

@brandonkelly
Copy link
Member

Nothing changed in Craft 5 related to PHP session data. It sounds like maybe some session data got corrupted somehow?

@thupsi
Copy link
Author

thupsi commented Mar 15, 2025

Could be, but I wouldn't know how to find out! Can I delete all session data?

@brandonkelly
Copy link
Member

Yeah, PHP stores it whenever the session.save_path php.ini setting is set.

@thupsi
Copy link
Author

thupsi commented Mar 24, 2025

Hi, I found time to do some more testing and:

  1. I created a new project from the same repository, so as to have no DB data that was possibly corrupted.
  2. I created one property entry, named test and one region entry named test region.
  3. In the test property I selected the test region in the region entry field.
  4. I created a minimal template:
<!DOCTYPE html>
<html lang="en">
<head>


</head>
<body>

    {% set property = craft.entries()
                .section('properties')
                .slug('test')
                .one() %}
        
    {% set region = property.region %}
            
    {% do craft.app.session.set('property', property) %}

    <p>test</p>
</body>
</html>
	

The template triggers the error, so it seems there is a Craft bug?

Note: Any similar template does not trigger errors in other projects, so I assume there must be something in the project config of this particular project, since we elminated the DB.

Would you like me to send the composer and project config files to check?

@brandonkelly
Copy link
Member

Would you like me to send the composer and project config files to check?

Yes please, as well as any custom plugins/modules the project is using.

@thupsi
Copy link
Author

thupsi commented Mar 26, 2025

I just sent the files, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants