Return value must be of type string, bool returned

破建站的

问题描述

清空了一下redis数据库 导致session无法使用redis驱动

程序代码

    //seesion配置
    ....
    'type' => 'redis', // or redis or redis_cluster

    'handler' => RedisSessionHandler::class,

    'config' => [
        'file' => [
            'save_path' => runtime_path() . '/sessions',
        ],
        'redis' => [
            'host' => '127.0.0.1',
            'port' => 6379,
            'auth' => '',
            'timeout' => 2,
            'database' => 1,
            'prefix' => env('DB_NAME').'_redis_session_',
        ],
        ...
    ],
    ....

报错信息

TypeError: Workerman\Protocols\Http\Session\RedisSessionHandler::read(): Return value must be of type string, bool returned in /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Protocols/Http/Session/RedisSessionHandler.php:116
Stack trace:
#0 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Protocols/Http/Session.php(170): Workerman\Protocols\Http\Session\RedisSessionHandler->read()
#1 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Protocols/Http/Request.php(306): Workerman\Protocols\Http\Session->__construct()
#2 /www/wwwroot/code/xiangmu/plugin/admin/app/middleware/GlobalInject.php(44): Workerman\Protocols\Http\Request->session()
#3 /www/wwwroot/code/xiangmu/vendor/workerman/webman-framework/src/App.php(341): plugin\admin\app\middleware\GlobalInject->process()
#4 /www/wwwroot/code/xiangmu/vendor/workerman/webman-framework/src/App.php(141): Webman\App::Webman\{closure}()
#5 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Connection/TcpConnection.php(710): Webman\App->onMessage()
#6 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Events/Revolt.php(146): Workerman\Connection\TcpConnection->baseRead()
#7 /www/wwwroot/code/xiangmu/vendor/revolt/event-loop/src/EventLoop/Internal/AbstractDriver.php(587): Workerman\Events\Revolt->Workerman\Events\{closure}()
#8 [internal function]: Revolt\EventLoop\Internal\AbstractDriver->Revolt\EventLoop\Internal\{closure}()
#9 /www/wwwroot/code/xiangmu/vendor/revolt/event-loop/src/EventLoop/Internal/AbstractDriver.php(494): Fiber->resume()
#10 /www/wwwroot/code/xiangmu/vendor/revolt/event-loop/src/EventLoop/Internal/AbstractDriver.php(549): Revolt\EventLoop\Internal\AbstractDriver->invokeCallbacks()
#11 [internal function]: Revolt\EventLoop\Internal\AbstractDriver->Revolt\EventLoop\Internal\{closure}()
#12 /www/wwwroot/code/xiangmu/vendor/revolt/event-loop/src/EventLoop/Internal/AbstractDriver.php(112): Fiber->start()
#13 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Events/Revolt.php(88): Revolt\EventLoop\Internal\AbstractDriver->run()
#14 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Worker.php(1594): Workerman\Events\Revolt->run()
#15 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Worker.php(1394): Workerman\Worker::forkOneWorkerForLinux()
#16 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Worker.php(1368): Workerman\Worker::forkWorkersForLinux()
#17 /www/wwwroot/code/xiangmu/vendor/workerman/workerman/src/Worker.php(572): Workerman\Worker::forkWorkers()
#18 /www/wwwroot/code/xiangmu/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()
#19 /www/wwwroot/code/xiangmu/start.php(4): support\App::run()
#20 {main}

操作系统及workerman/webman等框架组件具体版本

系统环境 Centos7

"workerman/webman-framework": "^1.5.0",
"workerman/workerman": "^5",
"workerman/http-client": "^2",
"webman-tech/laravel-http-client": "^1.2"

881 1 0
1个回答

破建站的
追踪了一下代码
namespace Workerman\Protocols\Http;
namespace Workerman\Protocols\Http;

/**
 * Class Session
 * @package Workerman\Protocols\Http
 */
class Session
{
        public function __construct(string $sessionId)
    {
        static::checkSessionId($sessionId);
        if (static::$handler === null) {
            static::initHandler();
        }
        $this->sessionId = $sessionId;
        //这个read返回了false
        if ($data = static::$handler->read($sessionId)) {
            $this->data = unserialize($data);
        }

        下面这个是具体的执行方法
            /**
     * {@inheritdoc}
     * @param string $sessionId
     * @return string
     * @throws RedisException
     * @throws Throwable
     */
    public function read(string $sessionId): string
    {
        try {
            //在这里返回了false  不应该是先写入session在读取吗
            return $this->redis->get($sessionId);
        } catch (Throwable $e) {
            $msg = strtolower($e->getMessage());
            if ($msg === 'connection lost' || strpos($msg, 'went away')) {
                $this->connect();
                return $this->redis->get($sessionId);
            }
            throw $e;
        }
    }
    }
}
  • walkor 2023-11-13

    v5还没发正式版,你先手动改成 return (string)$this->redis->get($sessionId);

  • 破建站的 2023-11-13

    可以了 这个改包的话 线上也得手动改 可否升级下此处代码

  • walkor 2023-11-13

    短时间内不会发版本,因为有很多提交还没验证,你先手动改吧

年代过于久远,无法发表回答
×
🔝