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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: aborted, code: 'ECONNRESET' #207

Closed
beeequeue opened this issue Oct 17, 2024 · 6 comments
Closed

Error: aborted, code: 'ECONNRESET' #207

beeequeue opened this issue Oct 17, 2024 · 6 comments

Comments

@beeequeue
Copy link
Contributor

beeequeue commented Oct 17, 2024

We're seeing a bunch of errors that seem to be happening because the socket on IncomingMessage is closing, and I have no idea where it could be coming from except this library

Is there some check that should be added somewhere either in this library or outside it to handle these errors?
I found #107 but that was a while ago and we're still seeing it on the latest version

I have not managed to reproduce these errors once locally which makes it pretty much impossible to debug 😢

Screenshot of all our logs say (bottom-to-top)

image

@usualoma
Copy link
Member

Hi @beeequeue, Thanks for the report.

ECONNRESET occurs when the connection is interrupted during a request. The cause is generally a network error, so there is no way to avoid it on hono's side.

Since it is captured as an error on hono, we can handle it with onError.

app.onError((err, c) => {
    console.error(err)
    return c.text('Internal Server Error', 500)
})

@beeequeue
Copy link
Contributor Author

We are still getting these uncaught errors after adding this to our application:

app.onError((error, c) => {
  if ("status" in error) {
    const statusCode = error.status ?? 500
      return c.json(
      {
        error: true,
        status: statusCode,
        message: error.message,
      },
      statusCode,
    )
  }

  logger.error({ error }, "Unhandled error")
  return c.json({ error: true, status: 500, message: "Internal Server Error" }, 500)
})

@beeequeue beeequeue reopened this Oct 18, 2024
@beeequeue
Copy link
Contributor Author

beeequeue commented Oct 21, 2024

I wish I could provide more to help like a reproduction but I have tried so many things and have never been able to create an aborted or ECONNRESET error locally.

@beeequeue
Copy link
Contributor Author

I have not been able to work around this issue

I have added error and close event listeners to:

  • the server returned by serve()
  • the Sockets created by connection and secureConnection events on the server
  • c.env.incoming

and not a single one have been called once despite the entire server crashing with the ECONNRESET error

We have had to resort to using process.on("uncaughtException") as that's the only thing that stops the server from crashing

@alumowa
Copy link
Contributor

alumowa commented Oct 24, 2024

I have not been able to work around this issue

I have added error and close event listeners to:

  • the server returned by serve()
  • the Sockets created by connection and secureConnection events on the server
  • c.env.incoming

and not a single one have been called once despite the entire server crashing with the ECONNRESET error

We have had to resort to using process.on("uncaughtException") as that's the only thing that stops the server from crashing

Hi all (I'm new here 😄)

This is one way to reproduce the connection reset errors locally (resets connection after 1 second):

import net from "node:net";
import { serve } from "@hono/node-server";
import App from "./App";

const server = serve(App, (info) => {
	console.log(`Server running on http://localhost:${info.port}`);
});

server.on("clientError", (error) => {
	// error here should be a ECONNRESET
	console.log(error);
});

function createSocketConnection(host: string, port = 3000) {
	const client = new net.Socket();

	client.connect(port, host, () => {
		console.log(`Client connected to ${host}:${port}`);
	});

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

	client.on("error", (err) => {
		console.error("Client Error:", err);
	});

	setTimeout(() => {
		// Ref: https://nodejs.org/docs/latest-v20.x/api/net.html#socketresetanddestroy
		client.resetAndDestroy();
	}, 1000);
}

createSocketConnection("127.0.0.1");

What you want to do in order to catch this server level error is to add the handler to the server object itself.

I don't see where this event is linked to Hono's onError handler, so that explains why it does not trigger it. Good luck hunting down the bug!

@beeequeue
Copy link
Contributor Author

While it creates a ECONNRESET error, it does not crash the server if we don't handle it which is the situation we're currently in. 🙁

We have also contacted AWS support about this, as we discovered all our services started encountering this issue on the same day (Oct 16th) and 2 of them are still using Koa instead of Hono.

@beeequeue beeequeue closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants