ERR_HTTP2_STREAM_CANCEL
Node.jsWARNINGNotableHTTP/2HIGH confidence

HTTP/2 stream was cancelled before completion

Production Risk

Normal in production under load; must be handled to prevent process crashes.

What this means

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.

Why it happens
  1. 1Client disconnects or navigates away mid-stream
  2. 2Client sends RST_STREAM with CANCEL code
  3. 3Session timeout or network interruption
How to reproduce

Triggered when the HTTP/2 session receives a RST_STREAM CANCEL from the remote peer.

trigger — this will error
trigger — this will error
const client = http2.connect('https://localhost:3000');
const req = client.request({ ':path': '/slow' });
req.destroy(); // sends RST_STREAM CANCEL to server

expected 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

Handle the stream error event gracefully
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.

Code examples
Triggerjs
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_CANCEL
Handle in try/catchjs
try {
  // 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
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_http2_stream_cancel(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Let stream cancel errors go unhandled

Unhandled error events on streams crash the Node.js process.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors