Skip to content

fix(server): subscription memory leak - #6161

Merged
KATT merged 11 commits into
nextfrom
fix/6156--subscription-memory-leak
Oct 29, 2024
Merged

KATT merged 11 commits into
nextfrom
fix/6156--subscription-memory-leak

Conversation

@oleasteo

Copy link
Copy Markdown
Contributor

Closes #6156

@oleasteo
oleasteo requested a review from a team as a code owner October 29, 2024 15:14
@railway-app

railway-app Bot commented Oct 29, 2024

Copy link
Copy Markdown

This PR was not deployed automatically as @oleasteo does not have access to the Railway project.

In order to get automatic PR deploys, please add @oleasteo to the project inside the project settings page.

@vercel

vercel Bot commented Oct 29, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-prisma-starter ✅ Ready (Inspect) Visit Preview Oct 29, 2024 3:55pm
og-image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 29, 2024 3:55pm
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 29, 2024 3:55pm

Comment on lines +347 to +348
// free up references for garbage collection
next = null;

@KATT KATT Oct 29, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is set on next iteration.... suuurely it's not needed

Suggested change
// free up references for garbage collection
next = null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this one should be removable thoughhh

}

const result: TRPCResultMessage<unknown>['result'] = {
result = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks unnecessary - if it's in the loop instead it should be GC'd, no?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see your comment "We need those declarations outside the loop for garbage collection reasons."

can you explain why?

it's within a closure... it should be freed automatically?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, no. I thought so, too. Until I had those test cases proving me wrong.

I mentioned this in nodejs/node#55585. It seems like a const xyz = ... inside a loop is not freed up at the end of the closure, but only when the same statement is reached again. I assume this to be some v8 optimization or bug. Maybe the node devs will tell me within that issue I opened on their side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Those cases are not as severe as the original reported issue, but still we'd have a dangling reference to the latest value. Seems to be a pretty easy error to have in nodejs, as even a yield* does have this issue itself xD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh wow, TIL!

@KATT KATT changed the title Fix subscription memory leak fix(server): subscription memory leak Oct 29, 2024
@KATT
KATT merged commit 5a96855 into next Oct 29, 2024
@KATT
KATT deleted the fix/6156--subscription-memory-leak branch October 29, 2024 16:31
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 31, 2024

This branch was successfully deployed

5 active deployments
Preview – www — bd67b2a6 Deployed Oct 29, 2024 by vercel[bot]
Preview – trpc-next-app-dir — bd67b2a6 Deployed Oct 29, 2024 by vercel[bot]
Preview – next-prisma-starter — bd67b2a6 Deployed Oct 29, 2024 by vercel[bot]
Preview – examples-todomvc — bd67b2a6 Deployed Oct 29, 2024 by vercel[bot]
Preview – og-image — bd67b2a6 Deployed Oct 29, 2024 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: memory leak in trpc 11 subscriptions.

2 participants