---
outline: deep
---

# 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

- [`packages/devframe/src/utils/streaming-channel.ts`](https://github.com/devframes/devframe/blob/main/packages/devframe/src/utils/streaming-channel.ts) — `stream.write()` throws `DF0031` when called after the stream has been closed, errored, or aborted by the consumer.
