Skip to content

Streams and connections

Streams buffer data. An unread or unclosed stream keeps buffers in external memory.

examples/node/05-stream-leak.js — creates readable streams and never destroys them.

examples/node/05-stream-fixed.jsstream.destroy(), pipeline() with error handling.

import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
await pipeline(
createReadStream('in.dat'),
createWriteStream('out.dat')
);
// auto-cleanup on completion or error
// Broken — new pool per request
app.get('/users', async (req, res) => {
const pool = createPool(config);
const rows = await pool.query('SELECT ...');
res.json(rows);
// pool never closed
});

Use one shared pool per process; close on SIGTERM.

Terminal window
node examples/node/05-stream-leak.js
node examples/node/05-stream-fixed.js