HTTP/2 trailers already sent on this stream
Production Risk
Low — only affects servers using HTTP/2 trailers.
Thrown when stream.sendTrailers() is called more than once on the same HTTP/2 stream. Trailing headers can only be sent once per stream; a second call violates the protocol.
- 1Calling stream.sendTrailers() twice on the same stream
- 2Middleware that sends trailers and also delegates to a handler that sends them
Triggered when sendTrailers() is called on a stream that has already sent its trailing headers.
server.on('stream', (stream) => {
stream.respond({ ':status': 200 }, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ 'x-timing': '10ms' });
stream.sendTrailers({ 'x-extra': 'oops' }); // throws
});
stream.end('body');
});expected output
Error [ERR_HTTP2_TRAILERS_ALREADY_SENT]: Trailing headers have already been sent
Fix
Call sendTrailers() exactly once inside the wantTrailers handler
WHEN Always — send all trailing headers in a single call
stream.on('wantTrailers', () => {
stream.sendTrailers({ 'x-timing': '10ms', 'x-extra': 'value' });
});Why this works
Combining all trailers into one call respects the single-invocation constraint.
server.on('stream', (stream) => {
stream.respond({ ':status': 200 }, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ 'x-timing': '10ms' });
stream.sendTrailers({ 'x-extra': 'oops' }); // throws
}); // this triggers ERR_HTTP2_TRAILERS_ALREADY_SENTtry {
// operation that may throw ERR_HTTP2_TRAILERS_ALREADY_SENT
riskyOperation()
} catch (err) {
if (err.code === 'ERR_HTTP2_TRAILERS_ALREADY_SENT') {
console.error('ERR_HTTP2_TRAILERS_ALREADY_SENT:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_http2_trailers_already_sent(...args) {
// validate args here
return performOperation(...args)
}✕ Call sendTrailers() more than once per stream
Only one trailing HEADERS frame is allowed per HTTP/2 stream.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev