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 #1431

Merged
merged 5 commits into from
Sep 20, 2024
Merged

Fix memory leak #1431

merged 5 commits into from
Sep 20, 2024

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Sep 19, 2024

What?

Fixes a memory leak that was introduced (regression) when working on #1420. This is done by:

  1. Adding a context cancel function in connection that will be closed when close() is called, which will ensure that the BaseEventEmitter is properly shutdown.
  2. Using an internal context in browser that will ensure that the connection and eventLoop are shutdown when browser.Close() is called.

Why?

This helps avoid memory leaks. The leaks were in the BaseEventEmitter since the connection was created with a background context. The background context wasn't being closed, and therefore goroutines and incoming messages from the connection were being held on the heap and not being cleared.

Before the fix you can see the memory slowly increasing:
Screenshot 2024-09-20 at 00 07 56

After the fix:
Screenshot 2024-09-20 at 00 22 35

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

Nice 👍

common/browser.go Outdated Show resolved Hide resolved
common/browser.go Outdated Show resolved Hide resolved
The context needs to be closed when the connection is closed. We're
using the context to ensure that this happens. The reason we need to
close the context is so that the BaseEventEmitter is correctly shutdown
when the connection is closed.

This is being added here incase the context coming into NewConnection
cannot be cancelled.
This will help cleanup the connection, as well as the eventLoop in
initEvents.
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.

2 participants