Skip to content

Instantly share code, notes, and snippets.

@cvuorinen cvuorinen/stream_test.php Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Example code to reproduce SSL socket stream bug, which causes it to hang and use 100% CPU
<?php
ini_set('max_execution_time', 10);
$url = 'ssl://gateway.sandbox.push.apple.com:2195';
$ssl = [
'local_cert' => 'path/to/valid/apns_sandbox.pem',
];
$sock = stream_socket_client(
$url,
$errno,
$errstr,
ini_get('default_socket_timeout'),
STREAM_CLIENT_CONNECT,
stream_context_create(array(
'ssl' => $ssl,
))
);
// this setting makes it use 100% cpu until timeout
// without this it also hangs until timeout, but doesn't use 100% cpu
stream_set_blocking($sock, 0);
$data = null;
if (!feof($sock)) {
$data = fread($sock, 1024);
}
var_dump($data);
@cvuorinen

This comment has been minimized.

Copy link
Owner Author

commented Apr 9, 2015

Expected result:

string(0) ""

Actual result:

Fatal error: Maximum execution time of 10 seconds exceeded in /tmp/stream_test.php on line 27

Call Stack:
    0.0001     233600   1. {main}() /tmp/stream_test.php:0
    0.4907     236528   2. fread() /tmp/stream_test.php:27

So far, only been able to reproduce with PHP 5.6.7 on Ubuntu 14.04. Works fine on CentOS with PHP 5.4, 5.5 and 5.6.

@rdlowrey

This comment has been minimized.

Copy link

commented Apr 14, 2015

I'm working on this today. Will update the actual bug report when fixed.

@rdlowrey

This comment has been minimized.

Copy link

commented Apr 14, 2015

This should now be corrected upstream as noted in the original bug report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.