php / php/php-src

evade `exit_status` overwrite by `exit()`

Open
#15,796 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Engine Feature Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

as you know there could be multiple exit() occurrences with the help of register_shutdown_function

my case exactly is a shutdown handler that executes once and does exit(), but original exit_status is lost because exit overwrites it. i would like to keep the original exit code. here's some code sample for quick showcase:

register_shutdown_function(function() {
  done(true);# bad termination
});
work();
done();# good termination
###
function work(): void
{
  throw new \Exception('test');# same as exit(255);
  #exit(1001);
}
function done(bool $bad=false): void
{
  static $DID=0;
  if ($DID) {
    return;
  }
  $DID++;
  # ...
  # cleanup
  # ...
  exit($bad ? 1 : 0);
  #exit(exit_status() ?: ($bad ? 1 : 0));# this may save the original
}

to keep behavior the same, i propose to add exit_status() getter similar to error_reporting() that only gets a exit_status value. what you think?

to test those, ive added it right after error_reporting
https://github.com/php/php-src/blob/e358634cdce6a7505b7d422c23ec205a483ad2fc/Zend/zend_builtin_functions.c#L457

ZEND_FUNCTION(exit_status) // {{{
{
	RETURN_LONG(EG(exit_status));
}
// }}}

but also have to add these in https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions_arginfo.h

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_exit_status, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
// ...
ZEND_FUNCTION(exit_status);
// ...
	ZEND_FE(exit_status, arginfo_exit_status)

it has some php file (https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions.stub.php) to generate, i didnt get how to use it

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 with Zend/zend_builtin_functions.c and the existing error_reporting entry point, then inspect Zend/zend_builtin_functions.stub.php and the generated Zend/zend_builtin_functions_arginfo.h. Determine the supported generation and testing workflow for a new exit_status builtin; done means the proposal is resolved and the original exit status can be retrieved without changing existing exit behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.