Skip to content

Fastify will not allow route registration after ready #1771

@areese159

Description

@areese159

🐛 Bug Report

Calling fastify.ready() causes errors to be thrown if you attempt to add routes after it finishes. Specifically, it will throw Cannot add route when fastify instance is already started! which seems like undesired behavior. This seems to be caused by Avvio emitting the start event when calling ready, since ready comes from Avvio. When start is emitted fastify appears to no longer allow route registration which seems to not be the correct behavior. From looking at the documentation on ready it appears this should not be a side effect and only the plugins should be loaded.

To Reproduce

const fastify = require("fastify");
const fp = require("fastify-plugin");

const plugin = fp(function(fastify, opts, next) {
    next();
});
const start = async function () {
    const app = fastify();
    app.register(plugin);
    await app.ready();
    // this will throw an error, specifically "Cannot add route when fastify instance is already started!"
    app.post('/test', function () {});
};
start();

Expected behavior

I would expect that fastify loads the plugins as expected but will continue to allow route registration until listen is called.

const fastify = require("fastify");
const fp = require("fastify-plugin");

const plugin = fp(function(fastify, opts, next) {
    next();
});
const start = async function () {
    const app = fastify();
    app.register(plugin);
    await app.ready();
    // this will not throw an error and fastify will allow you to register the route"
    app.post('/test', function () {});
};
start();

Your Environment

  • node version: 10.15.3
  • fastify version: >=2.6.0
  • os: Linux (Ubuntu)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionGeneral question about the projectstaleIssue or pr with more than 15 days of inactivity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions