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.

Connection.isConnected() falsly returns true after network disconnect/reconnect

See original GitHub issue

When I connect to the Redis instance with Jedis and then disable ethernet adapter in windows and reanable it, isConnected() method will return true, when in reality it is not and all redis commands like ping, lpush, etc. will throw java.net.SocketException.

Caused by: java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)
    at redis.clients.util.RedisOutputStream.flush(RedisOutputStream.java:223)
    at redis.clients.jedis.Connection.flush(Connection.java:276)
    ... 11 more

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
defnullcommented, Nov 8, 2019

Welcome to the joys of network programming: The only way to find out if you can send data over a socket, is sending data over a socket. This is true for any networking library, not just Jedis.

isConnected() == false tells you that the client needs to reconnect (which it does automatically). isConnected() == true does not tell you anything. The next command might get through, or it might not. Even if the TCP stream is perfectly fine and healthy at the time you call isConnected() or ping(), network might go down the millisecond after. You ALWAYS have to expect commands to fail. Checking isConnected() beforehand is useless.

That said, isConnected() will only lie to you if the socket is in an undefined state (disconnected, but the operating system does not know that yet). It returns true if the socket appear to be connected. That’s fair, I think.

0reactions
galsivancommented, Jan 24, 2019

It still doesn’t make a lot of sense because it is not really connected. Normally an isConnected() function checks that a connection is still alive, not that the object is initialized or resources are present. The name is misleading since the function returns true when the connection is dead and redis is down. I think maybe something like isInitialized() would make more sense. Just my two cents.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection.isConnected() falsly returns true after network ...
When I connect to the Redis instance with Jedis and then disable ethernet adapter in windows and reanable it, isConnected() method will ...
Read more >
[SOLVED] Reconnect ESP8266 NodeMCU to Wi-Fi Network ...
This example shows how to connect to a network and checks every 30 seconds if it is still connected. If it isn't, it...
Read more >
If isConnected returns true, why does Exeption say that it isn't ...
isConnected() tells you about the state of the Socket . If you have ever connected or accepted it, it returns true. Even after...
Read more >
Troubleshoot network problems - Azure Sphere | Microsoft Learn
For network failures, a high-level application can call WifiConfig_GetNetworkDiagnostics to gather information about the problem.
Read more >
Managing network connection status in React Native
The netInfo library fires the isConnected=false. The application is unable to use internet after. Even though other application on my device are ...
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