Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

@fastify/reply-from - Reply was already sent, did you forget to "return reply" #965

Open
paymog opened this issue Nov 19, 2023 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@paymog
Copy link

paymog commented Nov 19, 2023

馃挰 Question here

I'm getting the following error in production in unpredictable ways

Reply was already sent, did you forget to "return reply" in "<my url>" (POST)?

Here is the declaration of the fastify handler

  fastify.post<ProxyMessageType>(
    "/:project_id/subgraphs/:name/:version_label/gn*",
    {
      schema: ProxyMessageSchema,
    },
    async (req, reply) => {
      const deployment = await req.deploymentCache.get(
        {
          projectId: req.params.project_id,
          name: req.params.name,
          versionLabel: req.params.version_label,
        },
        async () => req.goldskyDbClient,
        req.log
      );

      if (!deployment) {
        req.log.warn("deployment not found");

        reply.status(404);
        reply.send({
          error: "reach out to support pls",
        });
        return;
      }

      const subgraphUtils = getSubgraphUtils(deployment.project, req.opts);
      const subgraphProxyUrl = subgraphUtils
        .getSubgraphHttpApiUrl(deployment.deploymentId)
        .toString();

      const replyFromUrl = `${subgraphProxyUrl}${req.params["*"]}`;

      requestCounter.inc(
        {
          projectId: req.params.project_id,
          subgraphName: req.params.name,
          versionLabel: req.params.version_label,
        },
        1
      );
      const endTimer = requestGauge.startTimer({
        projectId: req.params.project_id,
        subgraphName: req.params.name,
        versionLabel: req.params.version_label,
      });

      return reply.from(replyFromUrl, {
        onResponse: (_, reply, res) => {
          res.on("end", () => {
            endTimer();
          });

          reply.send(res);
        },
      });
    }
  );

I see in the announcement doc that I should return reply if I call reply.send after the handler has finished. I suspect that's what I should do here but even when I switch to

      reply.from(replyFromUrl, {
        onResponse: (_, reply, res) => {
          res.on("end", () => {
            endTimer();
          });

          reply.send(res);
        },
      });
      return reply

I still see the warning log line get printed.

Your Environment

  • node version: 18
  • fastify version: ~4.23.2
  • os: Linux
  • fastify reply-from: ^9.4.0
@paymog paymog added the help wanted Extra attention is needed label Nov 19, 2023
@mcollina
Copy link
Member

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

@jv-castify
Copy link

I believe you need to also return the 404 response you have.

if (!deployment) {
  req.log.warn("deployment not found");

  return reply //   Return this statement.
    .code(404)
    .send({
      error: "reach out to support pls",
    });
}

Also this:

return reply.from(replyFromUrl, {
  onResponse: (_, reply, res) => {
    res.on("end", () => {
      endTimer();
    });

    return reply.send(res); // Return this statement.
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants