Fix Async\runtime_stats() abort when called before the scheduler starts#165
Merged
Conversation
…alized buffer A buffer with capacity 0 — a scheduler queue read before the scheduler has allocated it — holds nothing, but circular_buffer_count() asserted `head < capacity` (0 < 0) and aborted on a debug build. Treat capacity 0 as empty and return 0 before the bounds asserts. Foundational part of the fix for #164: protects every caller of the primitive.
…t safety) The pool capacity was already clamped with `capacity > 0 ? ... : 0` against the pre-scheduler state, but the four circular_buffer_count() reads were not — so runtime_stats() read uninitialized buffers when called before the scheduler started. Clamp the counts the same way, consistent with the capacity read. Part of the fix for #164.
…on test Short-circuit runtime_stats() when ZEND_ASYNC_SCHEDULER is NULL (the scheduler has not launched yet, e.g. a top-level call before the first spawn) and return a zeroed snapshot with the same keys, instead of reading the unallocated queue buffers. Add tests/common/runtime_stats.phpt covering the top-level case (which used to abort the process) and the in-coroutine case. Completes the fix for #164.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #164.
Async\runtime_stats()aborted on a debug build (and returned a bogus result on a release build) when called before the scheduler started — e.g. at the top level of a script, before the firstspawn/await. The four global queue buffers (fiber_context_pool,microtasks,coroutine_queue,resumed_coroutines) are allocated lazily at scheduler launch; before that they are zero-initialized (capacity == 0), andcircular_buffer_count()assertedhead < capacity(0 < 0).Fixed in depth, three layers:
circular_buffer_count()returns 0 for an uninitialized buffer (capacity == 0) before the bounds asserts — protects every caller of the primitive.runtime_stats()clamps the queue counts withcapacity > 0 ? count : 0, matching the capacity clamp already there (the count reads had been left out — that inconsistency was the bug).runtime_stats()short-circuits before the scheduler starts (ZEND_ASYNC_SCHEDULER == NULL), returning a zeroed snapshot with the same keys (staticfiber_pool_min/fiber_stack_sizestill reported).Adds
tests/common/runtime_stats.phpt: at the top level it returns a sane zeroed array (the case that used to abort), and inside a coroutine it reports live values.