Skip to content

Commit

Permalink
fix: ignore errors when forcefully closing the socket (#601)
Browse files Browse the repository at this point in the history
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 #596, #598
  • Loading branch information
darrachequesne committed Apr 15, 2020
1 parent 71ece3e commit dcdbccb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/server.js
Expand Up @@ -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]
Expand Down
21 changes: 21 additions & 0 deletions test/server.js
Expand Up @@ -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",
Expand Down

0 comments on commit dcdbccb

Please sign in to comment.