Skip to content

Not working with fastify #32

@cjroebuck

Description

@cjroebuck

It doesn't seem to work with fastify (v5) anymore - just shuts down even though there are inflight requests.

Example server :

import Fastify from "fastify";
import GracefulServer from "@gquittet/graceful-server";
import { setTimeout } from "timers/promises";
let fastify = Fastify({ logger: true });

const gracefulServer = GracefulServer(fastify.server);

gracefulServer.on(GracefulServer.READY, () => {
  fastify.log.info("Server is ready");
});

gracefulServer.on(GracefulServer.SHUTTING_DOWN, () => {
  console.log("Server is shutting down");
});

gracefulServer.on(GracefulServer.SHUTDOWN, (error) => {
  console.log("Server is down because of", error.message);
});

// Declare a route
fastify.get("/", async (request, reply) => {
  await setTimeout(10000);
  return { hello: "world" };
});

// Run the server!
const start = async () => {
  try {
    await fastify.listen({ port: 3000 });
    fastify.log.info(
      `server listening on ${fastify.server.address()}, pid: ${process.pid}`
    );
    gracefulServer.setReady();
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

How to recreate:

run the example server node example.js

run a curl to http://127.0.0.1:3000 curl -v http://127.0.0.1:3000/

note that the log shows an incoming request (and it should be waiting for a response for 10 seconds).

now immediately either ctrl+c on the server, or send a SIGTERM to the process' pid: kill -15 $PID

The server logs 'Server is shutting down', followed by 'Server is down because of SIGTERM' and it shuts down immediately.

The curl request gets cut and replies with curl: (52) Empty reply from server.

Expected - the server should wait for the inflight request to finish (after 10s) and then shutdown. The curl command should return 200.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions