php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ospite at studenti dot unina dot it
New email:
PHP Version: OS:

 

 [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:02 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d1f0662e4d587754742891f0a179551d8f36674f
Log: Fixed bug #62725 (Calling exit() in a shutdown function does not return the exit value)
 [2012-08-02 15:03 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d1f0662e4d587754742891f0a179551d8f36674f
Log: Fixed bug #62725 (Calling exit() in a shutdown function does not return the exit value)
 [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
 [2014-10-07 23:23 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=d1f0662e4d587754742891f0a179551d8f36674f
Log: Fixed bug #62725 (Calling exit() in a shutdown function does not return the exit value)
 [2014-10-07 23:34 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=d1f0662e4d587754742891f0a179551d8f36674f
Log: Fixed bug #62725 (Calling exit() in a shutdown function does not return the exit value)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC