Skip to content

Runtime: set_exception_handler() / restore_exception_handler() (ext/standard parity) #3146

Description

@PurHur

Category

stdlib / runtime

Problem

set_exception_handler() and restore_exception_handler() are not registered. Uncaught exceptions always reach the VM top-level handler; Zend allows a user callback (and a stack of previous handlers) before process exit.

Blocked by: builtin Exception / Throwable classes (#195) — handler receives Throwable objects.

php-src reference

  • ext/standard/basic_functions.cPHP_FUNCTION(set_exception_handler), restore_exception_handler
  • Zend/zend_exceptions.czend_exception_get_default(), uncaught handler chain, zend_throw_exception_hook

Repro (today)

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "var_dump(function_exists(\"set_exception_handler\"));"'
# bool(false)
<?php
set_exception_handler(function (Throwable $e): void {
    echo 'handled:', $e->getMessage(), "\n";
});
throw new Exception('x');

Zend PHP: handled:x (no fatal). This compiler: undefined function and/or non-existing class Exception.

Stack behavior (done-when detail)

set_exception_handler(fn() => print "a\n");
set_exception_handler(fn() => print "b\n");
restore_exception_handler();
throw new Exception('z');
// Zend: prints b, then on second restore a, then default handler

Scope (this repo)

Piece Path
Builtin ext/standard/set_exception_handler.php, restore_exception_handler.php
Registry ext/standard/Module.php
VM lib/VM/Context.php — handler stack; lib/VM.php uncaught path calls top handler
Prerequisite #195 — register Exception, Throwable, Error
Web lib/Web/ — optional: map uncaught to HTTP 500 after handler (#152)
Tests test/compliance/cases/stdlib/exception_handler.phpt

Done when

  • Language: throw expressions and Exception object propagation #195 landed (or stub Exception sufficient for repro)
  • First repro prints handled:x on VM without process fatal
  • restore_exception_handler() restores previous callback; nested set_* matches Zend order
  • Handler returning false falls through to previous/default (Zend)
  • ./script/ci-fast.sh --filter exception_handler green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter exception_handler'

Related

Labels

stdlib, implementation-ready, phase-4:stdlib, area:vm

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions