Max call stack size exceeded on Connection.close #54

Open
opened 2018-08-27 08:08:58 +00:00 by NishantDesai1306 · 3 comments
NishantDesai1306 commented 2018-08-27 08:08:58 +00:00 (Migrated from github.com)

I have a periodic service in nodejs that tries to create 30-40 socket connections and close it in case (it gets connected successfully or some error occurs while connecting) but every now an then I get Maxmimum call stack size exceeded error at line connection.close()

My Code structure

const connection = ws.connect(socketServerUrl, (err) => {
    if (err) {
        // console.log the error
    }
    else {
        connection.sendPing();
    }
});

connection.on('error', (err) => {
    const reason = err && err.message ? err.message : err;

    // THIS IS WHERE I GET THAT MAX CALL STACK SIZE REACHED EXCEPTION
    connection.close(); // release the connection
});

connection.on('pong', (data) => {
    connection.close(); // release the connection
});

Error Screenshot
image

I have a periodic service in nodejs that tries to create 30-40 socket connections and close it in case (it gets connected successfully or some error occurs while connecting) but every now an then I get **Maxmimum call stack size exceeded** error at line `connection.close()` **My Code structure** ``` const connection = ws.connect(socketServerUrl, (err) => { if (err) { // console.log the error } else { connection.sendPing(); } }); connection.on('error', (err) => { const reason = err && err.message ? err.message : err; // THIS IS WHERE I GET THAT MAX CALL STACK SIZE REACHED EXCEPTION connection.close(); // release the connection }); connection.on('pong', (data) => { connection.close(); // release the connection }); ``` **Error Screenshot** ![image](https://user-images.githubusercontent.com/18420157/44648194-ea729d00-a9fd-11e8-879f-aebb7826f721.png)
NishantDesai1306 commented 2018-08-27 08:10:19 +00:00 (Migrated from github.com)

I am also open to other better ways to do this, basically what I need to do is check every few minutes whether socket servers are accepting connection or not.

I am also open to other better ways to do this, basically what I need to do is check every few minutes whether socket servers are accepting connection or not.
taomas commented 2018-09-18 02:55:31 +00:00 (Migrated from github.com)

i have the same problem when close browser or refresh browser

i have the same problem when close browser or refresh browser
sitegui commented 2018-11-28 16:28:51 +00:00 (Migrated from github.com)

Hi, the problem here seems to be a loop error -> close -> error -> ...

The error event will naturally also close the socket (https://github.com/sitegui/nodejs-websocket#event-errorerrobj), normally you don't have to explicit call it.

However, the lib shouldn't crash when someone does it :)
This happens because a call to close() (by WebSocket spec) does not close the socket right away. First it tries to inform the peer the connection will be dropped by a given reason. The underlying call to write errors and cycle error -> close -> write -> error -> ... begins

I don't have the time to fix this issue for now, however I'm open for a PR if you want to dive into the code.

Best regards,

Hi, the problem here seems to be a loop `error` -> `close` -> `error` -> ... The `error` event will naturally also close the socket (https://github.com/sitegui/nodejs-websocket#event-errorerrobj), normally you don't have to explicit call it. However, the lib shouldn't crash when someone does it :) This happens because a call to `close()` (by WebSocket spec) does not close the socket right away. First it tries to inform the peer the connection will be dropped by a given reason. The underlying call to `write` errors and cycle `error` -> `close` -> `write` -> `error` -> ... begins I don't have the time to fix this issue for now, however I'm open for a PR if you want to dive into the code. Best regards,
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: sitegui/nodejs-websocket#54
No description provided.