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

Ensure event handlers are unsubscribed in Core Components #8815

Merged
merged 1 commit into from
Sep 21, 2020

Conversation

Shazwazza
Copy link
Contributor

@Shazwazza Shazwazza commented Sep 4, 2020

Static events are pretty evil because if you don't unsubscribe from them the event handler will pin an object that should be GC'd to the root of the app. This can cause memory leaks but worse can cause really odd things to happen. I'm surprised this hasn't been an issue in our integration tests. There's a chance it is causing problems but we don't visibly see it happening. There's also a chance this is causing problems in websites in rare circumstances like overlapping appdomains but I can't be sure. What is clear is that we absolutely must unsubscribe from these events so this fixes that to ensure there isn't weird problems occuring.

This is a backport from the netcore project because this was absolutely causing problems with integration tests so it's strange we don't see these in the netframework projects.


This item has been added to our backlog AB#8094

@Shazwazza Shazwazza added release/8.6.5 type/bug state/sprint-candidate We're trying to get this in a sprint at HQ in the next few weeks labels Sep 4, 2020
@marcemarc
Copy link
Contributor

@Shazwazza should we update examples in the docs? to always show unsubscribing if people subscribe to an event, eg: https://our.umbraco.com/documentation/Reference/Events/MemberService-Events/

@Shazwazza
Copy link
Contributor Author

@marcemarc yes absolutely, this type of thing will absolutely cause problems in tests and we aren't sure yet if this is causing problems in websites but either way, it's imperative that everyone cleans up their event handlers in static events.

@marcemarc
Copy link
Contributor

@Shazwazza ok done!

@warrenbuckley warrenbuckley self-requested a review September 21, 2020 09:47
@warrenbuckley
Copy link
Contributor

All good :)

@warrenbuckley warrenbuckley merged commit e7f98a5 into v8/8.6 Sep 21, 2020
@warrenbuckley warrenbuckley deleted the v8/bugfix/events-unsubscribe branch September 21, 2020 10:27
@umbrabot umbrabot removed the state/sprint-candidate We're trying to get this in a sprint at HQ in the next few weeks label Sep 21, 2020
nul800sebastiaan pushed a commit that referenced this pull request Sep 21, 2020
Ensure event handlers are unsubscribed  in Core Components

(cherry picked from commit e7f98a5)
@nul800sebastiaan
Copy link
Member

Cherry picked for 8.7.1 in 3950f32

@ronaldbarendse
Copy link
Contributor

@Shazwazza If we should always unsubscribe all static events, why doesn't Umbraco ensure this is always done by also setting the event handlers to null in the last component that runs (e.g. the CoreInitialComponent and/or WebInitialComponent)?

I've checked the amount of events on startup and after a application pool recycle and restart using ContentService.Saved.GetInvocationList().Length, but it's always reset and doesn't increase. This is probably because static fields are unique per AppDomain and are thus reset on restart...

@Shazwazza
Copy link
Contributor Author

@ronaldbarendse because cleaning up event subscriptions is a good thing to do to make sure nothing strange happens :) We've talked about nulling out all events too as another precaution too. The reason for this PR is because we discovered some nasty leaks between threads in integration tests that were causing havoc which is down to events being static and not allowing things to be GC'd and then having those event handlers execute with stale resources. To be sure this isn't affecting anything else we unbind from events. Sure, in a web app this might not be so important but in other app types it certainly can be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants