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 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Extension: session Status: Verified
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、PHP 8.1.29 または 8.3.10 で issue の PHP reproduction を実行し、shutdown output を想定される fatal error と比較します。request shutdown 中の custom session handler の write call を追跡します。out-of-memory fatal が recursive session-call と failed-write warnings なしで残れば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。