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"

Offen
#15,529 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Extension: session Status: Verified
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die PHP-Reproduktion aus dem Issue mit PHP 8.1.29 oder 8.3.10 auszuführen und die Shutdown-Ausgabe mit dem erwarteten fatalen Fehler zu vergleichen. Verfolge den write-Aufruf des benutzerdefinierten Session-Handlers während des Request-Shutdowns; fertig ist die Untersuchung, wenn der Out-of-Memory-Fatalfehler ohne die rekursive Session-Aufruf-Warnung und die Warnungen über fehlgeschriebene Writes bestehen bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.