-
Notifications
You must be signed in to change notification settings - Fork 8.1k
ext/session: Fix GH-16027 session close() not called when write fails #22622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jorgsowa
wants to merge
1
commit into
php:PHP-8.4
Choose a base branch
from
jorgsowa:fix-gh16027-session-close-on-exception-8.4
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --TEST-- | ||
| GH-16027 (Using SessionHandler doesn't always close session file) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include('skipif.inc'); ?> | ||
| --FILE-- | ||
| <?php | ||
| $save_path = __DIR__ . '/gh16027'; | ||
| @mkdir($save_path); | ||
| session_save_path($save_path); | ||
|
|
||
| $id = str_repeat('a1', 16); | ||
| session_id($id); | ||
| session_set_save_handler(new SessionHandler(), true); | ||
| session_start(); | ||
|
|
||
| try { | ||
| $_SESSION['test'] = function () {}; | ||
| session_write_close(); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| $file = "$save_path/sess_$id"; | ||
| echo "session file exists: "; | ||
| var_dump(file_exists($file)); | ||
|
|
||
| /* A leaked lock from close() never being called would make this fail immediately. */ | ||
| $fp = fopen($file, 'r+'); | ||
| echo "lock acquired after failed write: "; | ||
| var_dump(flock($fp, LOCK_EX | LOCK_NB)); | ||
| fclose($fp); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $save_path = __DIR__ . '/gh16027'; | ||
| $id = str_repeat('a1', 16); | ||
| @unlink("$save_path/sess_$id"); | ||
| @rmdir($save_path); | ||
| ?> | ||
| --EXPECT-- | ||
| Exception: Serialization of 'Closure' is not allowed | ||
| session file exists: bool(true) | ||
| lock acquired after failed write: bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| GH-16027 (close() throwing while a write() exception is pending chains it as previous) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include('skipif.inc'); ?> | ||
| --FILE-- | ||
| <?php | ||
| class MySessionHandler implements SessionHandlerInterface { | ||
| function open($save_path, $session_name): bool { | ||
| return true; | ||
| } | ||
|
|
||
| function close(): bool { | ||
| echo "close: goodbye cruel world\n"; | ||
| throw new Exception('close failed'); | ||
| } | ||
|
|
||
| function read($id): string|false { | ||
| return ''; | ||
| } | ||
|
|
||
| function write($id, $session_data): bool { | ||
| echo "write: goodbye cruel world\n"; | ||
| throw new Exception('write failed'); | ||
| } | ||
|
|
||
| function destroy($id): bool { | ||
| return true; | ||
| } | ||
|
|
||
| function gc($maxlifetime): int { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| session_set_save_handler(new MySessionHandler()); | ||
| session_start(); | ||
|
|
||
| try { | ||
| session_write_close(); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| $previous = $e->getPrevious(); | ||
| echo 'previous: '; | ||
| var_dump($previous ? $previous->getMessage() : null); | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| write: goodbye cruel world | ||
| close: goodbye cruel world | ||
| Exception: close failed | ||
| previous: string(12) "write failed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --TEST-- | ||
| GH-16027 (exit() in close() while a write() exception is pending terminates cleanly) | ||
| --EXTENSIONS-- | ||
| session | ||
| --SKIPIF-- | ||
| <?php include('skipif.inc'); ?> | ||
| --FILE-- | ||
| <?php | ||
| class MySessionHandler implements SessionHandlerInterface { | ||
| function open($save_path, $session_name): bool { | ||
| return true; | ||
| } | ||
|
|
||
| function close(): bool { | ||
| echo "close: goodbye cruel world\n"; | ||
| exit("bye\n"); | ||
| } | ||
|
|
||
| function read($id): string|false { | ||
| return ''; | ||
| } | ||
|
|
||
| function write($id, $session_data): bool { | ||
| echo "write: goodbye cruel world\n"; | ||
| throw new Exception('write failed'); | ||
| } | ||
|
|
||
| function destroy($id): bool { | ||
| return true; | ||
| } | ||
|
|
||
| function gc($maxlifetime): int { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| session_set_save_handler(new MySessionHandler()); | ||
| session_start(); | ||
|
|
||
| try { | ||
| session_write_close(); | ||
| echo "unreachable\n"; | ||
| } catch (\Throwable $e) { | ||
| echo "unreachable catch: ", $e->getMessage(), "\n"; | ||
| } | ||
| echo "unreachable after\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| write: goodbye cruel world | ||
| close: goodbye cruel world | ||
| bye |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dance is never a good idea.