Calling exit() in a shutdown function does not return the exit value

Bug #62725 Calling exit() in a shutdown function does not return the exit value
Submitted: 2012-08-02 10:38 UTC Modified: 2012-08-02 15:04 UTC
From: ospite at studenti dot unina dot it Assigned: laruence (profile)
Status: Closed Package: CGI/CLI related
PHP Version: 5.4.5 OS: Debian GNU/Linux
Private report: No CVE-ID: None

 [2012-08-02 10:38 UTC] ospite at studenti dot unina dot it

Description:
------------
Calling exit() in a shutdown function behaves differently between php 5.3 and php 5.4 (I tested with the latest stable release 5.4.5 too).

The behavior I expect is that the value returned to the calling process is the one from the last exit() in a shutdown function, and php 5.3 honors that assumption, while php 5.4 does not.

As a side note, the documentation[1] tells only that calling exit() in a shutdown function blocks executing further shutdown functions but it does not make clear what the returned value will be.

[1] http://www.php.net/register_shutdown_function

Test script:
---------------
<?php

# This script behaves differently between php 5.3 and php 5.4
# Test it with php command line:
# $ php test_exit_in_shutdown_function.php; echo $?

function shutdown()
{
  echo 'Script executed with failure', PHP_EOL;
  exit(1);
}

register_shutdown_function('shutdown');

exit(0);

Expected result:
----------------
The return value is the one from the exit() inside the last shutdown function, this is the php 5.3 behavior:

$ php test_exit_in_shutdown_function.php; echo $?
Script executed with failure
1

Actual result:
--------------
In php 5.4.5 the return value from the exit() inside the shutdown function is ignored:

$ php test_exit_in_shutdown_function.php; echo $?
Script executed with failure
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [2012-08-02 15:04 UTC] laruence@php.net

-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence

 [2012-08-02 15:04 UTC] laruence@php.net

Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

Actually, in 5.3 it's not guarantee the exit code will equal to the last call to 
exit

like:
function shutdown()
{
  echo 'Script executed with failure', PHP_EOL;
  exit(0);
}

register_shutdown_function('shutdown');

exit(1);



$? will be 1.


however I fixed this issue, make 5.4 behavior same as 5.3