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

fix: memory leak by AbortController #172

Merged
merged 4 commits into from
May 30, 2024

Conversation

usualoma
Copy link
Member

Fixes #171, honojs/hono#2816

Two issues needed to be corrected in this issue.

incoming.destroyed is true for all POST requests

The documentation says "Deprecated. check request.destroyed instead."

CleanShot 2024-05-30 at 05 17 02@2x

However, incoming was flagged for “autodestroy" and incoming.destroy() was always called when the request body was read to the end. In other words, .abort() was always called in POST requests. POST requests complete successfully, so there are no major problems, but I think it was not a good behaviour.

https://github.com/nodejs/node/blob/c0c598d753d5ce7e1721215c6e6254dc02c023a8/lib/internal/streams/readable.js#L1690-L1716

As errored seemed more suitable for our application, I decided to use it.

.abort() (call without arguments) causes memory leak

The real problem of memory leaks is here, which was an interesting situation.

Calling abort() with no arguments uses an instance of DOMException, but (presumably) this object is referenced including its children and grandchildren, so it appears that a cycle of references occurs and a instance of AbortController cannot be freed.

https://github.com/nodejs/node/blob/c0c598d753d5ce7e1721215c6e6254dc02c023a8/lib/internal/abort_controller.js#L395-L397
https://github.com/nodejs/node/blob/c0c598d753d5ce7e1721215c6e6254dc02c023a8/lib/internal/abort_controller.js#L368-L378

For "reason", not only for DOMException, it seems to be necessary to pass a string, as specifying an Object seems to cause a memory leak.

tsconfig.json

In order to use FinalisationRegistry in server.test.ts, the settings in tsconfig.json needed to be reflected in server.test.ts, so tsconfig.json was changed.

32c09a9

@usualoma
Copy link
Member Author

@yusukebe Would you please review?

@usualoma
Copy link
Member Author

node --expose-gc is required for executing global.gc?.()

@kyscott18
Copy link

node --expose-gc is required for executing global.gc?.()

yes

@kyscott18
Copy link

This branch fixed my original bug! Thank you!

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

Hi @usualoma !

Thank you for the fix and the test is great! I'll merge this now.

@yusukebe yusukebe merged commit ba26b94 into honojs:main May 30, 2024
4 checks passed
@usualoma usualoma deleted the fix/abort-controller-memory-leak branch May 31, 2024 01:06
nicolewhite pushed a commit to autoblocksai/cli that referenced this pull request Jun 4, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@hono/node-server](https://togithub.com/honojs/node-server) |
[`1.11.1` ->
`1.11.2`](https://renovatebot.com/diffs/npm/@hono%2fnode-server/1.11.1/1.11.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@hono%2fnode-server/1.11.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@hono%2fnode-server/1.11.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@hono%2fnode-server/1.11.1/1.11.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@hono%2fnode-server/1.11.1/1.11.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/node-server (@&#8203;hono/node-server)</summary>

###
[`v1.11.2`](https://togithub.com/honojs/node-server/releases/tag/v1.11.2)

[Compare
Source](https://togithub.com/honojs/node-server/compare/v1.11.1...v1.11.2)

#### What's Changed

- ci: include tests for node 22 version by
[@&#8203;Jayllyz](https://togithub.com/Jayllyz) in
[honojs/node-server#170
- fix: memory leak by AbortController by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[honojs/node-server#172

#### New Contributors

- [@&#8203;Jayllyz](https://togithub.com/Jayllyz) made their first
contribution in
[honojs/node-server#170

**Full Changelog**:
honojs/node-server@v1.11.1...v1.11.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNzcuOCIsInVwZGF0ZWRJblZlciI6IjM3LjM3Ny44IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

Memory leak with hono, @hono/node-server, @hono/graphql-server
3 participants