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.

Error "write EPIPE" causes client to disconnect but server keeps running

See original GitHub issue

I get an error message on the server saying “Found error: Error: write EPIPE” when I use the following code to send messages from server to client and use an error listener. The error message is from the error listener I write on server. (Some codes are on the client which I didn’t post here)

try {
	ws.send(msg, function(error) {
        	if(error == undefined)
			return;
		else
		        logger.debug("Async error:"+error);
	});
} catch(e) {
	logger.debug("Sync error: " + e);
	ws.close();
}
wss.on('error', function error(err) {
	console.log('Error: ' + err.code);
});

Error listener is inside wss.on('connection', function(ws, req) {}):

ws.on('error', function(err) {
      logger.debug('Found error: ' + err);
});

ws.on('close', function() {
      logger.debug('connection closed.');
});

When I get this error, the client closes (readyState is not open), but the server keeps running. Is there anything I can do to figure out what causes the error and closes the client? I am running a realtime game. I didn’t do anything special other than just run the client normally and the client computer is in my room. I wonder if this is related to the server thinking the client is in idle status. For example, when I send little data from client to the server, the client crashes, when I keep pressing the mouse button, it sends messages to the server very often and the client is always alive.

I have used the “ping-pong” code on the server. But not sure if I need to implement that on the client.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
dnwldbs84commented, Mar 8, 2018

crazyyi It should be outside of onconnection. If you set it to inside of onconnection then it will be repeated for every socket connection.

1reaction
crazyyicommented, Jul 14, 2017

That is hard to reproduce using only ws. I need to figure out some other way. But when I am testing it today. I found that after a while all sockets are terminated. I use the following code:

function heartbeat() {
  this.isAlive = true;
}

wss.on('connection', function(ws, req) {
    const interval = setInterval(function ping() {
        wss.clients.forEach(function each(ws) {
            if (ws.isAlive === false) {
                logger.debug('terminating socket');
                return ws.terminate();
            }
            ws.isAlive = false;
            ws.ping('', false, true);
       });
    }, 30000);

    ws.isAlive = true;
    ws.on('pong', heartbeat);
});

After a few seconds all of the 30 something connected bots/clients are terminated (with log message “terminating socket”). I try to understand the ping-pong example but I think maybe I miss something?

UPD: After I took this part out of the wss.on('connection') and put it outside of this listener, the terminate thing doesn’t happen any more.

 const interval = setInterval(function ping() {
        wss.clients.forEach(function each(ws) {
            if (ws.isAlive === false) {
                logger.debug('terminating socket');
                return ws.terminate();
            }
            ws.isAlive = false;
            ws.ping('', false, true);
       });
    }, 30000);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js EPIPE error - Stack Overflow
So I've created a socket.io connection between the server and the client and a tcp connection between the server and the PLC. Any...
Read more >
what is a "write EPIPE" error? - Google Groups
I've done it a million times in the past but now I'm stuck on an error event. ... EPIPE means that writing of...
Read more >
Language Server Extension Guide - Visual Studio Code
Language Client: A normal VS Code extension written in JavaScript / TypeScript. This extension has access to all VS Code Namespace API. Language...
Read more >
Chapter 5. TCP Client/Server Example - Shichao's Notes
If we need to know which write caused the error, then we must either ignore the signal or return from the signal handler...
Read more >
typescript language server exited with error. error message is
closed first write finished { Error: write EPIPE at exports. ... seems to be a bit racy (this repro case causes it to...
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