socketcluster
socketcluster copied to clipboard
worker crash in "Socket hung up" exception when closing react-native clients
Our team implemented a client side socketCluster in react-native (iOS & Android). It worked correctly. but when a client application (iOS or Android) closed, the "Socket hung up" exception occurred and the current worker not worked either.
our server-side codes are:
//our codes in worker.js
var scServer = this.scServer;
scServer.on('connection', function (socket) {
console.log('socket #' + socket.id + ' connected in (' + process.pid + ')');
socket.on('message', function (data) {
console.log('message (' + process.pid + "): ", data);
});
socket.on('close', function (data) {
console.log('socket #' + socket.id + ' closed in (' + process.pid + "): ", data);
});
socket.on('error', function (data) {
console.log('error.name in (' + process.pid + "): ", data.name);
console.log('error.message in (' + process.pid + "): ", data.message);
console.log('error in (' + process.pid + "): ", data);
});
});
the exception in server side is:
error.name in (23190): SocketProtocolError
error.message in (23190): Socket hung up
error in (23190): { SocketProtocolError: Socket hung up
at SCServerSocket._onSCClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/scserversocket.js:249:17)
at WebSocket.<anonymous> (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/scserversocket.js:80:10)
at WebSocket.emit (events.js:182:13)
at WebSocket.emitClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:180:10)
at Socket.socketOnClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:802:15)
at Socket.emit (events.js:182:13)
at TCP._handle.close (net.js:611:12)
name: 'SocketProtocolError',
message: 'Socket hung up',
code: 1006 }
1550930660777 - Origin: Worker (PID 23190)
[Warning] SocketProtocolError: Socket hung up
at SCServerSocket._onSCClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/scserversocket.js:249:17)
at WebSocket.<anonymous> (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/scserversocket.js:80:10)
at WebSocket.emit (events.js:182:13)
at WebSocket.emitClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:180:10)
at Socket.socketOnClose (/Users/ali/IdeaProjects/SocketClusterBackend/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:802:15)
at Socket.emit (events.js:182:13)
at TCP._handle.close (net.js:611:12)
It seems there is an uncaught exception that crash worker (started with 1550930660777).
I read the below issue: [https://github.com/SocketCluster/socketcluster/issues/41]
// server.js
socketCluster.on('fail', function(error)
{
console.log(error);
});
// worker.js
worker.on('error', function(error)
{
console.log(error);
});
but there is not the worker object to set on-error in worker.js file and listening to 'fail' event of SocketCluster object in server.js file not catch this exception.
so if our problem is from the uncaught exception, please help me to handle this exception or any solution to our problem.
Dear @jondubois , do you have any solution to my problem?
@bodaghialib4 Sorry for the delay in response. Based the logs you provided, I cannot see that the worker is crashing. It's logged as a warning; which shouldn't crash the worker.
By default, SC should show the worker PIDs on launch. You can use the command line (e.g. ps -e | grep node
) to check if the process PID changes after the error. If the PID is the same, then it means that the worker did not crash. Also you can try adding a console.log at the top of worker.js
and see if your message gets logged after the error happens. If your log does not show, then it means that the worker did not restart.
Dear @jondubois , thanks a lot for your response and sorry for the delay in my response. I tested most of the situation and I find out that this situation only occurred when there is only one connection in the current worker. I create a simple test project and the error exists yet. my code is:
in client(react native - in App.js file):
componentDidMount() {
this.SCClient = null;
let options = {
hostname: '192.168.1.104',
port: 8000,
disconnectOnUnload: true,
autoReconnect: true
};
this.SCClient = socketCluster.create(options);
this.SCClient.on('connect', () => {
this.SCClient.subscribe("Hello");
try {
this.SCClient.publish("Hello",
{
uuid: userAccountStore.UID,
negotiation: 'hello',
}
);
} catch (error) {
console.warn("error in SCClient.publish:" + error);
}
});
this.SCClient.on('error', (data) => {
console.log('socketCluster error:' + data);
});
}
in worker:
var SCWorker = require('socketcluster/scworker');
var express = require('express');
var serveStatic = require('serve-static');
var path = require('path');
var morgan = require('morgan');
var healthChecker = require('sc-framework-health-check');
class Worker extends SCWorker {
run() {
console.log(' >> Worker PID:', process.pid);
var environment = this.options.environment;
var app = express();
var httpServer = this.httpServer;
var scServer = this.scServer;
if (environment === 'dev') {
// Log every HTTP request. See https://github.com/expressjs/morgan for other
// available formats.
app.use(morgan('dev'));
}
app.use(serveStatic(path.resolve(__dirname, 'public')));
// Add GET /health-check express route
healthChecker.attach(this, app);
httpServer.on('request', app);
var interval = setInterval(function () {
console.log("" + process.pid + " is OK")
}, 5000);
scServer.on('connection', function (socket) {
console.log('new socket #' + socket.id + ' connected in (' + process.pid + ')');
socket.on('message', function (data) {
//console.log('message (' + process.pid + "): ", data);
});
socket.on('close', function (data) {
console.log('socket #' + socket.id + ' closed in (' + process.pid + "): ", data);
});
socket.on('error', function (data) {
console.log('error in (' + process.pid + "): ", data);
});
});
}
}
new Worker();
logs:
/usr/local/bin/node /Users/ali/IdeaProjects/testSocketCluster/server.js
[Busy] Launching SocketCluster
!! The sc-hot-reboot plugin is watching for code changes in the /Users/ali/IdeaProjects/testSocketCluster directory
>> Broker PID: 36811
>> WorkerCluster PID: 36812
>> Worker PID: 36815
>> Worker PID: 36814
>> Worker PID: 36813
[Active] SocketCluster started
Version: 14.3.3
Environment: dev
WebSocket engine: ws
Port: 8000
Master PID: 36810
Worker count: 3
Broker count: 1
****************************
36813 is OK
36814 is OK
36815 is OK
****************************
36813 is OK
36814 is OK
36815 is OK
new socket #gKs9p-evCfaxntJdAAAA connected in (36813)
****************************
36813 is OK
36814 is OK
36815 is OK
****************************
36814 is OK
36813 is OK
36815 is OK
****************************
36813 is OK
36814 is OK
36815 is OK
socket #gKs9p-evCfaxntJdAAAA closed in (36813): 1006
1552229441997 - Origin: Worker (PID 36813)
[Warning] SocketProtocolError: Socket hung up
at SCServerSocket._onSCClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/scserversocket.js:249:17)
at WebSocket.<anonymous> (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/scserversocket.js:80:10)
at WebSocket.emit (events.js:182:13)
at WebSocket.emitClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:180:10)
at Socket.socketOnClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:802:15)
at Socket.emit (events.js:182:13)
at TCP._handle.close (net.js:611:12)
error in (36813): { SocketProtocolError: Socket hung up
at SCServerSocket._onSCClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/scserversocket.js:249:17)
at WebSocket.<anonymous> (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/scserversocket.js:80:10)
at WebSocket.emit (events.js:182:13)
at WebSocket.emitClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:180:10)
at Socket.socketOnClose (/Users/ali/IdeaProjects/testSocketCluster/node_modules/socketcluster-server/node_modules/ws/lib/websocket.js:802:15)
at Socket.emit (events.js:182:13)
at TCP._handle.close (net.js:611:12)
name: 'SocketProtocolError',
message: 'Socket hung up',
code: 1006 }
new socket #Mao7kZuvI1RebUezAAAA connected in (36815)
****************************
36815 is OK
36813 is OK
36814 is OK
****************************
36814 is OK
36815 is OK
36813 is OK
****************************
36814 is OK
36815 is OK
36813 is OK
****************************
36815 is OK
36814 is OK
****************************
36814 is OK
36815 is OK
****************************
36815 is OK
36814 is OK
****************************
36815 is OK
36814 is OK
****************************
36815 is OK
36814 is OK
****************************
36815 is OK
36814 is OK
****************************
I closed the client completely and reopened it. then this error occurred.
At first there are three workers but in the end, only two of them is OK (it seem process 36813 was crashed).
this error does not occur all times but most of the time I have this error.
I'm developing a React Native app,
Every time I stop the socket server, socket client will throw the Socket hung up
error that I cannot catch it. It causes my native app crashed.
I already did this without success:
socket = socketCluster.create(options);
socket.on('error', this.onError);
How can I catch the socket hangup error?
I'm developing a React Native app, Every time I stop the socket server, socket client will throw the
Socket hung up
error that I cannot catch it. It causes my native app crashed.I already did this without success:
socket = socketCluster.create(options); socket.on('error', this.onError);
How can I catch the socket hangup error?
Sorry, it's my fault!
I read the source code and figured out that I delayed the socket.on('error', this.onError);
1500ms.
So the error was thrown before the listener was attached
Dear @luatnd do your workers in the server-side continue working currectly when you close one of your React Native app?
Dear @luatnd do your workers in the server-side continue working currectly when you close one of your React Native app?
I'm not sure because I'm not the one who implemented the server-side, but I think my college did it easily without any issue. We've used socketcluster for 1 year.
I'm not sure because I'm not the one who implemented the server-side, but I think my college did it easily without any issue. We've used socketcluster for 1 year.
Thanks for your response. I think I use it in an inappropriate way.