Implement fiber context switch blocking? #7134
Closed
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.
Edit: I played around with this code and I am not sure if this is a good approach...
This PR introduces a new variable
EG(fiber_switch_enabled)that controls if fiber context switches are allowed. It is set totrueby default and can be switched tofalsein code sections that must not be interrupted (due to not being reentrant). I disabled context switches duringzend_gc_collect_cycles()because a fiber context switch during a destructor call can prevent garbage collection from working:The GC issue is only relevant in cycles collection, however userland code never knows if a destructor is called as part of a GC run or simply due to refcount reaching 0 (in this case switching fibers could be allowed). I decided to disable fiber context switches during any object destructor / free call to keep things simple and consistent (doing anyting that involves fibers in destructors is a horrible idea in general...).
Due to the nature of fibers (they are basically invisible to users of an API) even a simple internal function call like
zval_ptr_dtor()may result in a fiber context switch. This PR prevents this from happening in all GC-related areas. At the same time it can be reused by all internals code and extensions to keep fiber switching in check (signal handling /pcntlwill probably also need this).