0

Our online service will throw ECONNRESET from time to time, so I want to reproduce this error. After googling and stackoverflowing, I know ECONNRESET may occur when another peer abruptly closes the socket, meantime current peer is still reading data. The complete error message our online service got is:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20)

What I tried:

// server.ts
import net, { Socket } from "net";

const chunks = [];

const server = net.createServer((stream: Socket) => {
  stream.on("data", data => {
    console.log(data.byteLength); // still reading data
  });

  stream.on("error", e => console.log(e));
});

server.listen("8080", () => {
  console.log("The server is listening to 8080");
});
server.on("error", e => {
  console.log(e);
})
// client.ts
import net from "net";

const options = {
  port: 8080,
};

const client = net.connect(options, () => {
  console.log("connected!");
  client.write(Buffer.alloc(1024 * 1024 * 1024)); // big buffer so server will need to consume for a long time
  setTimeout(() => {
    client.destroy(); // abruptly close the socket
  }, 0);
});

However, no error is thrown. Is there a way to forge an ECONNRESET?

crazyones110
  • 296
  • 3
  • 18
  • I got the idea "abruptly close the socket" from this [answer](https://stackoverflow.com/a/17637900/12644421) – crazyones110 Sep 03 '21 at 18:15
  • You're mistaken about the cause, for a start. The per merely closing the socket while you're reading will merely cause a normal end of stream. There are several causes, as listed in your link, including the comments, which gives a hint as to how to simulate it: have the peer close the socket *without reading what has been sent to it.* NB I'm not fond of the term 'abruptly close', The RFCs use 'abortive close', which again can be performed in a number of ways, but mere 'abruptness' is not one of them. – user207421 Sep 04 '21 at 01:28

0 Answers0