Skip to content

errorfunc: document the error handler stack - #5253

Merged
lacatoire merged 1 commit into
php:masterfrom
lacatoire:fix/error-handler-stack-semantics
Aug 25, 2026
Merged

errorfunc: document the error handler stack#5253
lacatoire merged 1 commit into
php:masterfrom
lacatoire:fix/error-handler-stack-semantics

Conversation

@lacatoire

@lacatoire lacatoire commented Feb 5, 2026

Copy link
Copy Markdown
Member

set_error_handler() saves the handler that was active until then on an internal stack and installs the new one in its place; restore_error_handler() pops it back, together with the error_levels mask it was registered with. Neither page said so, which invites passing the return value of set_error_handler() back to it to "restore" it — that pushes a further entry instead of removing one, and silently resets error_levels to E_ALL.

Both pages now describe the save/restore mechanism, the null case and the empty-stack case. A caution warns against the re-registration pattern, and an example calls the previous handler from the new one. The seealso <simplelist> are unwrapped from their <para>, matching the exception handler pages.

The exception handler side is left out: it behaves differently since 8.3.5 and those pages already document it.

Sources: ZEND_FUNCTION(set_error_handler) and ZEND_FUNCTION(restore_error_handler) in Zend/zend_builtin_functions.c; user_error_handler and user_error_handlers in Zend/zend_globals.h; shutdown_executor() in Zend/zend_execute_API.c, which releases the stacks at the end of the request.

Fixes: #4883

@lacatoire lacatoire closed this Mar 2, 2026
@lacatoire lacatoire reopened this Aug 21, 2026
@lacatoire
lacatoire force-pushed the fix/error-handler-stack-semantics branch 4 times, most recently from 781a5ac to 5212897 Compare August 25, 2026 20:04
@lacatoire lacatoire changed the title Clarify stack-based behavior of error/exception handlers errorfunc: document the error handler stack Aug 25, 2026
set_error_handler() saves the handler that was active until then on an
internal stack and installs the new one in its place;
restore_error_handler() pops the saved one back, together with the
error_levels mask it was registered with. Neither page said so, which
invites restoring the previous handler by passing the return value of
set_error_handler() back to it. That grows the stack instead of
shrinking it, and resets error_levels to E_ALL.

Adds a caution against that pattern and an example calling the previous
handler from the new one. The seealso simplelists are unwrapped from
their para, matching the exception handler pages.
@lacatoire
lacatoire force-pushed the fix/error-handler-stack-semantics branch from 5212897 to 4e46d36 Compare August 25, 2026 20:09
@lacatoire
lacatoire merged commit cbfc5d5 into php:master Aug 25, 2026
2 checks passed
@lacatoire
lacatoire deleted the fix/error-handler-stack-semantics branch August 25, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarifications on the error/exception handler stacks

1 participant