Playing around with the library, I believe I found a memory leak. Here's a minimal application where I'm able to reproduce this issue:
<?php
namespace App;
use function Icicle\Awaitable\resolve;
use Icicle\Http\Message\{Request, Response};
use Icicle\Socket\Socket;
use Icicle\WebSocket\{Application, Connection};
use Icicle\Loop;
class EchoApplication implements Application
{
public function __construct()
{
Loop\periodic(10, function () {
$mem = round(memory_get_usage(false) / 1024) . 'k';
$peak = round(memory_get_peak_usage(false) / 1024) . 'k';
$date = date('H:i:s');
echo "$date: mem: $mem, peak: $peak\n";
});
}
public function onHandshake(Response $response, Request $request, Socket $socket)
{
return $response;
}
public function onConnection(Connection $connection, Response $response, Request $request)
{
yield $connection->send('Connected to echo WebSocket server powered by Icicle.');
$iterator = $connection->read()->getIterator();
while (yield $iterator->isValid()) {
$message = $iterator->getCurrent();
if ($message->getData() === 'close') {
yield $connection->close();
} else {
yield $connection->send($message);
}
}
}
}
This is simplified EchoApplication from attached example. Every 10 seconds It outputs current memory usage.
My test:
- run websocket server
- connect 1 client
- send 1 message from client
- wait, observe server output
Results:
With single connected client, every minute or so memory usage rises by a few kBs. After client is disconnected, I didn't observe it.
icicleio/websocket v0.9.5, XDebug enabled:
[Info @ 2016/03/29 18:57:37] HTTP server listening on 0.0.0.0:8080
[Debug @ 2016/03/29 18:57:41] Accepted client from 172.17.0.1:45434 on 172.17.0.2:8080
[Debug @ 2016/03/29 18:57:41] Received request from 172.17.0.1:45434 for localhost:10011/?encoding=text
[Debug @ 2016/03/29 18:57:41] Responded to request from 172.17.0.1:45434 for localhost:10011/?encoding=text with 101 Switching Protocols
18:57:47: mem: 2492k, peak: 2743k
18:57:57: mem: 2492k, peak: 2743k
18:58:07: mem: 2492k, peak: 2743k
18:58:17: mem: 2492k, peak: 2743k
18:58:27: mem: 2492k, peak: 2743k
18:58:37: mem: 2492k, peak: 2743k
18:58:47: mem: 2507k, peak: 2758k
18:58:57: mem: 2507k, peak: 2758k
18:59:07: mem: 2507k, peak: 2758k
18:59:17: mem: 2507k, peak: 2758k
18:59:27: mem: 2507k, peak: 2758k
18:59:37: mem: 2507k, peak: 2758k
18:59:47: mem: 2517k, peak: 2767k
18:59:57: mem: 2516k, peak: 2767k
...
icicleio/websocket v0.9.5, XDebug disabled:
[Info @ 2016/03/29 19:00:50] HTTP server listening on 0.0.0.0:8080
[Debug @ 2016/03/29 19:00:58] Accepted client from 172.17.0.1:45566 on 172.17.0.2:8080
[Debug @ 2016/03/29 19:00:58] Received request from 172.17.0.1:45566 for localhost:10011/?encoding=text
[Debug @ 2016/03/29 19:00:58] Responded to request from 172.17.0.1:45566 for localhost:10011/?encoding=text with 101 Switching Protocols
19:01:00: mem: 2185k, peak: 2436k
19:01:10: mem: 2186k, peak: 2436k
19:01:20: mem: 2186k, peak: 2436k
19:01:30: mem: 2186k, peak: 2436k
19:01:40: mem: 2186k, peak: 2436k
19:01:50: mem: 2186k, peak: 2436k
19:02:00: mem: 2200k, peak: 2450k
19:02:10: mem: 2199k, peak: 2450k
19:02:20: mem: 2199k, peak: 2450k
19:02:30: mem: 2199k, peak: 2450k
19:02:40: mem: 2199k, peak: 2450k
19:02:50: mem: 2199k, peak: 2450k
19:03:00: mem: 2209k, peak: 2459k
19:03:10: mem: 2208k, peak: 2459k
19:03:20: mem: 2208k, peak: 2459k
What happens after disconnecting client:
[Debug @ 2016/03/29 20:20:47] Disconnected client from 172.17.0.1:36750 on 172.17.0.2:8080
20:20:48: mem: 2178k, peak: 2452k
20:20:58: mem: 2178k, peak: 2452k
20:21:08: mem: 2178k, peak: 2452k
20:21:18: mem: 2178k, peak: 2452k
20:21:28: mem: 2177k, peak: 2452k
20:21:38: mem: 2177k, peak: 2452k
20:21:48: mem: 2173k, peak: 2452k
20:21:58: mem: 2173k, peak: 2452k
20:22:08: mem: 2173k, peak: 2452k
20:22:18: mem: 2173k, peak: 2452k
20:22:28: mem: 2173k, peak: 2452k
20:22:38: mem: 2173k, peak: 2452k
...
Tests were done on PHP v7.0.4, running on Docker container.
This issue is not present when using master branch (dev-master 90fd48a of icicleio/websocket, dev-master 329068a of icicleio/icicle).
Playing around with the library, I believe I found a memory leak. Here's a minimal application where I'm able to reproduce this issue:
This is simplified EchoApplication from attached example. Every 10 seconds It outputs current memory usage.
My test:
Results:
With single connected client, every minute or so memory usage rises by a few kBs. After client is disconnected, I didn't observe it.
icicleio/websocket v0.9.5, XDebug enabled:
icicleio/websocket v0.9.5, XDebug disabled:
What happens after disconnecting client:
Tests were done on PHP v7.0.4, running on Docker container.
This issue is not present when using master branch (dev-master 90fd48a of
icicleio/websocket, dev-master 329068a oficicleio/icicle).