php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65596 usleep prevents script timeout
Submitted: 2013-08-30 15:47 UTC Modified: 2013-08-30 19:12 UTC
From: ukrtelecom at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4Git-2013-08-30 (snap) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ukrtelecom at gmail dot com
New email:
PHP Version: OS:

 

 [2013-08-30 15:47 UTC] ukrtelecom at gmail dot com
Description:
------------
Usleeped time does not count in total timeout time.

Commenting out line 11 "usleep(10)" in the test script results with expected 
Fatal timeout in 2 seconds.
Enabling line 11 "usleep(10)" makes script exit by condition in 50 seconds.
Increasing waiting time to 100 seconds in line 2 results with timeout Fatal 
error in 60-70 sec.


PHP 5.4.20-dev (cli) (built: Aug 30 2013 14:18:16) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

./configure --disable-libxml --disable-xml --disable-simplexml --disable-
xmlreader --disable-xmlwriter --disable-dom --without-pear

OS Ubuntu 12.04 LTS 
Linux precise64 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 
x86_64 x86_64 x86_64 GNU/Linux

Test script:
---------------
<?php
$wait = 50;

echo ini_get('max_execution_time');

set_time_limit(2);

$ts = time();
while(true) {
	// usleep for 10 microseconds
	usleep(10);
	if ((time()- $ts) > $wait) {
		echo time() -$ts;
		echo "\n\nFailed! " . ini_get('max_execution_time') . "\n";
		break;
	}
}



Expected result:
----------------
0
Fatal error: Maximum execution time of 2 seconds exceeded in /home/vagrant/t.php 
on line 12

Actual result:
--------------
#with wait time 50 sec:
$ date; php t.php ; date
Fri Aug 30 15:40:54 UTC 2013
0
51

Failed! 2
Fri Aug 30 15:41:45 UTC 2013


#with wait time 100 sec:
$ date; php t.php ; date
Fri Aug 30 15:35:45 UTC 2013
0

Fatal error: Maximum execution time of 2 seconds exceeded in /home/vagrant/t.php 
on line 12
Fri Aug 30 15:36:40 UTC 2013


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-30 18:00 UTC] ab@php.net
-Status: Open +Status: Wont fix
 [2013-08-30 18:00 UTC] ab@php.net
This is unsolvable in PHP, sleep is a system call so it will suspend the execution 
. It can be only interrupted by a signal which can't be thrown from PHP because 
the execution is suspended :)
 [2013-08-30 18:26 UTC] anon at anon dot anon
@ab Well of course it's solvable.
1. sleepTime = min(specifiedTime, timeLimit - scriptTime)
2. Do system call.
3. scriptTime += sleepTime

I don't understand why you track script time in such a bizarre fashion that it somehow doesn't contribute to the time limit anyway.
 [2013-08-30 19:12 UTC] johannes@php.net
-Status: Wont fix +Status: Not a bug
 [2013-08-30 19:12 UTC] johannes@php.net
What happens depends on the operating system, in general we tr tomeasure the time spent on cpu, not the time elapsed.

On systems which have setitimer (Linux and other unixes) we use setitimer with ITIMER_PROF, which, in my linux system's man page is documented as

       ITIMER_PROF    decrements both when the process executes and when the 
                      system is executing on behalf  of  the  process. 
 
During sleep nothing is being executed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 14 09:01:31 2024 UTC