Skip to content

'Session ID unknown' errors #2

Closed

Description

@sagarjoshi-sct

Working with [email protected], @socket.io/[email protected] and node-cluster.
In low load scenarios, sometimes the first socket request fails with 400 'Session ID unknown'. The retry is successful though.
During high load, all the client connections keep failing continuously with the same error. None of the retries succeed.

Browser logs

/socket.io/?EIO=3&transport=polling&t=Nbmcdeq
97:0{"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000}2:40
/socket.io/?EIO=3&transport=polling&t=Nbmcd-l&sid=ssO51L53Emo8zXiyAAAA
ok
/socket.io/?EIO=3&transport=polling&t=Nbmcd-u&sid=ssO51L53Emo8zXiyAAAA
{"code":1,"message":"Session ID unknown"}
/socket.io/?EIO=3&transport=websocket&sid=ssO51L53Emo8zXiyAAAA
HTTP/1.1 400 Bad Request

Server logs with DEBUG=engine,socket.io*

2021-05-15T15:17:22.687Z engine handshaking client "ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:22.687Z engine:socket sending packet "open" ({"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000})
2021-05-15T15:17:22.687Z engine:socket sending packet "message" ([ '0' ])
2021-05-15T15:17:22.688Z engine:polling setting request
2021-05-15T15:17:22.688Z engine:socket flushing buffer to transport
2021-05-15T15:17:22.689Z engine:polling writing "97:0{"sid":"ssO51L53Emo8zXiyAAAA","upgrades":["websocket"],"pingInterval":20000,"pingTimeout":15000}2:40"
2021-05-15T15:17:22.689Z engine:socket executing batch send callback
2021-05-15T15:17:22.689Z socket.io:server incoming connection with id ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:22.689Z socket.io:client connecting to namespace /
2021-05-15T15:17:22.690Z socket.io:namespace adding socket to nsp /
2021-05-15T15:17:22.690Z socket.io:socket socket connected - writing packet
2021-05-15T15:17:22.690Z socket.io:socket joining room ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:22.690Z socket.io:socket packet already sent in initial handshake
2021-05-15T15:17:22.690Z socket.io:socket joined room [ 'ssO51L53Emo8zXiyAAAA' ]
2021-05-15T15:17:24.097Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.097Z engine handling "POST" http request "/socket.io/?EIO=3&transport=polling&t=Nbmcd-l&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.097Z engine setting new request for existing client
2021-05-15T15:17:24.098Z engine:polling received "7:40/api,"
2021-05-15T15:17:24.098Z engine:socket packet
2021-05-15T15:17:24.098Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.098Z socket.io-parser decoded 0/api, as {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.098Z engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=Nbmcd-u&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.098Z socket.io:client connecting to namespace /api
2021-05-15T15:17:24.098Z socket.io:namespace adding socket to nsp /api
2021-05-15T15:17:24.098Z engine unknown sid "ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.099Z socket.io:socket socket connected - writing packet
2021-05-15T15:17:24.099Z socket.io:socket joining room /api#ssO51L53Emo8zXiyAAAA
2021-05-15T15:17:24.099Z socket.io:client writing packet {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.099Z socket.io-parser encoding packet {"type":0,"nsp":"/api"}
2021-05-15T15:17:24.099Z socket.io-parser encoded {"type":0,"nsp":"/api"} as 0/api,
2021-05-15T15:17:24.099Z engine:socket sending packet "message" (0/api,)
2021-05-15T15:17:24.100Z socket.io:socket joined room [ '/api#ssO51L53Emo8zXiyAAAA' ]
2021-05-15T15:17:24.308Z engine intercepting request for path "/socket.io/"
2021-05-15T15:17:24.308Z engine handling "POST" http request "/socket.io/?EIO=3&transport=polling&t=Nbmce1b&sid=ssO51L53Emo8zXiyAAAA"
2021-05-15T15:17:24.308Z engine setting new request for existing client
2021-05-15T15:17:24.308Z engine:polling received "1:1"
2021-05-15T15:17:24.308Z engine:polling got xhr close packet
2021-05-15T15:17:24.309Z socket.io:client client close with reason transport close
2021-05-15T15:17:24.309Z socket.io:socket closing socket - reason transport close
2021-05-15T15:17:24.309Z socket.io:socket closing socket - reason transport close
2021-05-15T15:17:24.379Z engine unknown sid "ssO51L53Emo8zXiyAAAA"

Activity

  1. darrachequesne commented on May 15, 2021

    @darrachequesne
    Member

    Hi! A few questions:

    • are you able to reproduce consistently? could you please provide the code?
    • I was unable to reproduce the issue with Artillery. By "high load", how many connections in total/new connections per seconds are we talking about?
    • which loadBalancingMethod are you using? Is it least-connection, the default value?

    One possible explanation would be that the master process is overloaded and does not receive the sticky:connection message (sent here) in time, but that sounds weird.

    In the example above, are those the logs of a single child process? Or the logs of all child processes?

  2. sagarjoshi-sct commented on May 17, 2021

    @sagarjoshi-sct
    Author

    Hi @darrachequesne, thanks for the quick response!

    which loadBalancingMethod are you using? Is it least-connection, the default value?

    Yes

    I was unable to reproduce the issue with Artillery. By "high load", how many connections in total/new connections per seconds are we talking about?

    About 10K connections in 30 mins (~5.55 new connections/sec) with Artillery

    are you able to reproduce consistently?

    Yes with Artillery. Sporadic when testing manually.

    are those the logs of a single child process? Or the logs of all child processes?

    I'm not sure. There were 4 node-cluster workers running at the time. How can I distinguish?

    could you please provide the code?

    Can't share the entire code, but here's the relevant snippet.

    index.js

    const cluster = require('cluster');
    const http = require('http');
    const { setupMaster } = require('@socket.io/sticky');
    const RedisHelper = require('./lib/redis-helper');
    const AppServer = require('./lib/app-server');
    
    function initMaster() {
        console.log(`Master ${process.pid} is running`);
    
        const httpServer = http.createServer();
        setupMaster(httpServer, {
            loadBalancingMethod: 'least-connection',
        });
        httpServer.listen(process.env.WEBSOCKET_PORT);
        const numberOfCores = process.env.NO_OF_CPUS_FOR_NODE_CLUSTER
            ? parseInt(process.env.NO_OF_CPUS_FOR_NODE_CLUSTER, 10) : 1;
        for (let i = 0; i < numberOfCores; i += 1) {
            cluster.fork();
        }
    
        cluster.on('exit', (worker) => {
            console.log(`Worker ${worker.process.pid} died`);
            cluster.fork();
        });
    }
    
    function initWorker() {
        RedisHelper.init();
    
        const appServer = new AppServer();
        appServer.init();
    }
    
    if (process.env.USE_VERTICAL_SCALING === 'true') {
        if (cluster.isMaster) {
            initMaster();
        } else {
            console.log(`Worker ${process.pid} started`);
            initWorker();
        }
    } else {
        initWorker();
    }

    app-server.js

    const http = require('http');
    const express = require('express');
    const { setupWorker } = require('@socket.io/sticky');
    const socketIo = require('socket.io');
    const RedisHelper = require('./redis-helper');
    
    function AppServer() {
    }
    
    /**
     * Initialisize App servers, KMS and socket IO server
     */
    AppServer.prototype.init = function () {
        this.initApp();
        this.initSocketIo();
    };
    
    AppServer.prototype.initApp = function () {
        this.app = express();
    
        if (this.server == null) {
            this.server = http.createServer(this.app);
    
            const port = process.env.SERVER_PORT;
            this.server.listen(port, () => {
                console.log(`Server started on port ${port}`);
            });
        }
    };
    
    AppServer.prototype.initSocketIo = function () {
        this.io = socketIo(
            process.env.USE_VERTICAL_SCALING === 'true'
                ? http.createServer()
                : http.createServer().listen(process.env.WEBSOCKET_PORT),
            {
                pingInterval: process.env.PING_INTERVAL,
                pingTimeout: process.env.PING_TIMEOUT,
                handlePreflightRequest: (req, res) => {
                    res.writeHead(200, {
                        'Access-Control-Allow-Origin': process.env.FRONT_END_URL,
                        'Access-Control-Allow-Methods': 'GET',
                        'Access-Control-Allow-Headers': 'Authorization',
                        'Access-Control-Allow-Credentials': true,
                    });
                    res.end();
                },
            },
        );
    
        if (process.env.USE_VERTICAL_SCALING === 'true') {
            setupWorker(this.io);
        }
    
        const adapter = RedisHelper.getSocketIoAdapter();
        this.io.adapter(adapter);
    };
    
    module.exports = AppServer;
  3. sagarjoshi-sct commented on May 17, 2021

    @sagarjoshi-sct
    Author

    Also, I noticed that socket.io mentions this package in its v3 documentation, whereas the v2 page only mentions sticky-session.
    I'm still on socket.io v2.3.0. Should that matter ?

  4. sagarjoshi-sct commented on May 19, 2021

    @sagarjoshi-sct
    Author

    Update 1:

    The node-cluster is deployed on an AWS EC2 instance, which is behind an AWS ALB. Earlier I thought this was irrelevant as there is just a single EC2 instance. Stickiness was also enabled, just in case.

    Strangely, the 'Session ID unknown' errors go away if I use an AWS NLB instead, just for the socket.io connections (API requests continue flowing through the ALB).
    However, this adds overhead of an additional component.

    Is there any additional configuration required to make this work with ALB ?

  5. darrachequesne commented on May 19, 2021

    @darrachequesne
    Member

    I'm still on socket.io v2.3.0. Should that matter ?

    No, it should be compatible with Socket.IO v2 too.

    Is there any additional configuration required to make this work with ALB ?

    I don't think so. That's really weird, if there is a single EC2 instance, any load-balancer should route the traffic to this given VM. Could you please check if there are any difference for the HTTP requests (headers, cookie maybe?)?

  6. sagarjoshi-sct commented on May 26, 2021

    @sagarjoshi-sct
    Author

    Thanks @darrachequesne ,
    I'll inspect the request headers and cookies to see if ALB is modifying them.
    For now though, have discarded the ALB altogether, and the library works well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions