HTTP/2 stream was cancelled before completion
Production Risk
Normal in production under load; must be handled to prevent process crashes.
Thrown when an HTTP/2 stream is cancelled because the remote peer sent a RST_STREAM frame with CANCEL error code, or because the session was torn down. This commonly occurs when a client navigates away or drops the connection mid-stream.
- 1Client disconnects or navigates away mid-stream
- 2Client sends RST_STREAM with CANCEL code
- 3Session timeout or network interruption
Triggered when the HTTP/2 session receives a RST_STREAM CANCEL from the remote peer.
const client = http2.connect('https://localhost:3000');
const req = client.request({ ':path': '/slow' });
req.destroy(); // sends RST_STREAM CANCEL to serverexpected output
Error [ERR_HTTP2_STREAM_CANCEL]: The pending stream has been canceled
Fix
Handle the stream error event gracefully
WHEN On the server for all HTTP/2 stream handlers
server.on('stream', (stream) => {
stream.on('error', (err) => {
if (err.code === 'ERR_HTTP2_STREAM_CANCEL') return;
console.error('Stream error:', err);
});
});Why this works
Silently discarding CANCEL errors prevents process crashes from normal client disconnects.
const client = http2.connect('https://localhost:3000');
const req = client.request({ ':path': '/slow' });
req.destroy(); // sends RST_STREAM CANCEL to server // this triggers ERR_HTTP2_STREAM_CANCELtry {
// operation that may throw ERR_HTTP2_STREAM_CANCEL
riskyOperation()
} catch (err) {
if (err.code === 'ERR_HTTP2_STREAM_CANCEL') {
console.error('ERR_HTTP2_STREAM_CANCEL:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_http2_stream_cancel(...args) {
// validate args here
return performOperation(...args)
}✕ Let stream cancel errors go unhandled
Unhandled error events on streams crash the Node.js process.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev