question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Client connection is lost with no obvious reason

See original GitHub issue

I implemented an integration test that uses TcpClient and TcpServer. This test fails sporadically because the client loses the connection (e.g., disconnecting from 127.0.0.1:12345 due to connection lost) when trying to PollSocket, though the server is up and running.

I’ve treated this problem by increasing ConnectionLostEvaluationIntervalMs so that PollSocket is not called. Then the test is passed, and the client communicates correctly with the server.

The implementation of PollSocket does not provide an apparent reason for the connection loss:

private bool PollSocket()
{
    try
    {
        if (_client.Client == null || !_client.Client.Connected)
            return false;

        /* pear to the documentation on Poll:
         * When passing SelectMode.SelectRead as a parameter to the Poll method it will return 
         * -either- true if Socket.Listen(Int32) has been called and a connection is pending;
         * -or- true if data is available for reading; 
         * -or- true if the connection has been closed, reset, or terminated; 
         * otherwise, returns false
         */
        if (!_client.Client.Poll(0, SelectMode.SelectRead))
            return true;

        var buff = new byte[1];
        var clientSentData = _client.Client.Receive(buff, SocketFlags.Peek) != 0;
        return clientSentData; //False here though Poll() succeeded means we had a disconnect!
    }
    catch (SocketException)
    {
        return false;
    }
}

Thereby, it could be good to include some logging.

I’d like to know if I misunderstood some concepts or if there’re flaws in the PollSocket implementation?

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zllvmcommented, Dec 7, 2022

I’ve done the following change in PollSocket method:

catch (SocketException ex)
{
    Logger?.Invoke($"{Header}poll socket from {ServerIpPort} failed with ex = {ex}");
    return ex.SocketErrorCode == SocketError.TimedOut;
}

After that, I deployed it to the dev server and started a TCP connection that has been up and running for two days with no issues. I could test it by running various commands on the open connection, which worked perfectly. The log entry that I’ve added $"{Header}poll socket from {ServerIpPort} failed with ex = {ex}" appeared several time times (~20) in the monitoring system with Operation timed out exception.

Evidently, PollSocket introduces a harmful behavior by disconnecting an existing connection. I don’t know if it is possible to make it fully reliable. If not, then it should be allowed to override or disable it.

What is your opinion on this, @jchristn? Could we re-open this issue?

0reactions
jchristncommented, Jan 19, 2023

Please re-open if this doesn’t address the issue. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detecting loss of connection between server and client
To deal with this, you can send periodic messages just to check the connection. If the send fails, then the connection has been...
Read more >
Finding source of WiFi Connection Failures
"Client connection lost with reason: 4". Googling found lots of issues, most of which obviously did not apply here, while the rest less ......
Read more >
How to detect a lost TCP socket connection?
The most effective way is to use a timeout. If a packet is not received within a certain time period, then close the...
Read more >
After losing connection to server, client returns ...
Connection drops. Audiobook continues, but Jellyfin returns to server address input screen (as if the server had moved rather than the more ...
Read more >
CCP: EVE client random disconnects are getting REALLY ...
just connection lost, while the other clients running. ccp have no answer to this. they just say all is fine and the servers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found