Skip to content

Cannot listen on a port after closing the server. Error is misleading. #2411

@simihartstein

Description

@simihartstein

🐛 Bug Report

I am trying to temporarily swap out my main fastify server instance with a maintenance server while I handle some background processes.
When I try to swap back to my main server that was previously closed, it throws an error saying Fastify is already listening

I'm not sure if this is a bug or not supported since the documentation is unclear.

I have already worked around this by migrating the maintenance logic to the prehandler. I'm just looking for some clarification on the expected behavior or a clearer error message.

To Reproduce

Here is a minimal example to demonstrate:

const create= require("fastify");

const mainServer = create();
await mainServer.listen(8080, "0.0.0.0");

const maintenanceServer = create();
await mainServer.close();
await maintenanceServer.listen(8080, "0.0.0.0");

await maintenanceServer.close();
await mainServer.listen(8080, "0.0.0.0");
// throws an error with message 'Fastify is already listening'

Expected behavior

Either mainServer starts listening on port 8080 or a message saying that the fastify instance has already been closed and cannot be reopened.

Your Environment

  • node version: 12.16.1
  • fastify version: >=2.3.0
  • os: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions