From: info at tphnet dot com Operating system: Windows XP SP1 PHP version: 5.0.0 PHP Bug Type: Sockets related Bug description: Listening on non-blocking socket leaks memory
Description: ------------ I've been trying to write a simple multi-client server application in PHP. Everything is working just fine, the only problem is that the server is leaking memory. Reproduce code: --------------- $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($socket, 'localhost', 1234); socket_listen($socket); socket_set_nonblock($socket); while(true){ $new_connection = @socket_accept($socket); unset($new_connection); } Expected result: ---------------- The above server will accept connections on localhot:1234 and will disconnect you directly after you connect. The socket is setup in non-blocking mode, so that with a couple of minor modifications it becomes possible to handle multiple clients at the same time. Actual result: -------------- The code does exactly what it is supposed to do, but its memory use increases very fast. It fills up my 512 megs in about a minute. The memory leak occours in the socket_accept() function. Removing this line will stop the memory leak from occouring. If you remove the socket_set_nonblock() function it also does not leak memory anymore. This seems kind of logical because the script will "hang" on the socket_accept() function instead of looping over it constantly. In the version of the script I'm actually using, a "usleep(100000)" has been added in the while loop to make sure the script doesn't consume all available resources, but the memory leak still remains. Although it takes much longer to fill all memory because socket_accept() is called less frequent. Removing the "@" sign infront of the socket_accept() function causes the following two errors to be printed on the console for every call to socket_accpet(): Warning: socket_accept(): unable to accept incoming connection [0]: Een niet-blokkerende socketbewerking kan niet onmiddellijk worden voltooid. Warning: socket_accept(): unable to accept socket connection [0]: De bewerking is voltooid. The first error roughly translates to "A non-blocking socket action cannot be executed immediately" and the second to "The action has been completed". I'm not really an expert on sockets so I'm not sure if my script is just flawed or this is a real bug. I've used the above code in a somewhat more advanced form to create a small POP3 server. This server is functioning just fine, the only problem is that it is leaking memory. -- Edit bug report at http://bugs.php.net/?id=29560&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=29560&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=29560&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=29560&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=29560&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=29560&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=29560&r=needscript Try newer version: http://bugs.php.net/fix.php?id=29560&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=29560&r=support Expected behavior: http://bugs.php.net/fix.php?id=29560&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=29560&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=29560&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=29560&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29560&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=29560&r=dst IIS Stability: http://bugs.php.net/fix.php?id=29560&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=29560&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=29560&r=float