Edit report at http://bugs.php.net/bug.php?id=54230&edit=1

 ID:                 54230
 User updated by:    bas at baspeters dot com
 Reported by:        bas at baspeters dot com
 Summary:            stream_socket_accept leaks memory on SSL protocol
                     using peer parameter
 Status:             Open
 Type:               Bug
-Package:            OpenSSL related
+Package:            Sockets related
 Operating System:   Linux, OSX
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

For those who want to test but are unable to supply the required
certificate.pem file, here is a link:

http://www.baspeters.com/patches/certificate.pem

password is 'password' as stated in the test script.



I have changed the Package from 'OpenSSL' to 'Sockets related' because I
think it is a better match.


Previous Comments:
------------------------------------------------------------------------
[2011-03-11 20:40:03] bas at baspeters dot com

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 this bug report at http://bugs.php.net/bug.php?id=54230&edit=1

Reply via email to