Memory leak - adapter.rooms.size getting bigger and bigger
See original GitHub issueDescribe the bug Room size is getting bigger while there are a lot of connections. I think problem is more obvious, when server is not able to handle connections - if there is a lot of connections in the same time
Second issue is with disconnect event - not called every time when connection is closed/killed (don’t know if it’s bug)
To Reproduce Just copy&paste&run the code under.
Socket.IO server version: 4.1.3
(also tried on 2.2.0
)
Server
import io from 'socket.io';
import express from 'express';
const app = express();
const server = app.listen(3000);
io(server, {});
userNamespace = socketIo.of('/');
let disconnectCount = 0;
userNamespace.on('connection', async (socket) => {
const roomId = Math.round(Math.random() * 10000000000);
socket.join(`Room - ${roomId}`);
socket.on('disconnect', () => { // SECOND BUG - not called every time when connection is closed
disconnectCount++;
//console.log('Room - ${roomId} - leave');
});
});
setInterval(() => {
console.log(`Room size ${userNamespace.adapter.rooms.size}, disconnect count ${disconnectCount}`);
}, 5000);
WS client
Client for server v.4.1.3
npm install -g artillery@latest
npm install artillery-engine-socketio-v3
400 connections (rooms)
test.yaml
config:
target: "http://localhost:3000/"
phases:
- duration: 10
arrivalRate: 40
engines:
socketio-v3: {}
scenarios:
- name: "A user that just lurks"
weight: 90
engine: "socketio-v3"
flow:
- emit: ["add user"]
- think: 1
Run client
artillery run test.yaml
Important is return codes - 395x of “0” which is OK. 5x unable to connect. Client output should be something like this:
(output only for - FIRST RUN)
Summary report @ 15:31:25(+0200) 2021-08-30
Scenarios launched: 400
Scenarios completed: 395
Requests completed: 395
Mean response/sec: 31.63
Response time (msec):
min: 0.2
max: 3.8
median: 0.3
p95: 0.6
p99: 1.6
Scenario counts:
A user that just lurks: 400 (100%)
Codes:
0: 395
Errors:
Error: xhr poll error: 5
// FIRST RUN (0 >> 38) - should be zero at the end
Codes:
0: 395
Errors:
Error: xhr poll error: 5
Room size 0
Room size 6
Room size 76
Room size 178
Room size 32
Room size 74
Room size 74
Room size 74
Room size 74
Room size 46
Room size 38
// SECOND RUN (38 >> 76)
Codes:
0: 400
Errors:
-
Room size 38
Room size 96
Room size 140
Room size 123
Room size 76
// THIRD RUN (74 >> 292)
Codes:
0: 379
Errors:
Error: xhr poll error: 21
Room size 74
Room size 98
Room size 156
Room size 264
Room size 217
Room size 292
Room size 292
Room size 292
Expected behavior Room size should be 0 at the end of tests, after close all connections.
Platform:
- Device: Chrome
- OS: Windows
- Socket.io 4.1.3 (also tried on 2.2.0)
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
My application is proprietary but I can confirm this is happening also. Over 12 hours I’m logging 27k connections and 24k disconnections. It’s eventually causing my application to crash
@matiaslopezd the
socket
object should be properly GC’ed, so there’s no need to manually callremoveAllListeners()
You can test it by creating a lot of clients and check the heapdump after disconnection: https://socket.io/docs/v4/load-testing/
@BlackBacker apart from getting a warning from artillery, I can’t reproduce:
In your case, maybe artillery is spawning some kind of zombie clients? Could you please check the size of the
io.of("/").sockets
Map?