php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27297 set_time_limit() & register_shutdown_function()
Submitted: 2004-02-17 20:00 UTC Modified: 2022-04-07 16:13 UTC
Votes:6
Avg. Score:4.0 ± 1.0
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:3 (60.0%)
From: jason at thinkingman dot org Assigned: ilutov (profile)
Status: Closed Package: *General Issues
PHP Version: * OS: *
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: jason at thinkingman dot org
New email:
PHP Version: OS:

 

 [2004-02-17 20:00 UTC] jason at thinkingman dot org
Description:
------------
Hi

I'd like to propose a modification to the SET_TIME_LIMIT() and/or the REGISTER_SHUTDOWN_FUNCTION() or perhaps, the creation of a related new function.

Currently when a page goes to timeout, all buffers get flushed, and all headers get sent.  In the interest of handling timeouts better; it would be ** GREAT ** if instead of all this flushing and finalization occuring, if one could also choose to not have the current script flush and finalize; but to remain in the script as if no timeout occurred at all.

This way, when a timeout occurs one can (for example) do redirects, or choose to manually flush and clear buffers, allowing for cleaner INCLUDE implimentations, and so on.

I'm thinking something like the following could be implimented:

set_time_limit(300, true);

Where 'true' means; don't end script; just kick event off stating that timeout has occurred; allowing register_shutdown_function() to be called, if defined... and continuing normal operation of script.

void set_time_limit ( int seconds, [bool: event notify only]); default = false.

// code example:

<?
      set_time_limit(300, true);

      define("NORMAL", 0);
      define("ABORTED", 1);
      define("TIMEOUT", 2);

      function onShutdownEvent() {
         switch(connection_status()) {
            case NORMAL:
               // code normal exit.
            break;
            case ABORTED:
               // code aborted exit.
            break;
            case TIMEOUT:
               header("location: http://timeout.php");
               exit;
            break;
          }
      }
      
      register_shutdown_function('onShutdownEvent');

      while(1);
?>

Another example of the TIMEOUT switch might be:

case TIMEOUT:
   while(ob_get_level) ob_end_clean();
   if($dblink) mysql_close($dblink);

   include('./header.html');
   include('error.php');
   include('./footer.html');
   exit;
break;

Thanks,
Jason



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 09:09 UTC] jani@php.net
-Package: Feature/Change Request +Package: *General Issues -Operating System: Win32 +Operating System: * -PHP Version: 4.3.5RC3 +PHP Version: *
 [2011-11-08 08:00 UTC] bedemiralp at gmail dot com
I would like to propose another way to achieve same functionality:
Set_time_limit may throw an exception and we may catch it with a user defined 
exception handler (set_exception_handler)
 [2019-09-06 15:26 UTC] trevor dot white at ge dot com
This would be really useful on the CLI to stop a script running forever, set the execution time then the script timeout and gracefully shutdown.

Currently I have to do this with the Linux timeout command, but it's nasty and means you cannot naively handle timeouts which most other languages can.

timeout --signal=SIGHUP 120s script.php ....

<?php
// Protect from some causes of death
function sig_handler($sig) {
    echo "UNKNOWN, Script Dying: I was killed before the requested work could be completed.  (Execution Time " .
        round((microtime() - $GLOBALS['timer']),2) . " secs)" . PHP_EOL;
    exit (3);
}

// Start Timer
$timer = microtime(true);

// Catch Signals
pcntl_signal(SIGINT,  "sig_handler");
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP,  "sig_handler");
 [2022-04-07 16:13 UTC] ilutov@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ilutov
 [2022-04-07 16:13 UTC] ilutov@php.net
This feature request has been open for a long time with no action. Please send a PR or open a new issue on GitHub.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 14:01:29 2024 UTC