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

Memory leak with hono, @hono/node-server, @hono/graphql-server #2816

Closed
kyscott18 opened this issue May 27, 2024 · 2 comments
Closed

Memory leak with hono, @hono/node-server, @hono/graphql-server #2816

kyscott18 opened this issue May 27, 2024 · 2 comments
Labels

Comments

@kyscott18
Copy link

kyscott18 commented May 27, 2024

What version of Hono are you using?

4.3.10

What runtime/platform is your app running on?

Node

What steps can reproduce the bug?

import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { graphqlServer } from "@hono/graphql-server";
import { buildSchema } from "graphql";

setInterval(() => {
  const before = process.memoryUsage().heapUsed / 1024 / 1024;
  global.gc?.();
  const after = process.memoryUsage().heapUsed / 1024 / 1024;
  console.log({ before: before.toFixed(2), after: after.toFixed(2) });
}, 1_000);

export const app = new Hono();

const schema = buildSchema(`
type Query {
  hello: String
}
`);

const rootResolver = (ctx) => {
  return {
    hello: () => "Hello Hono!",
  };
};

app.use(
  "/graphql",
  graphqlServer({
    schema,
    rootResolver,
  })
);

serve(app);

What is the expected behavior?

No response

What do you see instead?

Result of running the server and then sending requests with autocannon after a few seconds:

autocannon -c 100 -d 10 -m POST -H "Content-Type: application/json" -b '{"query":"{ hello }"}' http://localhost:3000/graphql
(base) kylescott@Kyles-MacBook-Pro-2 hono-node-repro % node --expose-gc index.js
{ before: '8.86', after: '6.72' }
{ before: '6.83', after: '6.75' }
{ before: '6.77', after: '6.75' }
{ before: '6.77', after: '6.75' }
{ before: '6.82', after: '6.76' }
{ before: '6.78', after: '6.76' }
{ before: '6.83', after: '6.77' }
{ before: '55.64', after: '37.63' }
{ before: '153.87', after: '97.79' }
{ before: '205.73', after: '152.70' }
{ before: '262.65', after: '208.79' }
{ before: '310.95', after: '259.03' }
{ before: '355.99', after: '308.32' }
{ before: '400.77', after: '354.94' }
{ before: '444.92', after: '398.86' }
{ before: '489.06', after: '443.80' }
{ before: '523.01', after: '484.06' }
{ before: '565.09', after: '523.40' }

Additional information

https://github.com/kyscott18/hono-node-mem-leak

Pretty sure the memory for the request body is what is being leaked, not sure if it is related to GraphQL at all. I was able to fix this issue locally by removing the call to Readable.toWeb() in this file. https://github.com/honojs/node-server/blob/main/src/request.ts#L67

@kyscott18 kyscott18 added the bug label May 27, 2024
@usualoma
Copy link
Member

@kyscott18
Thank you!
Oh, this needs to be fixed. I will check.

@yusukebe
Copy link
Member

yusukebe commented Jun 5, 2024

This was fixed by honojs/node-server#172. Thanks!

@yusukebe yusukebe closed this as completed Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants