php / php/php-src

set_exception_handler not restored if calling exit within itself

Open
#18,619 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

What was fixed https://github.com/php/php-src/commit/3301d9602a2307007858c299c41795d3d95e3843 https://github.com/php/php-src/issues/13446#issuecomment-1991549384 bc it was broken from 8.3.0-8.3.4 is still broken if the exception handler itself calls exit.

https://3v4l.org/HkWfh
without exit: https://3v4l.org/FTWQf

The following code:

<?php

declare(strict_types=1);

function foo($e) {
    var_dump( set_exception_handler('foo') );
    restore_exception_handler();
    echo "---" . PHP_EOL;
    exit;
}

set_exception_handler('foo');

register_shutdown_function(function () {
    var_dump( set_exception_handler(null) );
});

strlen(null);

Resulted in this output:

NULL
---
NULL

But I expected this output instead:

NULL
---
string(3) "foo"

This matters, because since PHP 8.3, the exit code of the exit() call in exception handler is used as exit code of PHP

@iluuu1994

PHP Version
PHP 8.3.0+
Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the provided PHP script and compare the exit and non-exit cases from the linked 3v4l examples. Trace the interaction between set_exception_handler(), restore_exception_handler(), register_shutdown_function(), and exit in the PHP runtime. Done means the shutdown callback observes the restored "foo" handler when exit is called inside the exception handler.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.