php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77436 Missing note about OS specific behavior
Submitted: 2019-01-10 00:34 UTC Modified: 2019-08-13 14:16 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: kbush at rvhsolutions dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: 7.2.13 OS: win & nix
Private report: No CVE-ID: None
 [2019-01-10 00:34 UTC] kbush at rvhsolutions dot com
Description:
------------
---
From manual page: https://php.net/function.sleep
---

The behavior of sleep differs between windows and nix OS's but this behavior is not documented.

Specifically, sleep counts towards max_execution_time in windows but not in nix. 
Something is mentioned on the set_time_limit() page but it is not abundantly clear that sleep() time will be ignored.

Please add a note to the sleep page indicating that the behavior differs between operating systems. Also, it wouldn't hurt to add sleep() to the note block on set_time_limit after the system() reference.

Attached some code if you wish to verify this behavior. In windows you get 10 lines, in nix you get 30. 
We ran into this issue after migrating some old scripts from windows to nix and occasionally they seemed to run forever.

PS, if this could be fixed so that sleep does count toward execution time on nix, that would be best. Because of this difference a script with a 30 second timeout could run for months if stuck in a loop with sleep in it. That completely invalidates the point of a max execution time...

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

set_time_limit(10); $i = 1;
while($i<=30){
    echo "1 second has passed and the max_execution_time is: ".ini_get("max_execution_time")." - Run $i times so far...\r\n<br>";
    sleep(1); $i++;
}

Expected result:
----------------
1 second has passed and the max_execution_time is: 10 - Run 1 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 2 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 3 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 4 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 5 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 6 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 7 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 8 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 9 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 10 times so far... 

( ! ) Fatal error: Maximum execution time of 10 seconds exceeded in test.php on line 6

Actual result:
--------------
1 second has passed and the max_execution_time is: 10 - Run 1 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 2 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 3 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 4 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 5 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 6 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 7 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 8 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 9 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 10 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 11 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 12 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 13 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 14 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 15 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 16 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 17 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 18 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 19 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 20 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 21 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 22 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 23 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 24 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 25 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 26 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 27 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 28 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 29 times so far... 
1 second has passed and the max_execution_time is: 10 - Run 30 times so far... 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-11 18:47 UTC] mucahitcucen at gmail dot com
Hello,

This is not about sleep function. set_time_limit function causes this conflict.

In the documentation page (http://www.php.net/manual/en/function.set-time-limit.php), you can see a note about difference between unix and windows.

The note which is i refer:
"The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real."
 [2019-08-13 12:29 UTC] ferkert at navigate dot de
problem still exists PHP 7.2.19-0ubuntu0.18.04.1 (cli)


<?php
ini_set('max_execution_time', 2);
var_dump(ini_get('max_execution_time'));

$ois = 0;
while (true) {
    var_dump($ois++);
    sleep(1);
}
 [2019-08-13 14:16 UTC] kbush at rvhsolutions dot com
To ferkert at navigate dot de:
This ticket was a request to update the documentation, not a ticket about the behavior existing. You may want to create a new ticket if you feel something is not working correctly.
---
To mucahitcucen at gmail dot com:
I was originally requesting that a note be added to the "sleep" documentation page about the difference in behavior. 

I think a note (on the sleep documentation) would be helpful to raise awareness that this behavior exists and hopefully prevent people from relying on sleep + max_execution_time to limit their scripts run time.  Doubly so since the note on set_time_limit doesn't actually mention "sleep" as a system call.

Suggestion to add to sleep documentation:
"NOTE: sleep() is a system call and does not count towards max_execution_time in most cases. Please see the set_time_limit() function for more details."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 21:01:27 2024 UTC