PHP registered shutdown function not called sometimes
<p>I want to do a longtime job and i can't modify max execution time, so i take a solution: <strong>when the script is going to shutdown, the script will send a http request to itself, so the script can continue to run.</strong>
This is my test script t.php</p>

<pre><code>&lt;?php 
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));

function post_request_async($url, ARRAY $data) {
    // Convert the data array into URL Parameters like a=b&amp;foo=bar etc.
    $data = http_build_query($data);
    // parse the given URL
    $url = parse_url($url);
    $host = $url['host'];
    $path = $url['path'];
    $port = isset($url['port']) ? $url['port'] : "80";
    $fp = fsockopen($host, $port, $errno, $errstr, 5);
    if ($fp){
        $req = "";
        $req.="POST $path HTTP/1.0\r\n";
        $req.="Host: $host\r\n";
        $req.="Content-type: application/x-www-form-urlencoded\r\n";
        $req.="Content-length: ". strlen($data) ."\r\n";
        $req.="Connection: close\r\n\r\n";
        $req.= $data;
        fputs($fp, $req);
        fclose($fp);
    }
}

set_time_limit(10);
register_shutdown_function("shutdown_func");

file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." start --&gt;".PHP_EOL, FILE_APPEND);

function shutdown_func(){
    file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." shutdown_func --&gt;".PHP_EOL, FILE_APPEND);
    post_request_async("http://127.0.0.1/t.php", array());
    exit();
}

for(;;) {
    sleep(2);
}
?&gt;
</code></pre>

<p>But the result is strange:
zz.txt</p>

<pre><code>2012-05-16 10:11:42 start --&gt;
2012-05-16 10:11:54 shutdown_func --&gt;
2012-05-16 10:11:54 start --&gt;
2012-05-16 10:12:04 shutdown_func --&gt;
2012-05-16 10:12:04 start --&gt;
2012-05-16 10:12:14 shutdown_func --&gt;
2012-05-16 10:12:14 start --&gt;
2012-05-16 10:12:26 shutdown_func --&gt;
2012-05-16 10:12:26 start --&gt;
2012-05-16 10:12:38 shutdown_func --&gt;
2012-05-16 10:12:38 start --&gt;
2012-05-16 10:12:48 shutdown_func --&gt;
2012-05-16 10:12:48 start --&gt;
2012-05-16 10:12:58 shutdown_func --&gt;
2012-05-16 10:12:58 start --&gt;
2012-05-16 10:13:08 shutdown_func --&gt;
2012-05-16 10:13:08 start --&gt;
2012-05-16 10:13:18 shutdown_func --&gt;
2012-05-16 10:13:18 start --&gt;
2012-05-16 10:13:28 shutdown_func --&gt;
2012-05-16 10:13:28 start --&gt;
2012-05-16 10:13:40 shutdown_func --&gt;
2012-05-16 10:13:40 start --&gt;
2012-05-16 10:13:50 shutdown_func --&gt;
2012-05-16 10:13:50 start --&gt;
2012-05-16 10:14:02 shutdown_func --&gt;
2012-05-16 10:14:02 start --&gt;
2012-05-16 10:14:14 shutdown_func --&gt;
2012-05-16 10:14:14 start --&gt;
2012-05-16 10:14:26 shutdown_func --&gt;
2012-05-16 10:14:26 start --&gt;
2012-05-16 10:14:36 shutdown_func --&gt;
2012-05-16 10:14:36 start --&gt;
2012-05-16 10:14:48 shutdown_func --&gt;
2012-05-16 10:14:48 start --&gt;
2012-05-16 10:14:58 shutdown_func --&gt;
2012-05-16 10:14:58 start --&gt;
2012-05-16 10:15:08 shutdown_func --&gt;
2012-05-16 10:15:08 start --&gt;
2012-05-16 10:15:18 shutdown_func --&gt;
2012-05-16 10:15:18 start --&gt;
</code></pre>

<p>php_errors.log</p>

<pre><code>[16-May-2012 02:11:54 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:04 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:14 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:26 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:38 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:48 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:58 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:08 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:18 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:28 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:40 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:50 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:02 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:14 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:26 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:36 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:48 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:58 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:08 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:18 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error:  Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 33
</code></pre>

<p>Looks like the shutdown function get called for about 22 times and suddenly didn't get called. </p>

<p>My php version is 5.3.9 and server is IIS 7.5
Really need some help!
Thanks very much!</p>

<p><strong>-----------------------Update 1--------------------------------------------</strong></p>

<p>After some digging, I found a test script in php source. It looks like registered shutdown function could timeout.</p>

<pre><code>&lt;?php
set_time_limit(1);
register_shutdown_function("plop");

function plop() {
    $ts = time();
    while(true) {
        if ((time()-$ts) &gt; 2) {
            echo "Failed!";
            break;
        }
    }
}
plop();
?&gt;
</code></pre>

<p>The result is:</p>

<pre><code>Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9

Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9
</code></pre>
