Skip to content

Instantly share code, notes, and snippets.

@cvuorinen
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvuorinen/1640b1a8ff2a7a19dd8e to your computer and use it in GitHub Desktop.
Save cvuorinen/1640b1a8ff2a7a19dd8e to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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

@rdlowrey
Copy link

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