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.

How to handle ECONNRESET and EPIPE on streaming?

See original GitHub issue

Consider this example

const fs = require('fs');
const Koa = require('koa');

const app = new Koa();

app.use(async (ctx) => {
  ctx.body = fs.createReadStream('really-large-file');
});

app.listen(4000);

If user aborts request I’m getting either

  Error: read ECONNRESET
      at _errnoException (util.js:1024:11)
      at TCP.onread (net.js:618:25)

or

  Error: write EPIPE
      at _errnoException (util.js:1024:11)
      at WriteWrap.afterWrite [as oncomplete] (net.js:870:14)

What is the proper way to handle this type of errors?

P.S. I have no errors after request aborts with express

const fs = require('fs');
const express = require('express');

const app = express();

app.get('/', (req, res) => {
  fs.createReadStream('really-large-file').pipe(res);
});

app.listen(4000);

P.P.S. I’ve tried

app.use(async (ctx) => {
  fs.createReadStream('really-large-file').pipe(ctx.res);
  ctx.respond = false;
});

But it had no effect.

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
rvanmilcommented, Feb 1, 2019

I know this is an old issue but in case someone finds it and is unable to get things to work properly (koa-onerror did not work for me), here’s how I got this to work (koa 2.7.0). Looking at the onerror function here: https://github.com/koajs/koa/blob/8b4e2cd3bc6e165a0ea544686346cd79e437bc28/lib/context.js#L121 and here: https://github.com/koajs/koa/blob/8b4e2cd3bc6e165a0ea544686346cd79e437bc28/lib/context.js#L123-L128 once the headers are sent, nothing is done. But the error is emitted as an app-level error. So I created a handler for the app-level error event:

// Catch and log Koa app-level errors
const app = new Koa()
app.on('error', (error) => {
  if (error.code === 'EPIPE') {
    logger.warn('Koa app-level EPIPE error.', { error })
  } else {
    logger.error('Koa app-level error', { error })
  }
})

And now I’m no longer seeing the Error: write EPIPE messages 😊

4reactions
GuillaumeCiscocommented, Feb 26, 2018

I’m experiencing the same issue using webpackHotMiddleware with ssr. Every time I refresh the page in chrome, the error appears:

Error: read ECONNRESET
      at _errnoException (util.js:1003:13)
      at TCP.onread (net.js:623:25)

in the server log. As described in this thread, a close is not done correctly : https://github.com/websockets/ws/issues/1256 This thread deals with websocket, but we use event-stream here.

Is there a better way to handle this problem than just hiding the error with koa-onerror ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Koa.js and streaming. How do you handle errors?
1 Answer 1 ; const fs = require ; require('koa'); ; use(async ; catch (err) { ctx.status ; message; ctx.app ...
Read more >
node:internal/stream_base_commons - You.com | The AI ...
In this context, the EPIPE error probably means that you're trying to write to a socket that has been closed. Since the setInterval()...
Read more >
Issue 30319: Change socket.close() to ignore ECONNRESET
> 2. Ignore ECONNRESET in “socket.close” (backwards compatible, could use “os.close” if you really want to check for ECONNRESET) +1 ...
Read more >
Error Codes (The GNU C Library)
You can choose to have functions resume after a signal that is handled, rather than failing with EINTR ; see Primitives Interrupted by...
Read more >
TcpStream always terminates connections successfully (even ...
Some error here"); // or: std::process::exit(0); writeln!(writer, "We are done! ... you want the peer to see an ECONNRESET error rather than EPIPE...
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