How to handle ECONNRESET and EPIPE on streaming?
See original GitHub issueConsider 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:
- Created 6 years ago
- Reactions:10
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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-levelerror
event:And now I’m no longer seeing the
Error: write EPIPE
messages 😊I’m experiencing the same issue using
webpackHotMiddleware
with ssr. Every time I refresh the page in chrome, the error appears: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 ?