Skip to content

async preValidation hook does not abort request #1776

@tobspr

Description

@tobspr

🐛 Bug Report

When calling reply.send within an async preValidation hook, this does not abort the request and thus emits a Response has already been sent error. Related: #677

According to the linked issue and pr, it should have been fixed, however it doesn't seem to be (At least for me):

To Reproduce

Hook:

fastify.addHook("preValidation", async function (request, reply) {
  reply.code(403).send({error: "unauthenticated"});
});

Handler:

module.exports = function (fastify, opts, next) {
   fastify.get("/", {}, async (request, reply) => {
       // This gets still executed, and reply.sent is true here
   });
   next();
}

Expected behavior

The handler should not get called.

Your Environment

  • node version: 10.16.0
  • fastify version: >=2.5.0
  • os: Windows, Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions