Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server closes the socket after a time. How do I ensure it stays open? #87

Open
ihmpartners opened this issue Feb 5, 2024 · 6 comments
Labels

Comments

@ihmpartners
Copy link

Description

The server socket closes after a time ( varies )

I'm not sure if it's the browser/javascript end that is abandoning the connection or the server itself that's closing it .......

My web page tries to send a query to my socket and I get a 'WebSocket is already in CLOSING o CLOSED state' console message on the browser console.

@Theldus
Copy link
Owner

Theldus commented Feb 6, 2024

Hi @ihmpartners,
Normally this should not happen, since wsServer only closes the connection if it receives a CLOSE frame or if it receives an invalid frame.

A second option is if your browser is sending PING packets and wsServer is not responding to them, which is a bug and needs to be fixed. A third option is if your browser is silently losing connection with the wsServer, such as a weak WiFi signal, etc.

In any case, the dc75fda commit adds a few more messages reporting the proper close reason, which may help us investigate this further. You can build wsServer in verbose mode with:

# Makefile
$ CFLAGS="-DVERBOSE_MODE" make

# or via CMake:
$ mkdir build && cd build/
$ CFLAGS="-DVERBOSE_MODE" cmake ..
$ make

The verbose build should also report if it receives any type of malformed packet, etc.

The server socket closes after a time ( varies )

I know it varies, but can you give me an estimate? like under 30 minutes, more than 1 hour... I'll leave it running here and see if I can reproduce it too.

Is your client a browser? If so, which one? version, and etc too.

@ihmpartners
Copy link
Author

ihmpartners commented Feb 6, 2024 via email

@ihmpartners
Copy link
Author

ihmpartners commented Feb 6, 2024 via email

@ihmpartners
Copy link
Author

ihmpartners commented Feb 6, 2024 via email

@ihmpartners
Copy link
Author

All connections are reported as address 127.0.0.1 even when connecting from a remote based browser

@Theldus
Copy link
Owner

Theldus commented Feb 7, 2024

Hi @ihmpartners,

Alerts Connection opened, addr: 127.0.0.1
Received a Data message: {"Subscribe":
["currentTxp","LimProfile","overRangeOP"]} (size: 56, type: 1), from:
127.0.0.1
Received an Alert message: {"Type": "Subscribe"} (size: 21, type: 1), from:
127.0.0.1
An error has occurred while trying to read next byte
                      <<<< About 3 minutes
Data Connection closed, addr: 127.0.0.1
Closing: normal close
An error has occurred while trying to read next byte
Alerts Connection closed, addr: 127.0.0.1
Closing: normal close

If I open my web page, both connections establish.
If I then just sit and wait it takes < 5 minutes before closing

Interesting... what I feared is happening: a completely random disconnection without an apparent error. I tried to reproduce here, but the server maintained the connection for over two hours... with no apparent errors.

An error has occurred while trying to read next byte

This means that there was an abrupt disconnection, without a close handshake, etc.

I really have no idea what it could be... but maybe sending periodic pings is enough to keep the connection alive, something like this:

while (1) {
    ws_ping(NULL, 2);
    sleep(10);
}

in a separate thread. This way, a ping packet will be sent to the client every 10 seconds (with tolerance of 2 failed PONGs), which (hopefully) will keep the connection alive. There is an example file at: examples/ping/ping.c that you can run and see if the connection still closing after a few minutes.

If for some reason you get "Closing, reason: many unanswered PINGs" (in verbose mode), it means that your client is not responding to all PINGs and that there is a connection issue between both parties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants