fix: ignore errors when forcefully closing the socket (#601)

In order to catch the following errors:

```
events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:154:25)
    at writeGeneric (internal/stream_base_commons.js:145:3)
    at Socket._writeGeneric (net.js:780:11)
    at Socket._write (net.js:792:8)
    at doWrite (_stream_writable.js:441:12)
    at writeOrBuffer (_stream_writable.js:425:5)
    at Socket.Writable.write (_stream_writable.js:316:11)
    at abortConnection (<myproject>/node_modules/engine.io/lib/server.js:506:12)
    at <myproject>/node_modules/engine.io/lib/server.js:353:7
    at Server.verify (<myproject>/node_modules/engine.io/lib/server.js:158:14)
    at Server.handleUpgrade (<myproject>/node_modules/engine.io/lib/server.js:351:8)
```

Closes https://github.com/socketio/engine.io/issues/596, https://github.com/socketio/engine.io/pull/598
This commit is contained in:
Damien Arrachequesne
2020-04-15 11:42:31 +02:00
committed by GitHub
parent 71ece3ebf6
commit dcdbccb3dd
2 changed files with 24 additions and 0 deletions

View File

@@ -503,6 +503,9 @@ function sendErrorMessage(req, res, code) {
*/
function abortConnection(socket, code) {
socket.on("error", () => {
debug("ignoring error from closed connection");
});
if (socket.writable) {
const message = Server.errorMessages.hasOwnProperty(code)
? Server.errorMessages[code]

View File

@@ -802,6 +802,27 @@ describe("server", function() {
});
});
it("should abort connection when upgrade fails", done => {
listen({ allowUpgrades: true }, port => {
const req = http.request(
{
port,
path: "/engine.io/",
headers: {
connection: "Upgrade",
upgrade: "websocket"
}
},
res => {
expect(res.statusCode).to.eql(400);
res.resume();
res.on("end", done);
}
);
req.end();
});
});
it(
"should trigger if a poll request is ongoing and the underlying " +
"socket closes, as in a browser tab close",