Unable to reconnect

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Unable to reconnect

Postby minimax » 25 Apr 2018, 11:24

Hello,
I'm testing HRC+ which should be able to reconnect client when he looses his connection. When I kill the connection using killconnection in SFS client library, it works.

The problem is when I want to switch the Internet connection from Wifi to mobile data for example (I think, that this is very common use case). HRC+ is activated, it tries to connect to the server, but in server log I see this error mesage:

24 Apr 2018 | 19:37:54,356 | WARN | SFSWorker:Sys:1 | controllers.v290.SystemReqController | | com.smartfoxserver.bitswarm.exceptions.SessionReconnectionException: Session Reconnection failure. Time expired for Session: { Id: 8, Type: DEFAULT, Logged: Yes, IP: x.x.x.x:45505 }
com.smartfoxserver.bitswarm.sessions.DefaultReconnectionManager.reconnectSession(DefaultReconnectionManager.java:154)
com.smartfoxserver.bitswarm.sessions.DefaultSessionManager.reconnectSession(DefaultSessionManager.java:383)
com.smartfoxserver.v2.controllers.system.Handshake.execute(Handshake.java:68)
com.smartfoxserver.v2.controllers.v290.SystemReqController.processRequest(SystemReqController.java:172)
com.smartfoxserver.v2.controllers.v290.SystemReqController.enqueueRequest(SystemReqController.java:127)
com.smartfoxserver.bitswarm.io.protocols.AbstractProtocolCodec.dispatchRequestToController(AbstractProtocolCodec.java:39)
com.smartfoxserver.v2.protocol.SFSProtocolCodec.dispatchRequest(SFSProtocolCodec.java:133)
com.smartfoxserver.v2.protocol.SFSProtocolCodec.onPacketRead(SFSProtocolCodec.java:90)
com.smartfoxserver.v2.protocol.binary.BinaryIoHandler$1.run(BinaryIoHandler.java:477)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

The session could not have expired because the HRC is activated after ~5 seconds and in config file I have idle time set to 180 seconds. What I think is that because new connection has different IP (Wifi and mobile data is different provider) it is unable to find the old session. Is that possible? And if it is is there a way how to fix this?

Thanks for any answer
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 26 Apr 2018, 08:08

Hi,
we have discussed the issue with switching the WIFI many times.
What you're saying is correct, the IP address is the problem.

We have two specific articles that will clarify how this works:
http://smartfoxserver.com/blog/what-rea ... tworkwifi/
http://smartfoxserver.com/blog/handling ... e-devices/

In particular the latter, discusses the matter of 3G/4G and WIfi switching.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 27 Apr 2018, 21:24

Thanks Lapo. After some testing the HRC does not seems to work even with same IP. If I turn Wifi off and on, the HTC is activated on the client side and creates new connection, but the server refuse to reuse the session (same error message) even with same IP. The problem seems to be realted to connection state on the server side. On client its closed, but on server the session (and socket) is still alive and for some reason SFS refuse to reuse this session. Is this behaviour intended? I dont see any real life situation where the sockets are closed on both sides so it can be reused.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 28 Apr 2018, 20:16

The fact that testing locally results in an open connection on the server side depends on how the OS handles the networks switch at a lower level of the TCP stack.
This is very OS dependent and you may find different behaviors under different OSes.

There's not much you can do about it. We wrote about this in the first article I linked, where we explain what happens when pulling the network cable. That's why we recommend detecting the switch via the device's own API and create a new connection.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 30 Apr 2018, 19:06

The problem is not just switching the connection (e.g. Wifi<->3G). HRC does not work even if the client looses temporarly his connection (eg. looses 3G signal temporarly). In this situation, the client detects connection lost, HRC is activated and the client is rejected by the server (with confusing error message Time expired for Session), because naturally the connection is still opened on the server side and I agree, there is not much we can do about it, but would it be possible to add some option to Smartfox config file to reuse opened connections on the server side?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 02 May 2018, 07:53

Hi,
the reconnection upon signal loss is heavily dependent on the state of the TCP from both sides. The client is expected to detect the disconnection because it is on the right side (i.e. the side where the loss of signal happened). The problem is the server side, where it is likely nothing will be detected. Once again we're in a situation very similar to pulling the client's ethernet cable, resulting in no disconnection from the server side.

but would it be possible to add some option to Smartfox config file to reuse opened connections on the server side?

This is not really possible because the connection was lost on the client side and it is the client that initiates the connection. In other words the server cannot reuse a connection that is already lost from the end that initiated it.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 09 May 2018, 11:47

Hello,
I'm still not sure that I understand. What happened is:

1) Client loosed the connection, HRC+ correctly detected the connection lost and tried to reconnect
2) Client created new TCP connection to the server
3) Server throwed exception with "Session expired" message

In step 3) server knows, that the client is trying to reconnect. Why the server will not reuse the session for the new socket? Because of course that the client will not use the old socket and the server can close it safely. Adding option to config file for this would help us tremendously.

If HRC+ expects correctly closed socket on both sides, what is the real world scenario, when HRC+ will restore the session?

Thanks
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 09 May 2018, 14:31

minimax wrote:Hello,
I'm still not sure that I understand. What happened is:

1) Client loosed the connection, HRC+ correctly detected the connection lost and tried to reconnect
2) Client created new TCP connection to the server
3) Server throwed exception with "Session expired" message

In step 3) server knows, that the client is trying to reconnect. Why the server will not reuse the session for the new socket? Because of course that the client will not use the old socket and the server can close it safely. Adding option to config file for this would help us tremendously.

I am not sure either of this specific case. Is there a way to reliably reproduce it?


If HRC+ expects correctly closed socket on both sides, what is the real world scenario, when HRC+ will restore the session?

A actual disconnection :)
If the socket is closed on one side only, it's not a disconnection. It's an half closed socket.
If the client end is closed but the server is not, the server will not recognize the reconnection attempt.
If it's the other way around (server closed, client open) then the client will not trigger a reconnection.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Jochanan
Posts: 79
Joined: 11 May 2018, 09:12

Re: Unable to reconnect

Postby Jochanan » 11 May 2018, 09:29

Lapo wrote:In step 3) server knows, that the client is trying to reconnect. Why the server will not reuse the session for the new socket? Because of course that the client will not use the old socket and the server can close it safely. Adding option to config file for this would help us tremendously.

I am not sure either of this specific case. Is there a way to reliably reproduce it?
[/quote]

What about when you are on mobile, you lost the mobile signal entirely/switch BTS (4g<->edge both happens a lot in elevators/underground stations) and then the signal is restored after some time. or you simply turn off and then on your mobile data. This use case is not working at all.

I would love to hear one single real, reproducable use case when the reconnection actually works.
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 11 May 2018, 11:26

Hello,
it can be reproduced easily, on mobile phone just turn Wifi on and off.

We have probably found a problem from decompiled code, in function:

Code: Select all

public ISession reconnectSession(ISession tempSession, String prevSessionToken)


This could probably fix the issue:

Code: Select all

-- if (session.isReconnectionTimeExpired()) {
++ if (session.isFrozen() && session.isReconnectionTimeExpired()) {


Because isReconnectionTimeExpired is set only to frozen sessions.

Thanks
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 11 May 2018, 16:42

Jochanan wrote:
Lapo wrote:In step 3) server knows, that the client is trying to reconnect. Why the server will not reuse the session for the new socket? Because of course that the client will not use the old socket and the server can close it safely. Adding option to config file for this would help us tremendously.

I am not sure either of this specific case. Is there a way to reliably reproduce it?


What about when you are on mobile, you lost the mobile signal entirely/switch BTS (4g<->edge both happens a lot in elevators/underground stations) and then the signal is restored after some time. or you simply turn off and then on your mobile data. This use case is not working at all.

I would love to hear one single real, reproducable use case when the reconnection actually works.[/quote]
Please re-read what was wrote in the whole thread, and the links to two in-depth articles on this topic.
Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 11 May 2018, 16:52

minimax wrote:Hello,
it can be reproduced easily, on mobile phone just turn Wifi on and off.

Which mobile phone, exactly?
I ask because every OS (even different releases) can behave differently.

We have probably found a problem from decompiled code, in function:

Code: Select all

public ISession reconnectSession(ISession tempSession, String prevSessionToken)


This could probably fix the issue:

Code: Select all

-- if (session.isReconnectionTimeExpired()) {
++ if (session.isFrozen() && session.isReconnectionTimeExpired()) {


Because isReconnectionTimeExpired is set only to frozen sessions.

Thanks

Two things here:
1) reverse engineering the server code is not allowed by the EULA accompanying SFS2X
2) I am not sure what you're trying to say?
A session is "frozen" when a disconnection is detected on the server side and HRC+ is active.
The change you're suggesting is incorrect, because the session object is certainly frozen as it comes from the data structure that holds all of the disconnected and frozen sessions that are waiting to be reconnected. Adding the isFrozen() check is redundant.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 16 May 2018, 14:50

Which mobile phone, exactly?
I ask because every OS (even different releases) can behave differently.


Android 7.0

Two things here:
1) reverse engineering the server code is not allowed by the EULA accompanying SFS2X


Sorry, I did not know. I am not trying to abuse anything. I am just trying to solve the problem.

2) I am not sure what you're trying to say?
A session is "frozen" when a disconnection is detected on the server side and HRC+ is active.
The change you're suggesting is incorrect, because the session object is certainly frozen as it comes from the data structure that holds all of the disconnected and frozen sessions that are waiting to be reconnected. Adding the isFrozen() check is redundant.


I understand, but I dont think, that the session must be frozen, because there is a variable allowUnfrozenReconnection and if its true (and it is always), it looks also for unfrozen sessions if there is no frozen. This is why I think that this was intended to work.

Thanks
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unable to reconnect

Postby Lapo » 16 May 2018, 15:32

I would have to check with a colleague, but the variable you're referring to is just for tests and it's not allowed to work in production. In fact "unfrozen" connections cannot be reconnected (they don't have a valid reconnection timer).

The freeze/unfreeze mechanism is necessary for the Session to maintain consistency. When a connection is lost the server freezes the Session, which in turn starts to queue all the events that need to be sent out. If the client reconnects in time, the unfreeze hooks up the new connection with the Session and all queued up messages are sent out.

I understand what you're asking but there's a central issue: the server must be notified the disconnection. If you shut down the WIfi and no disconnection is ever detected on the other side, there's no way for the mechanism to work.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
minimax
Posts: 7
Joined: 25 Apr 2018, 11:08

Re: Unable to reconnect

Postby minimax » 13 Jun 2018, 10:28

What I'm trying to say is that we were not able to reproduce any situation from real world where the HRC actually works. If there is some problem with the connectivity, than only the client side is interrupted. I understand that it is not possible in current version, but it would be very nice if SFS could handle this situations maybe in future releases. I think that many people would appreciate this.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 41 guests