Skip to content

Retry handler destroys the socket on error status code #4222

@fatal10110

Description

@fatal10110

Bug Description

On getting status code >= 400 from serever, retry handler throws an error which closes the open connection

Reproducible By

you will see here

New connection
connection closed
New connection
Hello World

const fastify = require("fastify");
const { RetryAgent, Agent } = require("undici");

const app = fastify();

let tries = 0;

app.get("/", (req, res) => {
  if (++tries % 2 !== 0) {
    return res.status(500).send("Test error");
  }

  res.send("Hello World");
});

app.server.on("connection", (socket) => {
  console.log("New connection");

  socket.on("close", () => {
    console.log("connection closed");
  });
});

async function start() {
  await app.listen({ port: 3000 });

  const a = new RetryAgent(new Agent({ connections: 1 }));
  const res = await a.request({
    origin: "http://localhost:3000",
    path: "/",
    method: "GET",
  });
  console.log(await res.body.text());
}
start();

Expected Behavior

It will pass the error but will leave the connection open if its non-connection related error

Additional context

By destroying the socket it also prevents the user get the error body from response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions