write after end stream error
Asked Answered
M

2

6

I work with NodeJS Transform stream that reads data from a Readable flow stream then converts it.

When I destroy the Transform stream sometimes I get an error because the destroy function unpipes the Transfrom stream itself but do this async and if it's not quick enough the Readable stream pushes some new data during the disposing, that causing the following error:

Uncaught Error: write after end

Do I do something wrong, you write your code like unpipe before destroying, or how should I make sure the readable does not push data after I called the destroying and do not get any error?

Mailman answered 12/4, 2018 at 19:2 Comment(0)
C
4

Using node v8.11.1 I can replicate this problem with Readable when attempting to destroy() a stream from inside the on("data", (chunk) => void) callback. The errors disappeared when I deferred the call.

// Calling stream.destroy() directly causes "Error: write after end".
Promise.resolve()
  .then(() => stream.destroy())
  .catch(console.log)
Cantilena answered 1/5, 2018 at 4:12 Comment(0)
P
3

A similar error due to closing the stream abruptly in the on handler is

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed

The solution for both errors is to defer the destroy() call to the next tick:

process.nextTick(() => stream.destroy());
Pusillanimity answered 21/12, 2018 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.