GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(core): allow unregistering callback through `on` by arturovt · Pull Request #11710 · getsentry/sentry-javascript

Conversation

@arturovt

@arturovt arturovt commented Apr 22, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This commit updates the return signature of the client's on function to be a void function,
which, when executed, unregisters a callback. This adjustment is necessary for managing instances
where objects are created and destroyed, ensuring that callbacks are properly unregistered to
prevent self-referencing in callback closures and facilitate proper garbage collection.

Typically, changing a type from void to () => void (or VoidFunction) shouldn't be considered
a breaking change because void signifies the absence of a return value, implying that the return
value of the on function should never be used by consumers.

Opting for the on approach, which returns a cleanup function, "seems" simpler because having another
function called off requires saving the callback reference for later removal. With our pattern,
we encapsulate both the registration and removal of event listeners within a single function call.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mydea

mydea commented Apr 22, 2024

Copy link
Copy Markdown
Member

While I can see where this is coming from, so far we decided against this to reduce complexity there. We generally do not do a lot of cleanup in a lot of places because (esp. in browser) because we make assumptions about the environment where something runs. I am not an Angular expect (cc @Lms24 ), but shouldn't the goal be to rather avoid the error handler to be continuously torn down and re-created? To me, it seems like this is the optimization that should be done, not to cleanup the one lone handler that is registered there.

Now, I don't have a problem per-se to add this change, the main reason we do not have cleanup everywhere is because it adds bundle size (not necessarily this change itself, but ensuring everywhere that stuff is properly cleaned up can sum up to a not-so-small amount of code, sadly). Not saying we shouldn't do this, just thinking out loud... 🤔

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arturovt

arturovt commented Apr 22, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Hey @mydea

Well, it's generally considered good practice to have a cleanup functional API, akin to addEventListener and removeEventListener. Initially, I pondered adding an off function instead of modifying on. However, as you pointed out, this approach seems to introduce additional complexity compared to enhancing the existing on function to return a cleanup function directly tied to the added callback. This approach avoids a redundant "abstraction" where a callback needs to be stored in a variable for later removal, as is the case with removeEventListener, which requires a reference to the callback.

What you mentioned about the Angular error handler also applies to other frameworks that incorporate server-side rendering and could potentially extend to microfrontend applications. Take, for example, a React application that is rendered on the server side and destroyed with each HTTP request. Similarly, in the context of microfrontend applications, consider an app associated with a specific URL segment, such as /blog. When navigating away from that URL, the corresponding app instance needs to be properly destroyed and all of the resources should be cleaned up.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Lms24

Lms24 commented Apr 22, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Hey @arturovt thanks for opening this PR!

Regarding the unsubscription mechanism we need to discuss this more thoroughly to decide what we're doing here. I'd tend to agree with @mydea that ideally we shouldn't add the extra bytes for correctly handling unsbscribing if at all possible. However, we should of course find a solution that doesn't cause hundreds of subscriptions from the errorHandler (or in other situations as you pointed out).

Consider, for instance, a scenario in Angular where the error handler is created and destroyed with each app rendering cycle.

Is this really default behaviour in Angular? If so there's probably a good reason for it but on first glance it's a bit unintuitive/unnecessary to me 🤔 Would appreciate any context/knowledge around this, thx :)

Also a general question: Is there a concrete reason for opening the PR? For example, did the SDK cause some kind of out of memory error that can be attributed to the client.on hooks? I just want to assess impact and priority for finding a good path forward.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arturovt

arturovt commented Apr 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@Lms24 This solution, lacking the functionality to remove an event listener when certain components are destroyed, such as any class with 'disposable' functionality, may inadvertently lead to memory leaks. When a callback captures local variables, there's no issue of leaks. However, if a callback captures this, it creates a closure that strongly retains a reference to this, preventing its garbage collection.

Describing the technical necessity behind why this is crucial can be somewhat challenging, especially when explaining the importance of removeEventListener.

Considering an Angular example: when the root injector is destroyed, it also destroyes all injectees it maintains as records. You may observe a console.error coming from ngOnDestroy (hook which is invoked when the root injector is destroyed, I added it intentionally). And at the end, you may also observe the error handler has not been GCd because its reference is retained in client hooks:

Screenshot from 2024-04-23 13-38-46

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Lms24
Lms24 reviewed Apr 25, 2024
View reviewed changes

@Lms24 Lms24 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We talked about this internally and decided to go ahead with this change. Thanks for explaining and of course for opening the PR! You're right, we should be good citizens and provide a cleanup functionality. Also obviously, use it in situations like the Angular error handler.

Before we can merge this, would you mind adding tests for the unregistering functionality?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/types/src/client.ts
* Receives the span as argument.
*/
on(hook: 'spanStart', callback: (span: Span) => void): void;
on(hook: 'spanStart', callback: (span: Span) => void): () => void;

@Lms24 Lms24 Apr 25, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Let's add a comment to the JSDocs about what's returned from the methods

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@arturovt arturovt May 3, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I addressed your comments, I see there're some random failures happening between commits, not sure if they're related...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andreiborza andreiborza self-assigned this Jul 2, 2024
@mydea

mydea commented Jul 2, 2024

Copy link
Copy Markdown
Member

Hey, we just noticed that this kind of fell of the table in between reviews - sorry about that! Would you mind rebasing on develop and resolving any conflicts, then I think this should be ready for a final review & merge!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arturovt

arturovt commented Jul 2, 2024

Copy link
Copy Markdown
Contributor Author

Hey, we just noticed that this kind of fell of the table in between reviews - sorry about that! Would you mind rebasing on develop and resolving any conflicts, then I think this should be ready for a final review & merge!

Done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mydea
mydea approved these changes Jul 3, 2024
View reviewed changes

@mydea mydea left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Nice, thank's a lot! Made some very minor adjustments to the tests, but LGTM 🚀

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/core/test/lib/base.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/core/test/lib/base.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/core/test/lib/base.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andreiborza andreiborza removed their assignment Jul 3, 2024
@mydea mydea self-assigned this Jul 3, 2024
arturovt and others added 3 commits July 4, 2024 10:17
@arturovt @mydea
feat(core): allow unregistering callback through on
2c38627
This commit updates the return signature of the client's `on` function to be a void function,
which, when executed, unregisters a callback. This adjustment is necessary for managing instances
where objects are created and destroyed, ensuring that callbacks are properly unregistered to
prevent self-referencing in callback closures and facilitate proper garbage collection.

Typically, changing a type from `void` to `() => void` (or `VoidFunction`) shouldn't be considered
a breaking change because `void` signifies the absence of a return value, implying that the return
value of the `on` function should never be used by consumers.

Opting for the `on` approach, which returns a cleanup function, "seems" simpler because having another
function called `off` requires saving the callback reference for later removal. With our pattern,
we encapsulate both the registration and removal of event listeners within a single function call.
@mydea
Apply suggestions from code review
cf9e279
@mydea
fix it, oops...
d7c2fd4
@mydea
mydea merged commit 506e6ca into getsentry:develop Jul 4, 2024
@mydea

mydea commented Jul 4, 2024

Copy link
Copy Markdown
Member

Thank you for your contribution! 🚀

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arturovt
arturovt deleted the feat/core-on-rm-callback branch July 4, 2024 11:53
@q1blue q1blue mentioned this pull request Jul 27, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.15.0 https-quantumblockchainai-atlassian-net/ossinsight#783
Open
@amykapernick amykapernick mentioned this pull request Jul 27, 2024
[Snyk] Upgrade @sentry/nextjs from 8.14.0 to 8.15.0 amykapernick/wedding#11
Open
@aam-digital-ci aam-digital-ci mentioned this pull request Jul 27, 2024
[Snyk] Upgrade @sentry/browser from 8.13.0 to 8.15.0 Aam-Digital/ndb-core#2482
Closed
This was referenced Jul 30, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.16.0 https-quantumblockchainai-atlassian-net/ossinsight#787
Open
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.17.0 https-quantumblockchainai-atlassian-net/ossinsight#791
Open
@amykapernick amykapernick mentioned this pull request Aug 1, 2024
[Snyk] Upgrade @sentry/nextjs from 8.14.0 to 8.17.0 amykapernick/wedding#12
Open
@wkylin wkylin mentioned this pull request Aug 1, 2024
[Snyk] Upgrade @sentry/react from 8.15.0 to 8.17.0 wkylin/pro-react-admin#370
Closed
@q1blue q1blue mentioned this pull request Aug 6, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.18.0 https-quantumblockchainai-atlassian-net/ossinsight#799
Open
@wkylin wkylin mentioned this pull request Aug 7, 2024
[Snyk] Upgrade @sentry/react from 8.15.0 to 8.18.0 wkylin/pro-react-admin#371
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Lms24 Lms24 Lms24 left review comments
@mydea mydea mydea approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@mydea mydea

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@arturovt @mydea @Lms24 @andreiborza
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.