php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15209 Under Apache, register_shutdown_function() broke between 4.0.x to 4.1.x
Submitted: 2002-01-24 13:01 UTC Modified: 2003-02-24 02:53 UTC
Votes:17
Avg. Score:4.8 ± 0.5
Reproduced:15 of 16 (93.8%)
Same Version:10 (66.7%)
Same OS:8 (53.3%)
From: priebe at mi-corporation dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.1.x-4.3.0 OS: RH Linux 7.3
Private report: No CVE-ID: None
 [2002-01-24 13:01 UTC] priebe at mi-corporation dot com
Under Apache 1.3.20 and PHP 4.1.1, register_shutdown_function() will run the specified function, but it will not close the connection to the client.

I have isolated the problem to sapi/apache/sapi_apache.c.  In apache_php_module_main(), php_request_shutdown() is called.  When called from this location, the connection is not shut down.  However, if you comment this call out (along with the AP(in_request) = 0), it will be called from php_apache_request_shutdown() instead.  When called from this function, php_request_shutdown() operates as expected.

I do not understand enough about why the changes were made from 4.0.x to 4.1.x.  I also have not fully tested this change to see if there are undesirable side effects.  I was hoping that someone more familiar with PHP internals would look at it and have an "A-HA" moment.

Here is a patch (hope this survives the cut-and-paste):

--- php-4.1.1/sapi/apache/sapi_apache.c	Sat Aug  4 21:42:45 2001
+++ ../php-4.1.1-changed/php-4.1.1/sapi/apache/sapi_apache.c	Thu Jan 24 12:08:40 2002
@@ -89,13 +89,13 @@
 
 		(void) php_execute_script(&file_handle TSRMLS_CC);
 	}
-
+/*
 	AP(in_request) = 0;
 	
 	zend_try {
 		php_request_shutdown(NULL);
 	} zend_end_try();
-	
+*/	
 	return (OK);
 }
 /* }}} */

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-03 09:38 UTC] mfischer@php.net
Can't be critical, no one else reported this yet.

Please test with latest Apache/PHP and see if this problem still exists (regardless of the source in sapi_apache.c)
 [2002-06-03 09:46 UTC] priebe at mi-corporation dot com
IIRC, somebody from the PHP team set the "Critical" flag after confirming its existence.  I've come to the conclusion that register_shutdown_function() is one of the least-exercised portions of the PHP core, based on the fact that the docs and online comments have never mentioned the fact that it never worked under anything but Apache+*nix.

Seems to me that the nature of the bug _is_ critical, since any application that depends on register_shutdown_function() will fail horribly if the call does not work.

At any rate, we will test.  Thanks.
 [2002-06-03 15:59 UTC] jtate@php.net
This bug still exists in 4.2.1 version.  If it's reproduced 6/6 times on different platforms with an average score of 4.9, it's pretty important if not critical.

Here's a sample script:
<?PHP
function foo () 
{
  sleep (10);
  print "foo<BR>\n";
}

print "pre shutdown<BR>\n";
register_shutdown_function (foo);
print "shutting down<BR>\n";

?>

This should return immediately printing
pre shutdown
shutting down

but instead waits ten seconds then prints
pre shutdown
shutting down
foo

 [2002-06-07 11:38 UTC] zeev@php.net
This should be discussed.  Generally, it's not obvious that shutdown functions should be executed after the connection shuts down.  It may very well be used to print out information, which obviously cannot be done if it's done after the user disconnects.

I believe that getting this behavior to work is only possible under Apache.  With most (all?) other server APIs, it's only possible to do things while the user is still connected.  So, if we want consistency across web server, the existing behavior is the desired one.
 [2002-06-07 11:48 UTC] jtate@php.net
Then we need two functions.  One for the case where we don't want to close the connection, and one for offline processing after closing the connection.  Is there some other mechanism for offline processing?  If not do you have any ideas on how one might implement such an extension?
 [2002-06-11 12:38 UTC] sts at mail dot xubion dot hu
> Generally, it's not obvious that shutdown
>functions should be executed after the connection shuts
>down.
It's not obvius, but worked (and documented?) this way 
before, and some programs used this feature exstensively 
(such as ours) And now we have to say to our customers 
that they shouldn't upgrade from 4.0.6 if they want to use 
our system. I think there are other people who have the 
same situation.
 [2002-06-27 11:56 UTC] cam at wecreate dot ca
I agree that there should be a function that can run things after the script has finished with it's output portion. I can think of many examples where this is desirable.

For example, if I wanted to get a list of ads to display on a website, i could run the DB queries that do the logging after the user is happily on thier way to other things. This would yeild a significant *PERCEIVED* speed increase for the user.

If it only works on Apache/*NIX, I think that's fine. More incentive to switch to this platform. There are other functions and features that are specific to this platform too.

I JUST found this function, and I've got a few very good uses for it (the case where the connection is closed BEFORE the function runs). Plus maintaining backwards compatability would be nice :)
 [2002-08-19 14:01 UTC] spearson8872 at aol dot com
I am using AIX 4.3.3, Apache 1.3.26, mod_perl 1.26 and PHP 4.2.2 This problem is one of 2 that are causing compilation failures for me.

The other is that the LARGE_FILE definition (in mod_perl's ap_config_auto.h) is causing "conflicting type" errors for mmap64 when compiling sapi_apache.c (I will raise a seperate bug report for this second problem)
 [2002-10-01 06:04 UTC] yohgaki@php.net
Dup of #14251
 [2002-10-02 09:35 UTC] priebe at mi-corporation dot com
Is this really a dupe of 14251?  Maybe they involve some of the same code, but these are really two different issues.  14251 involves the fact that the shutdown function code gets a different working directory.  15209 concerns whether the shutdown function runs before or after the HTTP connection is closed.
 [2002-10-02 10:33 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip


 [2002-10-04 07:44 UTC] priebe at mi-corporation dot com
I grabbed a snapshot today (php4-200210040300).  I jtate's script, and still got the erroneous (IMO) behavior.  My connection to the server did not close until the shutdown function completed.  

Looking at the suspect section of sapi_apache.c, I do not see any changes.  Not that I expected to see my patch verbatim, but I would expect something in that vicinity to change.
 [2002-10-12 10:21 UTC] sniper@php.net
Marking as critical since this worked at some point.
And there was also posting by jtate@php.net with a possible
fix on php-dev@lists.php.net


 [2002-12-11 08:30 UTC] andrei@php.net
Changing to 'Verified', since there is some disagreement on how this function should operate.
 [2002-12-12 10:06 UTC] sts at mail dot xubion dot hu
I copy my mail sent to the dev list here too, to make it more noticable:

I know there was some hot discussion about this topic but I really need to
get
this bug fixed. Even I'll make a patch with my zero knowledge of c if no one
would like to make it, but please try to find a reasonable sollution that
fits (almost) everyone's need.

I thought of one. I think a new function with the name
register_apache_shutdown_function (or somethink like this) might be good.
It's name would say that it only works in apache, it could be documented
that
it's the *only* function that closes the connection before the registered
functions are handled. Or maybe a parameter could tell if the connection
should be closed before the first registered function is started.

I'll hope there's some way to solve this problem, because it's not easy to
tell every customer to use our patch (or use 4.0.6) before they are staring
to use our programs that rely on this forgotten feature.
Thanks,
Arpi
 [2002-12-12 11:37 UTC] jtate@php.net
I gave up on my patch.  Too much work, not enought time, and ultimately, I couldn't find a place in PHP land where PHP was still running and Apache had closed the connection to put my hooks in.  There may be a way to tell Apache to close the connection through SAPI, but I am not aware of it.  (I'm not an apache hacker).  Someone posted a way to fork a process to the background, but it isn't a PHP land solution.

From Carsten Gehling:
Maybe this is what you need?

http://www.naken.cc/mikehup.php

I use this on a CMS site, where the users upload imagefiles with ftp. After
that, they use a php webinterface to start an importscript (written in
Perl). By doing this command in php:

system("/usr/local/bin/mikehup /usr/bin/perl
/www/servers/netlag/cronscripts/import_billede.pl &");

The importscript is started and executes in the background while the
php-script finishes execution.

Hope that helps

- Carsten
 [2002-12-23 11:25 UTC] brianm-php at dealnews dot com
The following script will cause IE to stop loading the page when zlib.output_compression is used.  This was not true before the changes to register_shutdown_function as the output was not sent.

You can see a test at http://dealnews.com/zlibshutdown.php.  Mozilla gracefully handles the mixed output by truncating the non-compressed data.

================================
<?php

register_shutdown_function("shutdown_test");

?>

<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffff" leftmargin="8" topmargin="8" marginheight="8" marginwidth="8">
This is in the HTML body.

<?php

function shutdown_test()
{
    echo "This is the shutdown function.";
}

?>

</body>
</html>
 [2002-12-27 10:52 UTC] jtate@php.net
I created a patch to add this functionality back under mod_php4 systems.  This patch was posted to the php-dev list yesterday.  Check out the list archives.  An improved patch will be posted later today.
 [2002-12-30 11:26 UTC] jtate@php.net
The functionality for register_shutdown_function under 4.0.x will be replaced with a function named apache_register_shutdown_function.  This should be available under 4.3.1 and later versions of PHP.  I will close this bug when the patch has been committed.
 [2003-02-24 02:53 UTC] zeev@php.net
Closing as bogus.
If we ever support this kind of behavior, it won't be a part of register_shutdown_function().
If there was a working patch for apache_register_shutdown_function(), I missed it - please resend.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 17:01:33 2024 UTC