n.socket.close is not a function Error when server stopped Firefox only
We are using Signalr v2.4.0, we are observing the following error during testing on Firefox (v65) browser only. SignalR went into some kind of recovery loop that keep failing. the $.connectionHub.error call back never triggers. We cannot handle this error from our code. We would like to recover the signalR connection once server connection re-established. We are developing a SPA, so ideally the connection should recover when server is back online.
TypeError: connection.socket.close is not a function[Learn More] jquery.signalR.js:1926:17 stop 2 jQuery stop reconnectTimeout (1750)
Chrome browser same scenario was performing as expected.
Expected behavior
Issue connection error, state change, disconnect callback (any one of them will be ok)
Actual behavior
None of the connection state or exception callback is triggered
Steps to reproduce
connection type is websocket. Start connection connect to hub (without generated proxy) stop application pool on IIS running the signalr hub (server) then in firefox you will get connection lost (good) then n.socket.close is not a function error, and you will continue to get then until page unload. (not good) [update on observation] eventually, one may get a Failed to ping server error from exception (error) callback method. after perhaps a couple hundred times around the reconnectionTimeout stop exception loop.
Could you collect client-side traces using the guide in our documentation and upload them here? (Please drag-and-drop a text file into the comment box to attach it rather than pasting the entire content). Be aware that the log may contain message content, so if this is production data, please let us know and we can give you an email address to send it to.
I have made a trace from DEV, this behaviour is induced by stopping visual studio debug. The same behaviour is observed by stopping IIS or App Pool. This only happen on Firefox. I have since updated to SignalR 2.4.1, there is no change in behaviour, the firefox version is 66.0.3.
This is not a problem on Chrome browser.
I have attached the trace file. FireFox_Console_Trace.txt
The pattern is SignalR trying to call Closing the WebSocket, then error, n.socket.close is not a function, then repeat.
Hrm, if n.socket.close is not a function it indicates that something has manipulated the WebSocket prototype itself. Can you provide a sample project that reproduces the issue? Specifically, we are interested in the client-side libraries in use, it's very possible one of those is messing with the WebSocket (we've seen Angular do this in the past).
Our main application is in emberjs, there are hundreds of dependencies. However I can actually reproduce this with out test client side application, which has no dependencies apart from JQuery. I will download the sample SignalR chat application from MSDN code, then see if I can replicate with that. This might be a Firefox issue.
@anurse I have downloaded the chat sample application from MSDN Code. Upgrade it to signalr v2.4.1, then run it in Firefox, I can replicate the same problem when stop debug from visual studio. You will get n.socket.close s not a function. I have attached the sample app. SignalRChatSample_v2.4.1.zip
Hey, I just stumbled upon this very same issue using firefox browser with react app. The problem is that the websocket itself doesn't contain close() function. So when signalR tries to call ws.close() it fails, it has to call ws.websocket.close(). I created a crude fix for that overwriting window.WebSocket and adding close method to it:
if(!window.WebSocket.modified) {
console.log("Hopifing websocket...");
window.WebSocket = class extends window.WebSocket {
constructor(url: string | URL, protocols?: string | string[]) {
super(url, protocols);
console.log("Create ws: ", this);
}
close() {
console.log("Ws close: ", this);
if(super.close!=null) {
super.close();
} else {
// @ts-ignore
this.websocket.close();
}
}
};
// @ts-ignore
window.WebSocket.modified = true;
}
Call this anytime before opening the signalR connection.
The problem is that the websocket itself doesn't contain close() function.
Looks like it exists. Technically it's window.WebSocket.prototype.close that exists, but it's there.