Potential problem with connection management
I am having an issue when I destroy all view models on the client-side, connections are not terminated on the server. I am seeing that Dispose is called on the server-side models, but connections remain open for another ~60 seconds. Not sure if that is causing an issue, but here is what I am seeing:
(note that I am still on 3.0.1 - #7)
- Mount component and connect view model
- Unmount component and destroy viewmodel
- Observe Dispose is called on server
- Navigate to a client component that does not have a dotnetify connection
- After ~60 seconds, observe that the dotnet server reports "request finished"
- Navigate to a client component that has a dotnetify connection
- Receive an error (below)
dotnet output (my own note in bold): info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://192.168.0.8:5000/dotnetify?id=udciaGwHHoxMlvz9fnw_YA info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. Disposed (output from overridden Dispose(). in this example, this happened about 5 seconds after handshake) info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 54858.6045ms 101
RN/Expo error:
console.error: "Error: Connection disconnected with error 'Error: Websocket closed with status code: undefined (undefined)'."
__expoConsoleLog
\node_modules\expo\src\logs\RemoteConsole.js:98:8
error
\node_modules\react-native\Libraries\ReactNative\YellowBox.js:69:16
log
\node_modules\dotnetify\dist\signalR-netcore.js:1883:35
Expo is prominently showing console.warnings and console.errors from the SignalR library which would otherwise be hidden from view if this happens on a browser. I assume they are only shown on dev build. The important thing is whether the app can recover and re-establish connection when the message is dismissed. How did it behave?
The hard error is hidden in Production mode, but the app does not immediately re-establish connection to the server. Similar repro:
- Mount component and connect view model
- Unmount component and destroy viewmodel
- Observe Dispose is called on server
- Navigate to a client component that does not have a dotnetify connection
- After ~60 seconds, observe that the dotnet server reports "request finished"
- Navigate to a client component that has a dotnetify connection
- ~~Receive an error~~ Observe client vm does not have an active server connection
- After some more time (~60 more seconds), dotnetify client goes from "disconnected" connection state to "reconnecting" and successfully re-establishes the connection and client vm becomes operational
If there are no active viewmodels on the client-side (dotnetify.react.getViewModels() is none) I can understand why dotnetify would be in a disconnected (or even retrying) state. But if the client has explicitly requested to connect another vm (dotnetify.react.connect @ step 6 above), should the framework not immediately try to establish/re-establish connection? Is it that the client framework has not yet realized that the server has closed the connection? Should the client immediately go into disconnected state when the server closes the connection?
I should also mention, this behavior is the same when the redscreen is dismissed in dev mode
Try the latest version. I couldn't reproduce on the example in this repo running on Android emulator. If you can still reproduce, please describe the steps using the repo example.
I am having a hard time reproducing the issue in the example repo, but there is still something here. Not quite sure what at the moment.
In my app I am able to get into a state where the server has closed the connection, the client has destroyed all vms, and I navigate to a component that wants a new vm connection (dotnetify.react.connect) and the client does not establish a connection nor produces any errors. This leaves me with a component that appears functional but doesn't have any server interaction. I realize this is obscure and I am not giving you much to help with, but any thoughts you have would be appreciated.
Something in your app lead to that state. Can you find a way to modify this example repo to mimic it?
On Wed, Nov 28, 2018 at 8:45 PM skyleraustin [email protected] wrote:
In my app I am able to get into a state where the server has closed the connection, the client has destroyed all vms, and I navigate to a component that wants a new vm connection (dotnetify.react.connect) and the client does not establish a connection nor produces any errors. This leaves me with a component that appears functional but doesn't have any server interaction. I realize this is obscure and I am not giving you much to help with, but any thoughts you have would be appreciated.
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/dsuryd/dotnetify-react-native-demo/issues/10#issuecomment-442704974, or mute the thread https://github.com/notifications/unsubscribe-auth/AOS8kkf5iZr9MfteFE36x3gAimAEHUmzks5uz2aEgaJpZM4YyC8i .
Rereading the steps, seems that the server has terminated the particular connection id, but somehow the SignalR client still holds on to it and use it for the next VM, and that's why it's timing out waiting to use a terminated connection. Can you add DeveloperLogging middleware and check if you see something like this at step 5?
[dotNetify] connId=0EO1erJAHPTdnxsRcd6zWg type=OnDisconnected
Something in your app lead to that state. Can you find a way to modify this example repo to mimic it?
I will work on this
Rereading the steps, seems that the server has terminated the particular connection id, but somehow the SignalR client still holds on to it and use it for the next VM, and that's why it's timing out waiting to use a terminated connection. Can you add DeveloperLogging middleware and check if you see something like this at step 5?
[dotNetify] connId=0EO1erJAHPTdnxsRcd6zWg type=OnDisconnected
I have had the UseDeveloperLogging middleware enabled since I started using the framework and have not encountered output like that.
However, in my testing just now, I encountered an exception that I have not seen in most of my other testing. It remains in this state for some time (again about 60 seconds) with unusable client until connection is reestablished
(again, bold is my notes)
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 POST http://192.168.0.8:5000/dotnetify/negotiate 0 info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 0.2538ms 200 application/json info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://192.168.0.8:5000/dotnetify?id=7R-R_FmJT6GgFR9Xr_Tqvg info: Microsoft.AspNetCore.SignalR.HubConnectionContext[1] Completed connection handshake. Using HubProtocol 'json'. Disposed (client vms destroyed because app is sent to background) app is currently on a component that doesnt use dotnetify about 30 seconds later info: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[14] Connection id "0HLIMF7535QQG" communication error. System.IO.IOException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitable.GetResult() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive() --- End of inner exception stack trace --- info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 49904.9288ms 101 after another 60 seconds or so with a dead client component, connection is re-established
My scenario aside, what is the expected behavior when something like this is done when the application is done with dotnetify:
dotnetify.react.getViewModels().forEach(vm => {
vm.$destroy()
});
and then when it needs it again (no matter how long it has been since disposal), a new vm is connected? Does the client framework (have the ability to?) check if an operating server connection is open?
If not, is there a good way to confirm dotnetify has an open connection before attempting to connect vms?
Based on what I am experiencing, dotnetify.react.connect() is not "safe".
Disposed (client vms destroyed because app is sent to background) about 30 seconds later
Do your repro steps always involve sending app to the background? The iOS will kill SignalR connection in this mode, not sure about Android. The 30 seconds would then be the default signalR timeout.
after another 60 seconds or so with a dead client component, connection is re-established
Try reducing reconnection delay using dotnetify.hub.reconnectDelay.
As an experiment, see if manually stopping prev connection before connecting helps:
if (dotnetify.hub.isHubStarted())
{
dotnetify.hub._connection.stop();
dotnetify.hub._hub = null;
}
My scenario aside, what is the expected behavior when something like this is done when the application is done with dotnetify:
dotnetify.react.getViewModels().forEach(vm => { vm.$destroy() });
DotNetify's operating assumption is that once a signalR connection is established, it is for the lifetime of the app. Destroying all VMs won't do anything to the connection.
and then when it needs it again (no matter how long it has been since disposal), a new vm is connected? Does the client framework (have the ability to?) check if an operating server connection is open?
It doesn't need to check. It simply uses the Promise object returned by the SignalR client and starts sending requests when the promise is resolved, and retry starting the connection if rejected.
If not, is there a good way to confirm dotnetify has an open connection before attempting to connect vms?
The app should use dotnetify.connectionStateHandler to access the connection events and do its own robustness strategy to deal with connection problems.
Thank you for the responses. I will do more testing this weekend.
I am getting dotnetify.hub.isHubStarted() is not a function on 3.3.1. I am looking into it but if you are around...
A little different than the documentation (http://dotnetify.net/core/api/connection), but here is my reconnectDelay and reconnectRetry: reconnectDelay: [2, 5, 10] reconnectRetry: null
As noted above, dotnetify.hub.isHubStarted() does not work, but dotnetify.hub.isConnected seems to. Stopping and dropping the hub in your example does force a disconnect, but an immediate reconnect does not occur when dotnetify.react.connect is used. Same problem. Dead component until reconnection occurs.
I will continue experimenting with this new information
Sorry it’s dotnetify.isHubStarted.
On Sun, Dec 2, 2018 at 10:44 AM skyleraustin [email protected] wrote:
A little different than the documentation ( http://dotnetify.net/core/api/connection), but here is my reconnectDelay and reconnectRetry: reconnectDelay: (3) [2, 5, 10] reconnectRetry: null
As noted above, dotnetify.hub.isHubStarted() does not work, but dotnetify.hub.isConnected seems to. Stopping and dropping the hub in your example does force a disconnect, but an immediate reconnect does not occur. Same problem. Dead component until reconnection occurs.
I will continue experimenting with this new information
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/dsuryd/dotnetify-react-native-demo/issues/10#issuecomment-443531773, or mute the thread https://github.com/notifications/unsubscribe-auth/AOS8km7r6NFej5EyqfcD1sYQJt_taTFPks5u1B-MgaJpZM4YyC8i .
I think I am getting closer. When my app gets sent to the background, I would prefer to disconnect dotnetify and ensure that it does not attempt to reconnect until the app re-enters the foreground AND a component is loaded that wants a dotnetify vm.
if (dotnetify.isHubStarted) {
dotnetify.hub.reconnectRetry = -1;
dotnetify.hub._connection.stop();
dotnetify.hub._hub = null;
}
From looking at dotnetify.js:1648, setting dotnetify.hub.reconnectRetry = -1; was the only was I could see to effectively disable reconnection. Is there currently a more elegant way to accomplish this?
When the app returns to the foreground, I am restoring dotnetify.hub.reconnectRetry = null;, and when dotnetify.react.connect occurs, the connection is successfully restored.
THAT ALL SAID..
After the dotnetify connection state goes to disconnected (and then terminated), the following error is thrown:
console.error: "Error: Connection disconnected with error 'Error: Websocket closed with status code: 1001 (Stream end encountered)'."
__expoConsoleLog \node_modules\expo\build\logs\RemoteConsole.js:89:37 error \node_modules\react-native\Libraries\YellowBox\YellowBox.js:59:8 log \node_modules@aspnet\signalr\dist\cjs\Utils.js:196:34
\node_modules@aspnet\signalr\dist\cjs\HttpConnection.js:354:36 step \node_modules@aspnet\signalr\dist\cjs\HttpConnection.js:34:27 \node_modules@aspnet\signalr\dist\cjs\HttpConnection.js:9:13 tryCallTwo \node_modules\promise\setimmediate\core.js:45:7 doResolve \node_modules\promise\setimmediate\core.js:200:23 Promise \node_modules\promise\setimmediate\core.js:66:12 \node_modules@aspnet\signalr\dist\cjs\HttpConnection.js:5:36 onclose \node_modules@aspnet\signalr\dist\cjs\WebSocketTransport.js:96:58 dispatchEvent \node_modules\event-target-shim\lib\event-target.js:172:43 \node_modules\react-native\Libraries\WebSocket\WebSocket.js:276:10 emit \node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:191:12 __callFunction \node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:349:47 \node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106:26 __guard \node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:297:10 callFunctionReturnFlushedQueue \node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:105:17
if (dotnetify.isHubStarted) { dotnetify.hub.reconnectRetry = -1; dotnetify.hub._connection.stop(); dotnetify.hub._hub = null; } From looking at dotnetify.js:1648, setting dotnetify.hub.reconnectRetry = -1; was the only was I could see to effectively disable reconnection. Is there currently a more elegant way to accomplish this?
I can add a new API dotnetify.disconnect() to do all the above.
After the dotnetify connection state goes to disconnected (and then terminated), the following error is thrown: console.error: "Error: Connection disconnected with error 'Error: Websocket closed with status code: 1001 (Stream end encountered)'."
Couldn't reproduce this. I added a new page to the example repo which disconnects dotNetify on mount. Navigated to the page, I could see the server log the disconnected event. Went back to the page with dotNetify; it immediately connected without error.
I just did the same with the example repo and am able to reproduce the error. Also confirmed the offending line is dotnetify.hub._connection.stop();.
Perhaps it is either something with iOS or my environment. Unfortunately, I do not have a way to confirm on Android. Do you have a way to test on iOS?
I can't find many other leads, but there is this unresolved issue here
Is this issue still a problem? I have had this problem since upgrading to dotnet core 3.1 and dotnetify 3.6.2 and haven't been able to recreate it from example application. The websocket disconnects and refreshing the page doesn't do anything until I logout of the application. It occurs after the token expires and hitting refresh on the page. It is only happening in production.