Skip to content

DF0031: Write to Closed Stream

Message

Cannot write to closed stream "{channel}#{id}".

Cause

stream.write(chunk) was called after the stream was closed via stream.close() / stream.error() or after the consumer cancelled (which aborts stream.signal).

Fix

Producers should poll stream.signal.aborted and exit cleanly:

ts
const stream = channel.start({ id })
try {
  for (const chunk of source) {
    if (stream.signal.aborted)
      return
    stream.write(chunk)
  }
  stream.close()
}
catch (err) {
  stream.error(err)
}

Source

Released under the MIT License.