Stream push() called after EOF has been emitted
Production Risk
Can crash stream pipelines; ensure stream termination logic is single-path.
Thrown when stream.push(null) has been called to signal the end of a readable stream, and then stream.push() is called again with additional data. Once null is pushed the stream is in an ended state and no more data can be added.
- 1Calling push(null) to end the stream and then pushing more data in a callback
- 2Race condition between an async data source and the stream end signal
- 3Logic error where stream termination and data production paths are not mutually exclusive
Triggered when push() is called on a Readable stream after push(null) has already been called.
const { Readable } = require('stream');
const r = new Readable({ read() {} });
r.push('data');
r.push(null); // EOF
r.push('more'); // throws ERR_STREAM_PUSH_AFTER_EOFexpected output
Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF
Fix
Track whether EOF has been pushed and guard accordingly
WHEN In custom Readable implementations with async data sources
class MyReadable extends Readable {
constructor() {
super();
this._done = false;
}
_read() {
if (this._done) return;
fetchChunk().then(chunk => {
if (!chunk) { this._done = true; this.push(null); return; }
this.push(chunk);
});
}
}Why this works
Using a flag ensures push(null) is called exactly once and no further pushes occur.
const { Readable } = require('stream');
const r = new Readable({ read() {} });
r.push('data');
r.push(null); // EOF
r.push('more'); // throws ERR_STREAM_PUSH_AFTER_EOF // this triggers ERR_STREAM_PUSH_AFTER_EOFtry {
// operation that may throw ERR_STREAM_PUSH_AFTER_EOF
riskyOperation()
} catch (err) {
if (err.code === 'ERR_STREAM_PUSH_AFTER_EOF') {
console.error('ERR_STREAM_PUSH_AFTER_EOF:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_stream_push_after_eof(...args) {
// validate args here
return performOperation(...args)
}✕ Push null and then push more data
Once EOF is signalled the stream is sealed; further pushes are illegal.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev