php / php/php-src

Running out of memory in custom session handler's `write` function causes "Cannot call session save handler in a recursive manner"

未关闭
#15,529 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Extension: session Status: Verified
主要语言
C
星标
40.4k
派生
8.2k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

The following code:

<?php

ini_set('memory_limit', '16M');

class TestSessionHandler implements SessionHandlerInterface
{
	public function close(): bool
	{
		error_log("Close");
		return true;
	}
	public function destroy(string $id): bool
	{
		error_log("Destroy");
		return true;
	}
	public function gc(int $max_lifetime): int|false
	{
		error_log("GC");
		return 0;
	}
	public function open(string $path, string $name): bool
	{
		error_log("Open");
		return true;
	}
	public function read(string $id): string|false
	{
		error_log("Read");
		return 'a|i:1234;';
	}
	public function write($id, $data): bool
	{
		error_log("About to run out of memory");
		var_dump(strlen(str_repeat('0', 16777216)));
		error_log("Wrote session");
		return true;
	}
}

session_set_save_handler(new TestSessionHandler());
session_start();
error_log("End of code");

Resulted in this output:

Open
Read
End of code
About to run out of memory
PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 16777248 bytes) in /Users/ssigwart/Downloads/sessionsTest.php on line 35

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 16777248 bytes) in /Users/ssigwart/Downloads/sessionsTest.php on line 35
PHP Warning:  PHP Request Shutdown: Cannot call session save handler in a recursive manner in Unknown on line 0

Warning: PHP Request Shutdown: Cannot call session save handler in a recursive manner in Unknown on line 0
PHP Warning:  PHP Request Shutdown: Failed to write session data using user defined save handler. (session.save_path: /usr/local/share/phpsess, handler: TestSessionHandler::updateTimestamp) in Unknown on line 0

Warning: PHP Request Shutdown: Failed to write session data using user defined save handler. (session.save_path: /usr/local/share/phpsess, handler: TestSessionHandler::updateTimestamp) in Unknown on line 0
Close

But I expected this output instead:

Open
Read
End of code
About to run out of memory
PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 16777248 bytes) in /Users/ssigwart/Downloads/sessionsTest.php on line 35

I'd expect the fatal error, but not the recursive session call.

The above simulates the error using a large string, but this happens when a session is large and the custom error handler needs to allocate some more data to write the data.

PHP Version

PHP 8.1.29, PHP 8.3.10

Operating System

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先使用 PHP 8.1.29 或 8.3.10 运行 issue 中的 PHP 复现,并将 shutdown 输出与预期的 fatal error 进行比较。跟踪请求 shutdown 期间自定义 session handler 的 write 调用;当 out-of-memory fatal 仍然存在,且不再出现递归 session-call 和写入失败警告时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。