From:
Operating system: Linux, OSX
PHP version: 5.3.5
Package: OpenSSL related
Bug Type: Bug
Bug description:stream_socket_accept leaks memory on SSL protocol using peer
parameter
Description:
------------
I encountered a memory leak issue when creating an SSL enabled socket
server in a long lived process. When an incoming listening socket is
accepted on either the SSL or TLS protocol, the &$peer passthrough variable
in the stream_socket_accept function causes a memory leak.
NOTE: The test script below needs a valid certificate.pem file to work!!
NOTE: The test script spawns a webserver to be reached on 127.0.0.1:8888
NOTE: Testing can be done in a browser by choosing appropriate protocol
(http/https) and perform regular page refreshes
Test matrix:
SSL=true, PEER=true -> memory leak!
SSL=false, PEER=true -> no memory leak
SSL=true, PEER=false -> no memory leak
SSL=false, PEER=false -> no memory leak
Test script:
---------------
define('SSL', true);
define('PEER', true);
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'local_cert',
'certificate.pem');
stream_context_set_option($context, 'ssl', 'passphrase', 'password');
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
if(SSL) {
$server = stream_socket_server("ssl://0.0.0.0:8888", $errno, $errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
} else {
$server = stream_socket_server("tcp://0.0.0.0:8888", $errno, $errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN);
}
while(true) {
if(PEER) { $client = @stream_socket_accept($server, 2, $peer); } else {
$client = @stream_socket_accept($server, 2); }
if($client === false) continue;
@fwrite($client, "HTTP/1.1 200 OK\r\n\r\nMemory usage:
".memory_get_usage().' bytes');
@fclose($client);
}
Expected result:
----------------
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Memory usage: 641032 bytes
Actual result:
--------------
Memory usage: 641368 bytes
Memory usage: 641560 bytes
Memory usage: 641752 bytes
Memory usage: 641944 bytes
Memory usage: 642232 bytes
Memory usage: 642424 bytes
Memory usage: 642616 bytes
Memory usage: 642808 bytes
Memory usage: 643000 bytes
Memory usage: 643192 bytes
Memory usage: 643384 bytes
Memory usage: 643576 bytes
--
Edit bug report at http://bugs.php.net/bug.php?id=54230&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=54230&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=54230&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=54230&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=54230&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=54230&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=54230&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=54230&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=54230&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=54230&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=54230&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=54230&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=54230&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=54230&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=54230&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54230&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54230&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=54230&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=54230&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=54230&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=54230&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=54230&r=mysqlcfg