A 503 that could not happen

We shipped a change that signs our render links with a secret held in the server environment instead of a value stored in the database. It has a guard: if the secret is missing, refuse to issue a link rather than fall back to something weaker. The guard returns 503.

Then production returned a 503 for a request that should never have reached that guard — and the body attached to it belonged to a different response entirely.

POST /v1/render-link     (invalid API key)

HTTP/2 503
{"error":"unauthorized","message":"Invalid API key"}

A 503 status carrying a 401 body. That combination is not supposed to be constructible. This is how it happened, including the four attempts to reproduce it that all failed, and the Fastify behaviour underneath.

Reading the mismatch

The body is unambiguous: "Invalid API key" is the string our authentication middleware sends. Our global error handler has its own wording for a missing key — "Missing API key. Send it in the x-api-key header." — so there was no doubt which code produced the body. Authentication ran, rejected the request, and replied.

The status was the problem. reply.code(503) appears exactly once in our entire codebase: inside the route handler, in the guard described above. For the status to be 503, that line had to execute. Which meant the handler ran after the middleware had already sent a reply.

Fastify does not do that. Replying from a hook halts the chain. It is in the documentation, in a sentence that leaves no room:

Replying from a hook implies that the hook chain is stopped and the rest of the hooks and handlers are not executed. [...] If the hook is async, reply.send() must be called before the function returns or the promise resolves, otherwise, the request will proceed.

Our middleware calls reply.send() synchronously, well before the function returns. By the documentation, the chain stops.

A better probe than the first one

Before chasing the framework, we wanted a cheaper test of the claim "the handler ran". The handler's first statement is a validation check that returns 400 when the request body has no url and no html, several lines before the 503 guard. So: send an invalid key with an empty body. If the handler is not running, the response is the middleware's 401. If it is running, the status becomes 400.

invalid key + {"url": "..."}   ->  503   (the signing guard)
invalid key + {}               ->  400   (the body check, earlier in the handler)

Both carried the 401 body. The status tracked exactly how far into the handler execution got. The handler was running, and the mismatch was it overwriting the status of a response already on its way out.

Four reproductions that failed

Knowing what was happening did not explain why, and nothing is fixable until it reproduces. Four attempts, in increasing fidelity:

  1. A synthetic app — an async hook that sends and returns, a handler that sets a different status. Handler skipped. Status 401. Correct.
  2. The real middleware and the real route, with the database and Redis mocked. Correct.
  3. Over a real socket rather than Fastify's inject(), in case the in-process path differed. Correct.
  4. The compiled output — production runs dist/ from tsc, not TypeScript through a test runner — against a real Redis and a real SQLite file. Still correct.

Four environments, none reproducing a thing that happened on every single request in production. At that point the useful question stops being "why is the framework misbehaving" and becomes "what is still different about my harness".

The difference

Every probe had registered the route in isolation. Production registers it inside an application that also sets up CORS, Swagger, metrics and logging. So we ran the actual compiled application — the real dist/index.js, nothing stubbed but the network it talks to.

bogus key + no url  ->  status=400  body={"error":"unauthorized",...}

Reproduced. From there it was a bisect. Not CORS. Not Swagger. Not the metrics hooks. The reproduction needed the request-id hook and the 5xx alerting hook — and the only thing those two have in common is that both are onSend.

Two hooks

Stripped of our code entirely, it is this:

import Fastify from 'fastify'

async function run (onSendCount) {
  const app = Fastify()
  let handlerRan = false

  for (let i = 0; i < onSendCount; i++) {
    app.addHook('onSend', async (req, reply, payload) => payload)
  }

  app.get('/', {
    preHandler: async (req, reply) => {
      reply.code(401).send({ error: 'unauthorized' })
    },
    handler: async (req, reply) => {
      handlerRan = true
      return reply.code(400).send({ error: 'handler ran' })
    }
  })

  const res = await app.inject({ method: 'GET', url: '/' })
  await app.close()
  console.log(`onSend=${onSendCount} handlerRan=${handlerRan} status=${res.statusCode}`)
}

for (const n of [0, 1, 2, 3]) await run(n)
onSend hooksHandler skipped?Status
0yes401 — correct
1yes401 — correct
2no400
3no400

Fastify 5.12.5, Node 22. Adding a second onSend hook changes whether replying from a hook halts the request.

Narrowing further, both halves matter. It needs the replying hook to be async and at least two async onSend hooks. Callback-style onSend hooks do not trigger it; neither does the same number of onRequest or preValidation hooks. And it is not specific to preHandler — with two async onSend hooks registered, replying from onRequest, preParsing, preValidation or preHandler all let the handler run.

What it meant for us, stated plainly

Our application registers exactly two onSend hooks. So every authenticated route was running its handler for requests that authentication had already rejected.

The impact was bounded, and the bound was luck rather than design. Handlers dereference the authenticated key within a few lines and throw when it is missing, and the resulting error reply is discarded because a response was already sent. Nothing was authorised, no capture ran, no quota moved, and no client ever received a success body it should not have. But any statement before that first dereference did execute on rejected requests, and that is not a property anyone should be relying on.

It was invisible for the same reason. The body a client receives is always the correct rejection. Only the status changes, and only on a handler that sets one before touching the key. Exactly one of our routes does that. Our other endpoints either reject at schema validation before any handler runs, or throw before setting a status. It took a missing environment variable, producing a conspicuous 503 on precisely that route, to expose any of it.

The fix is one line per rejection

Return the reply instead of sending and returning nothing:

// before
reply.code(401).send({ error: 'unauthorized' })
return

// after
return reply.code(401).send({ error: 'unauthorized' })

This is Fastify's documented idiom, it costs nothing, and it halts correctly regardless of how many onSend hooks exist.

One place deliberately did not get the change. Our rate limiters are helpers called from inside handlers that return a boolean, and their callers branch on it:

if (!(await ipRateLimit(reply, opts))) return reply

Returning the reply object from those is truthy, which inverts the test and lets rate-limited requests straight through. The first pass of the fix did exactly that, mechanically, across every reply.code(...).send(...) in the middleware directory. It was caught before committing, and the code now carries a comment explaining why those two functions are different. A fix applied by pattern-match is still a change you have to read.

The test registers two onSend hooks on purpose

The regression test builds an application with both hooks, because without them it passes against the broken code and proves nothing. We checked that directly: reverting a single rejection branch makes it fail, and restoring the fix makes it pass. A test you have not watched fail is a test you do not know the meaning of.

It is older than we thought, and it used to be loud

The original report called this 5.12.5 behaviour, because that is the version we were on. Running the reproduction against every major since fastify@4.0.0 says otherwise: the halting behaviour is identical all the way back. Two async onSend hooks have always been enough to let the handler run.

What did change is the symptom, and the boundary is exact:

VersionHandler halts?What you see
4.0.0 – 5.10.0NoUncaught ERR_HTTP_HEADERS_SENT, process exits 1
5.11.0 – 5.12.5NoNothing. The status is overwritten and the process is fine.

Before 5.11.0, the handler writing to an already-sent reply threw out of a promise and took the process down. From 5.11.0 the throw is gone and the chain still runs.

That is the part worth sitting with. A crash is a bug report on its first occurrence. A silently wrong status code is a thing you run for weeks. The fix that removed the noise is the reason this survived in our production long enough to be confusing — and we only found it because a status code disagreed with its own body, which is the one trace it still leaves.

The reproduction and the full version matrix are in the repository at ops/fastify-7090/.

Reported upstream

Filed as fastify/fastify#7090 with the minimal reproduction, the narrowing matrix and the list of affected hooks.

Checked 2026-11-25, and the answer arrived. A maintainer replied on 24 October with one line — “This is an application bug to me” — and a diff putting return in front of the reply.code(401).send(...). The test-only pull request that pinned the two-hook boundary (#7129) was closed on 14 November without comment. The issue itself is still open.

We are not going to argue with that. It is our bug, by the only definition that matters: our line, our outage. The update this post needs is not a rebuttal, it is the reason the line was written that way, and that reason is still sitting in the documentation. docs/Reference/Hooks.md reads, today:

“If the hook is async, reply.send() must be called before the function returns or the promise resolves, otherwise, the request will proceed. When reply.send() is called outside of the promise chain, it is important to return reply otherwise the request will be executed twice.”

Read it the way someone writing a hook reads it. Calling reply.send() inside the async function is calling it before the promise resolves, so the first sentence says you are done. return reply is then introduced for the other situation — sending from outside the promise chain — and the example directly beneath it is a setImmediate, which is exactly that other situation. Our handler was the first case. The first case is the one that does not work.

Why it does not work was then explained by two people who are not us, and that exchange is worth more than anything above it. Reply implements then, so returning or awaiting it waits for the response stream to actually end. A bare reply.send() returns Promise.resolve(undefined) instead, and the hook runner advances on the next microtask tick. reply.sent is just raw.writableEnded, which is set only after the last onSend hook has run. With zero or one onSend hooks the response has ended by that tick and the chain halts. With two, the chained onSend promises need more ticks than the hook did, writableEnded is still false when preHandlerCallback tests it, and the handler runs (SameRainbows’ walk-through, with a table across 0–3 hooks and all three calling styles).

That is the sentence we were missing in October: the halt is inferred from a fact about the response instead of being carried as a fact about the control flow. Everything odd about this bug follows from it — why a hook in an unrelated file changes the semantics of a hook in this one, why the fix is per-rejection rather than central, and why it went quiet at 5.11.0 rather than being fixed there. Someone else in the thread had shipped the same defect in a different HTTP framework’s filter chain, produced the identical symptom, and settled it by tracking the halt on the chain state explicitly. A response fact and a control-flow fact are not the same fact, and conflating them is what makes the outcome depend on a registration count.

So: closed as code, and we think correctly. Unresolved as documentation, where the sentence that sent us down this road is still live at Hooks.md lines 346–350 as of 5.12.8. If you only take one thing from this post, take the rule rather than the diagnosis: in an async hook, return reply or await it, every time, regardless of what else is registered. It costs six characters and it is unconditionally correct.

Screenshot, PDF and video capture API

100 free captures a month, no card. The engineering above is the kind we write up rather than hide.

Get a free API key

Three things worth keeping

A status code that disagrees with its body is a lifecycle problem. Nothing else produces that shape. Two pieces of code both believed they owned the response, and reading the body told us which one actually sent it.

"Cannot reproduce" is a statement about your harness. Four increasingly faithful environments all said the bug did not exist while it was happening on every production request. The move that worked was not thinking harder about the framework; it was enumerating what the harness still did not have, and running the real application.

Documented behaviour is still worth verifying. The documentation says replying from a hook stops the chain. It does — under a condition the documentation does not mention, which nothing in our code could have told us about.

Related reading