-
-
Notifications
You must be signed in to change notification settings - Fork 79
fix(zend_bailout): Fix zend_bailout handling #537 #625
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
Merged
Merged
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
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,147 @@ | ||
| # Bailout Guard | ||
|
|
||
| When PHP triggers a "bailout" (via `exit()`, `die()`, or a fatal error), it uses | ||
| `longjmp` to unwind the stack. This bypasses Rust's normal drop semantics, | ||
| meaning destructors for stack-allocated values won't run. This can lead to | ||
| resource leaks for things like file handles, network connections, or locks. | ||
|
|
||
| ## The Problem | ||
|
|
||
| Consider this code: | ||
|
|
||
| ```rust,ignore | ||
| #[php_function] | ||
| pub fn process_file(callback: ZendCallable) { | ||
| let file = File::open("data.txt").unwrap(); | ||
|
|
||
| // If callback calls exit(), the file handle leaks! | ||
| callback.try_call(vec![]); | ||
|
|
||
| // file.drop() never runs | ||
| } | ||
| ``` | ||
|
|
||
| If the PHP callback triggers `exit()`, the `File` handle is never closed because | ||
| `longjmp` skips Rust's destructor calls. | ||
|
|
||
| ## Solution 1: Using `try_call` | ||
|
|
||
| The simplest solution is to use `try_call` for PHP callbacks. It catches bailouts | ||
| internally and returns normally, allowing Rust destructors to run: | ||
|
|
||
| ```rust,ignore | ||
| #[php_function] | ||
| pub fn process_file(callback: ZendCallable) { | ||
| let file = File::open("data.txt").unwrap(); | ||
|
|
||
| // try_call catches bailout, function returns, file is dropped | ||
| let result = callback.try_call(vec![]); | ||
|
|
||
| if result.is_err() { | ||
| // Bailout occurred, but file will still be closed | ||
| // when this function returns | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Solution 2: Using `BailoutGuard` | ||
|
|
||
| For cases where you need guaranteed cleanup even if bailout occurs directly | ||
| (not through `try_call`), use `BailoutGuard`: | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::prelude::*; | ||
| use std::fs::File; | ||
|
|
||
| #[php_function] | ||
| pub fn process_file(callback: ZendCallable) { | ||
| // Wrap the file handle in BailoutGuard | ||
| let file = BailoutGuard::new(File::open("data.txt").unwrap()); | ||
|
|
||
| // Even if bailout occurs, the file will be closed | ||
| callback.try_call(vec![]); | ||
|
|
||
| // Use the file via Deref | ||
| // file.read_to_string(...); | ||
| } | ||
| ``` | ||
|
|
||
| ### How `BailoutGuard` Works | ||
|
|
||
| 1. **Heap allocation**: The wrapped value is heap-allocated so it survives | ||
| the `longjmp` stack unwinding. | ||
|
|
||
| 2. **Cleanup registration**: A cleanup callback is registered in thread-local | ||
| storage when the guard is created. | ||
|
|
||
| 3. **On normal drop**: The cleanup is cancelled and the value is dropped normally. | ||
|
|
||
| 4. **On bailout**: Before re-triggering the bailout, all registered cleanup | ||
| callbacks are executed, dropping the guarded values. | ||
|
|
||
| ### API | ||
|
|
||
| ```rust,ignore | ||
| // Create a guard | ||
| let guard = BailoutGuard::new(value); | ||
|
|
||
| // Access the value (implements Deref and DerefMut) | ||
| guard.do_something(); | ||
| let inner: &T = &*guard; | ||
| let inner_mut: &mut T = &mut *guard; | ||
|
|
||
| // Explicitly get references | ||
| let inner: &T = guard.get(); | ||
| let inner_mut: &mut T = guard.get_mut(); | ||
|
|
||
| // Extract the value, cancelling cleanup | ||
| let value: T = guard.into_inner(); | ||
| ``` | ||
|
|
||
| ### Performance Note | ||
|
|
||
| `BailoutGuard` incurs a heap allocation. Only use it for values that absolutely | ||
| must be cleaned up, such as: | ||
|
|
||
| - File handles | ||
| - Network connections | ||
| - Database connections | ||
| - Locks and mutexes | ||
| - Other system resources | ||
|
|
||
| For simple values without cleanup requirements, the overhead isn't worth it. | ||
|
|
||
| ## Nested Calls | ||
|
|
||
| `BailoutGuard` works correctly with nested function calls. Guards at all | ||
| nesting levels are cleaned up when bailout occurs: | ||
|
|
||
| ```rust,ignore | ||
| #[php_function] | ||
| pub fn outer_function(callback: ZendCallable) { | ||
| let _outer_resource = BailoutGuard::new(Resource::new()); | ||
|
|
||
| inner_function(&callback); | ||
| } | ||
|
|
||
| fn inner_function(callback: &ZendCallable) { | ||
| let _inner_resource = BailoutGuard::new(Resource::new()); | ||
|
|
||
| // If bailout occurs here, both inner and outer resources are cleaned up | ||
| callback.try_call(vec![]); | ||
| } | ||
| ``` | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Prefer `try_call`**: For most cases, using `try_call` and handling the | ||
| error result is simpler and doesn't require heap allocation. | ||
|
|
||
| 2. **Use `BailoutGuard` for critical resources**: Only wrap values that | ||
| absolutely must be cleaned up (connections, locks, etc.). | ||
|
|
||
| 3. **Don't overuse**: Not every value needs to be wrapped. Simple data | ||
| structures without cleanup requirements don't need `BailoutGuard`. | ||
|
|
||
| 4. **Combine approaches**: Use `try_call` where possible and `BailoutGuard` | ||
| for critical resources that must be cleaned up regardless. |
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.
Uh oh!
There was an error while loading. Please reload this page.