php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69428 fgets blocks and consumes CPU on a non-blocking TLS stream without data
Submitted: 2015-04-11 22:56 UTC Modified: 2015-04-14 15:35 UTC
From: nathan dot renniewaldock at gmail dot com Assigned:
Status: Duplicate Package: OpenSSL related
PHP Version: 5.6.7 OS: Ubuntu 12.04
Private report: No CVE-ID: None
 [2015-04-11 22:56 UTC] nathan dot renniewaldock at gmail dot com
Description:
------------
TLS streams are blocking and consuming 100% CPU when there's no data when set to non-blocking. Test script is using a local IRC server so I didn't have to write a server. Removing tls:// and changing the port to 6667 works as expected.

Test script:
---------------
<?php
$context = stream_context_create();
stream_context_set_option($context, "ssl", "verify_peer", false);

$fp = stream_socket_client("tls://localhost:6697", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context) or die();
stream_set_blocking($fp, 0);

while (!feof($fp)) {
        echo microtime(true)." Reading...\n";
        $line = fgets($fp);
        echo microtime(true)." $line\n";

        if (!$fp) {
                usleep(10000);
        }
}
@fclose($fp);
?>

Expected result:
----------------
"Reading..." should repeat every ~10ms and CPU usage should be low


Actual result:
--------------
$ php test.php
1428791498.132599 Reading...
1428791498.171197 :localhost NOTICE AUTH :*** Looking up your hostname...

1428791498.171224 Reading...
1428791498.171237 :localhost NOTICE AUTH :*** Found your hostname (cached)

1428791498.171251 Reading...
# At this point htop shows it using 100% CPU until the server send a blank line to check the connection hasn't broken
1428791508.17128


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-11 23:02 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2015-04-11 23:02 UTC] requinix@php.net
Duplicate of bug #69402
 [2015-04-11 23:32 UTC] nathan dot renniewaldock at gmail dot com
Must've missed that one when searching. But that one is correct, my blank lines are from it returning after timeout is exceeded.
 [2015-04-13 20:01 UTC] schmidt dot a at rogers dot com
I've experienced the same thing on 5.5.23 (this did not occur in 5.5.22).  There were 4 issues fixed in 5.5.23 related to streams and openssl.
 [2015-04-14 15:35 UTC] rdlowrey@php.net
This is now fixed upstream in 5.5 and 5.6 branches. The RC period has already begun for 5.5.24 and 5.6.8 so this fix will not be reflected in releases until 5.5.25 and 5.6.9.

In the meantime you can try with the latest snap builds (after a few hours pass):

http://snaps.php.net/
http://windows.php.net/snapshots/
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu May 23 14:01:34 2019 UTC