Yii2: session_regenerate_id(): Failed to create(read) session ID: memcache (path: 11.11.11.11:1111)

Created on 29 Mar 2017  ·  28Comments  ·  Source: yiisoft/yii2

My environment:php7.0.x yii2-2.0.10

I installed php7 and wrote session data to Memcache, and I made sure that the corresponding extensions were installed, either Memcache or Memcached. I get one of the following errors:

PHP Recoverable Error – yii\base\ErrorException
session_regenerate_id(): Failed to create(read) session ID: memcache (path: 11.11.11.11:1111)

The following is the code area where the error occurred:

/**
     * Updates the current session ID with a newly generated one .
     * Please refer to <http://php.net/session_regenerate_id> for more details.
     * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
     */
    public function regenerateID($deleteOldSession = false)
    {
        if ($this->getIsActive()) {
            // add @ to inhibit possible warning due to race condition
            // https://github.com/yiisoft/yii2/pull/1812
            if (YII_DEBUG && !headers_sent()) {
                session_regenerate_id($deleteOldSession);
            } else {
                @session_regenerate_id($deleteOldSession);
            }
        }
    }

The strange question bothered me, I created the test file, simply used the session, including the write or read, everything was normal. I have to doubt that the framework of the problem, suspected frame using the expired function. But all this hope to get your confirmation.

to be verified

Most helpful comment

Seems like you need to upgrade memcached.

This bug was openned with PHP.net about it:
https://bugs.php.net/bug.php?id=71187

They identified that the issue was with the memcached code and they opened this pull request and closed the bug report.
https://github.com/php-memcached-dev/php-memcached/pull/164

The pull request was merged on January 20th so presumably this is available since version 3.0.0 where they mention "Fixes crash with session_regenerate_id (work-around for PHP bug)".
https://github.com/php-memcached-dev/php-memcached/releases/tag/v3.0.0

All 28 comments

How is your session configured? via CacheSession?

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

I remember this being fixed in 2.0.11 ... As I had same issue with Redis

at least I believe it is the same issue

@xiaoxing598 can you try updating Yii?

[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = memcache

I think it is not configured php error caused by the following script I used to test the session is normal.

<?php
ini_set("session.save_handler", "memcached");
//ini_set("session.save_handler", "files");
ini_set("session.save_path", "11.11.11.11:1111");
//ini_set("session.save_path", "/tmp");
session_start();
$_SESSION['AA']='helloworld!';
echo $_SESSION['AA'];

@samdark Which version of the problem in the repair?I will upgrade to the latest and try again

@Renkas I was suspected to be Memcache, but when I changed to Redis, the problem still existed and I began to think that the problem might be somewhere else.

2.0.11 and master.

I have upgraded to the latest version:

- Removing yiisoft/yii2 (2.0.10)
  - Installing yiisoft/yii2 (2.0.11.2)
    Downloading: 100%

Still this problem, the problem seems to have not been resolved.

qq 20170329234524_

Looking at your configuration it seems you are not using Yii's session handling for Memcached but PHP's own implementation.

I'm using Redis session component myself (https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/topics-session.md) and in my previous comment I expected you have similiar solution - but it seems to be quite different thing.

Anyway I don't see this being Yii issue - rather your configuration or PHP's. But sorry can't help anymore.

It seems PHP 7 is raising a fatal error in case of failure to start a session which was not the case previously...

If you modify the php.ini file, let the session be written to the file. The above problem will not happen.

@Renkas Use Memcached not to avoid this problem? Switching to Redis will make me take extra costs because I purchased the cloud service.
If you let Yii manage the session, is there a support for Memcache?

Because of your question:
http://www.yiiframework.com/doc-2.0/yii-web-cachesession.html
http://www.yiiframework.com/doc-2.0/yii-caching-memcache.html

But like @samdark mentioned their seems to be also an other issue.
Could not reproduce on RancherOS php 7.0.15 with memcached as sessionhandler via php.ini.

@samdark This issue has affected the progress of the actual project development, you can help me solve it? thank you very much!

@ItsReddi @Renkas
I try to manage my own session, because the use of Memcache, so according to Rdis Session wrote a class for Memcache, through this way to solve.
But I think the problem should be resolved by the Yii framework, after all, my project is very much, if the above way, I need to spend a lot of time to modify the previous project, it will be very terrible.

@xiaoxing598 could you share the class you've implemented and note what was changed?

_Sorry to reply to you so late today, I just arrived at the office. It is now 10:15 Beijing time_

I declare using a custom session manager

'components' => [
    'session'=>[
            'class'=>'app\components\MemSession',
    ],
]

Imitate the "Redis Session" category for Memcache transformation

<?php

namespace app\components;

use Yii;
use yii\web\Session as WebSession;

/**
 * <p>自定义会话数据维护类<p>
 *
 * 使用此类把会话信息存储到Memcache中,使用此方法后会替换掉PHP自己的会话管理。
 *
 * @package app\components
 */
class MemSession extends WebSession
{
    /**
     * @var string 缓存使用组件
     */
    public $memcache = 'cache';

    /**
     * @var string 键前缀
     */
    public $keyPrefix;

    /**
     * 初始化组件
     */
    public function init()
    {
        $this->memcache = Yii::$app->get($this->memcache);

        if ($this->keyPrefix === null) {
            $this->keyPrefix = substr(md5(Yii::$app->id), 0, 5);
        }
        parent::init();
    }

    /**
     * Returns a value indicating whether to use custom session storage.
     * This method overrides the parent implementation and always returns true.
     * @return boolean whether to use custom storage.
     */
    public function getUseCustomStorage()
    {
        return true;
    }

    /**
     * Session read handler.
     * Do not call this method directly.
     * @param string $id session ID
     * @return string the session data
     */
    public function readSession($id)
    {
        $data = $this->memcache->get($this->calculateKey($id));
        return $data === false || $data === null ? '' : $data;
    }

    /**
     * Session write handler.
     * Do not call this method directly.
     * @param string $id session ID
     * @param string $data session data
     * @return boolean whether session write is successful
     */
    public function writeSession($id, $data)
    {
        return (bool)$this->memcache->set($this->calculateKey($id), $data, $this->getTimeout());
    }

    /**
     * Session destroy handler.
     * Do not call this method directly.
     * @param string $id session ID
     * @return boolean whether session is destroyed successfully
     */
    public function destroySession($id)
    {
        return $this->memcache->delete($this->calculateKey($id));
    }

    /**
     * Generates a unique key used for storing session data in cache.
     * @param string $id session variable name
     * @return string a safe cache key associated with the session variable name
     */
    protected function calculateKey($id)
    {
        return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
    }
}

Hi, is this question confirmed?

No. Not able to reproduce it still...

If you like, I can provide as much support as possible as long as you need it.

_This problem is dedicated to the development of our server can be reproduced, this is a Tencent cloud server, I trust you, if you are convenient, you can connect the terminal. You can leave an email address, I sent you sensitive information._

@xiaoxing598 I'm not involved in this problem yet, but if someone from core team will ask for a credentials to access to your server, please, verify person identity first. All core team members have a personal PGP signature and the fingerprint is published on GitHub profile of each core team member.

Ask to sign some message with that key and use gpg --verify message.txt to check the key fingerprint. Thank you

@samdark Is there any other place to check your public key file?

$ gpg --keyserver hkp://subkeys.pgp.net --search-keys 6A325E4A
gpg: searching for "6A325E4A" from hkp server subkeys.pgp.net
gpg: keyserver timed out
gpg: keyserver search failed: Keyserver error

Just send me an email with details. Can't promise to check it fast though... traveling currently.

@samdark Mail([email protected]) has been sent, please pay attention to check.

Seems like you need to upgrade memcached.

This bug was openned with PHP.net about it:
https://bugs.php.net/bug.php?id=71187

They identified that the issue was with the memcached code and they opened this pull request and closed the bug report.
https://github.com/php-memcached-dev/php-memcached/pull/164

The pull request was merged on January 20th so presumably this is available since version 3.0.0 where they mention "Fixes crash with session_regenerate_id (work-around for PHP bug)".
https://github.com/php-memcached-dev/php-memcached/releases/tag/v3.0.0

Seems we can't solve it within Yii so I'm closing the issue.

I just stepped into this problem with php7.1+memcached from ondrej ppa and yii1.
Weird version info about php-memcached extension:
Package: php-memcached
Versions:
3.0.3+2.2.0-1+deb.sury.org~trusty+1

Solved by adding memcached.sess_binary_protocol=0 to /etc/php/7.1/mods-available/memcached.ini

I solved this issue by changing the usergroup permission to www-data. As following for ubuntu:
sudo chown -R www-data:www-data /root_folder/

Was this page helpful?
0 / 5 - 0 ratings