|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__.'/_executor.php'; |
| 4 | + |
| 5 | +// Self-contained repro of https://github.com/php/frankenphp/issues/2368 |
| 6 | +if (!class_exists('StrictSessionHandler', false)) { |
| 7 | + abstract class AbstractSessionHandler implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface |
| 8 | + { |
| 9 | + private string $sessionName; |
| 10 | + private string $prefetchId; |
| 11 | + private string $prefetchData; |
| 12 | + private ?string $newSessionId = null; |
| 13 | + |
| 14 | + public function open(string $savePath, string $sessionName): bool |
| 15 | + { |
| 16 | + $this->sessionName = $sessionName; |
| 17 | + return true; |
| 18 | + } |
| 19 | + |
| 20 | + abstract protected function doRead(string $sessionId): string; |
| 21 | + abstract protected function doWrite(string $sessionId, string $data): bool; |
| 22 | + abstract protected function doDestroy(string $sessionId): bool; |
| 23 | + |
| 24 | + public function validateId(string $sessionId): bool |
| 25 | + { |
| 26 | + $this->prefetchData = $this->read($sessionId); |
| 27 | + $this->prefetchId = $sessionId; |
| 28 | + return '' !== $this->prefetchData; |
| 29 | + } |
| 30 | + |
| 31 | + public function read(string $sessionId): string |
| 32 | + { |
| 33 | + if (isset($this->prefetchId)) { |
| 34 | + $prefetchId = $this->prefetchId; |
| 35 | + $prefetchData = $this->prefetchData; |
| 36 | + unset($this->prefetchId, $this->prefetchData); |
| 37 | + if ($prefetchId === $sessionId || '' === $prefetchData) { |
| 38 | + $this->newSessionId = '' === $prefetchData ? $sessionId : null; |
| 39 | + return $prefetchData; |
| 40 | + } |
| 41 | + } |
| 42 | + $data = $this->doRead($sessionId); |
| 43 | + $this->newSessionId = '' === $data ? $sessionId : null; |
| 44 | + return $data; |
| 45 | + } |
| 46 | + |
| 47 | + public function write(string $sessionId, string $data): bool |
| 48 | + { |
| 49 | + $this->newSessionId = null; |
| 50 | + return $this->doWrite($sessionId, $data); |
| 51 | + } |
| 52 | + |
| 53 | + public function destroy(string $sessionId): bool |
| 54 | + { |
| 55 | + return $this->newSessionId === $sessionId || $this->doDestroy($sessionId); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + class StrictSessionHandler extends AbstractSessionHandler |
| 60 | + { |
| 61 | + public function __construct(private SessionHandlerInterface $handler) {} |
| 62 | + |
| 63 | + public function open(string $savePath, string $sessionName): bool |
| 64 | + { |
| 65 | + parent::open($savePath, $sessionName); |
| 66 | + return $this->handler->open($savePath, $sessionName); |
| 67 | + } |
| 68 | + |
| 69 | + public function updateTimestamp(string $sessionId, string $data): bool |
| 70 | + { |
| 71 | + return $this->write($sessionId, $data); |
| 72 | + } |
| 73 | + |
| 74 | + protected function doRead(string $sessionId): string |
| 75 | + { |
| 76 | + return $this->handler->read($sessionId); |
| 77 | + } |
| 78 | + |
| 79 | + protected function doWrite(string $sessionId, string $data): bool |
| 80 | + { |
| 81 | + return $this->handler->write($sessionId, $data); |
| 82 | + } |
| 83 | + |
| 84 | + protected function doDestroy(string $sessionId): bool |
| 85 | + { |
| 86 | + return $this->handler->destroy($sessionId); |
| 87 | + } |
| 88 | + |
| 89 | + public function close(): bool |
| 90 | + { |
| 91 | + return $this->handler->close(); |
| 92 | + } |
| 93 | + |
| 94 | + public function gc(int $maxlifetime): int|false |
| 95 | + { |
| 96 | + return $this->handler->gc($maxlifetime); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +return function () { |
| 102 | + // Pre-flock timer: any request that doesn't get the lock immediately will |
| 103 | + // have this fire while blocked in flock(), so when the lock is finally |
| 104 | + // released the request bails out *inside* StrictSessionHandler::doRead — |
| 105 | + // the path that leaks the fd without the fix from 2c6d2b5. |
| 106 | + set_time_limit(1); |
| 107 | + |
| 108 | + $savePath = sys_get_temp_dir() . '/frankenphp-session-deadlock'; |
| 109 | + @mkdir($savePath); |
| 110 | + ini_set('session.use_strict_mode', '1'); |
| 111 | + ini_set('session.save_path', $savePath); |
| 112 | + session_set_save_handler(new StrictSessionHandler(new SessionHandler()), true); |
| 113 | + session_id('testsession2368'); |
| 114 | + session_start(); |
| 115 | + |
| 116 | + // Reset the timer so the holder (and any waiter that successfully acquires |
| 117 | + // the lock) gets enough headroom to finish. |
| 118 | + set_time_limit(5); |
| 119 | + |
| 120 | + echo "Done.\n"; |
| 121 | + flush(); |
| 122 | + // Hold the lock long enough that a 1s timer expires for any concurrent |
| 123 | + // waiter still blocked in flock(). |
| 124 | + system('sleep 2'); |
| 125 | + echo "ok!\n"; |
| 126 | +}; |
0 commit comments