socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Emitting to rooms fails sometimes after server restarts

Open MyMomSaysIAmSpecial opened this issue 9 years ago • 3 comments

Current behaviour

We have two users, A and B, who chats in a room, if server restarts and users are rejoined to their rooms, sometimes (sometimes!) one (randomly) of users can stop receiving messages to his room despite he joined it.

The fact what user is joined the room is checked with socket.rooms before sending a message.

Here is visual representation

Image of chatlog

Here we see user A and user B sending each other simple plaint text messages, and at certain moment user at left, stops to receive messages to room he is in, and receives only messages emitted to global scope

From server side sending is as simple as that

io.emit('test', message + ' single');
io.to('testroom').emit('chat_message', message + ' room');

This situation is not "systematic", you can have this case after first restart, or have not after 15. Who will stop receive messages is not clear too, it can be user A, user B, or both (both case is much more rare)

It doesn't matter server is running as cluster or single instance, and situation is same on 2.0.1 socket version and 1.7.4, socket.io-redis used as adapter, his version doesn't matter too.

Also what i forgot to mention, what this problem exists only if you "connecting" to socket.io via reconnect, if you just reload your page just after server restart everything is perfect

Steps to reproduce

Just restart socket.io server while user chats

p.s. i ll try to make a fork of fiddle later

MyMomSaysIAmSpecial avatar May 11 '17 14:05 MyMomSaysIAmSpecial

Preliminary reason is redis flushing on SIGINT. I am using same instance of redis for socket.io-redis adapter and for storing chat application cache, and i am flushing it on server restart like this

process.on('SIGINT', function () {
    redisClient.flushdb();
    process.exit();
});

I made exactly 200 server restarts, and in 100% cases situation wasn't repeated when flushing was commented out, the only thing what bothers me in all this, is why this happens with flushdb (at first it was flushall).

I init redis like this

const redis = require('redis');
const redisClient = redis.createClient(
    {
        host: '127.0.0.1',
        port: 6379,
        password: 'password'
    }
);

and using it with socket.io-redis adapter like this

io.adapter(
    require('socket.io-redis')(
        {
            pubClient: redis.createClient(
                {
                    host: '127.0.0.1',
                    port: 6379,
                    password: 'password'
                }
            ),
            subClient: redis.createClient(
                {
                    host: '127.0.0.1',
                    port: 6379,
                    password: 'password'
                }
            )
        }
    )
);

so logically flushdb should not have been affected adapter databases, but looks like it does. Tomorrow i ll test with separate redis instances and maybe with another redis package

Sadly i couldn't reproduce this situation on fiddle fork, probably project is too small and there is lack of redis based logic (my production application relies on redis heavily)

p.s. selecting different database for adapter not helped too

const redisPubClient = redis.createClient(
    {
        host: '127.0.0.1',
        port: 6379,
        password: 'password'
    }
);
redisPubClient.select(7);

const redisSubClient = redis.createClient(
    {
        host: '127.0.0.1',
        port: 6379,
        password: 'password'
    }
);
redisSubClient.select(7);

// redis sticky sessions
io.adapter(
    require('socket.io-redis')(
        {
            pubClient: redisPubClient,
            subClient: redisSubClient
        }
    )
);

Update

It is nothing to have with redis package, i am commented out flushing (tried both all and db) server side, and tried both for 35 restarts, works fine, but after i tried to flush (tried both all and db) from redis-cli (command line) simultaneously with server restart problem showed up again. I tried to bind error, connect, ready, end and so on (all of possible) events to redis clients, nothing abnormal (only ready and connection events are firing), but problem still here, so now i ll try to split flushing on SIGINT and server startup with timeout or smth like that, and if that wont help another redis instance should.

Update

Despite i tried

  1. To change flushall with flushdb
  2. Select different database for redis adapter and cache
  3. Select different instances (ports) for redis adapter and cache
  4. Flush from redis-cli on server restart
  5. Use one single instance of node.js, without clustering

The only guaranteed solution i found to avoid this problem is to not use flushing at all, and just reset necessary cache via set/hset/etc. for example my solution looks looks like this:

process.on('SIGINT', function () {
    for (let socket in io.sockets.sockets) {
        let jwt = io.sockets.sockets[socket].jwt;
        redisClient.hmset(
            'user:' + jwt.project.name + ':' + jwt.user.id,
            'online', 'false',
            'session_count', '0'
        )
    }

    redisClient.end(true);
    redisClient.quit();

    process.exit(0);
});

and works perfectly.

MyMomSaysIAmSpecial avatar May 11 '17 18:05 MyMomSaysIAmSpecial

@MyMomSaysIAmSpecial how do you manage the case where your server die without a SIGINT ?

jflebeau avatar Dec 22 '17 02:12 jflebeau

Closed due to inactivity, please reopen if needed.

darrachequesne avatar Feb 19 '21 14:02 darrachequesne