fix: Shut down NetworkManager when reloading the domain#4068
fix: Shut down NetworkManager when reloading the domain#4068simon-lemay-unity wants to merge 4 commits into
NetworkManager when reloading the domain#4068Conversation
| { | ||
| if (Application.isPlaying) | ||
| { | ||
| #if UNITY_2023_1_OR_NEWER |
There was a problem hiding this comment.
NGOv2.X offers support from 6000.0 editor so I don't think this if is needed
There was a problem hiding this comment.
The same check is used elsewhere in the same file, but happy to remove it if it's not needed.
There was a problem hiding this comment.
Yeah, remove it. I guess we just missed some stuff to cleanup. I will follow up on it
EmandM
left a comment
There was a problem hiding this comment.
I'd rather have started NetworkManager's register themselves to be shutdown instead of doing a search. No need to make domain reloads any slower for users.
Also, how does Fast Enter Play Mode interact with this problem? Do we need to register against different callbacks for that?
| if (Application.isPlaying) | ||
| { | ||
| #if UNITY_2023_1_OR_NEWER | ||
| var networkManager = Object.FindAnyObjectByType<NetworkManager>(); |
There was a problem hiding this comment.
We added our own static API that hides all the different mutations FindObjects has gone through over time.
Though I think rather than searching for all NetworkManagers, it'd be better if any started NetworkManager registers themselves to something so they can be shut down. Objects.FindAll can be slow, I'd rather avoid needing to make Domain reloads any slower.
| var networkManager = Object.FindAnyObjectByType<NetworkManager>(); | |
| var networkManager = FindObjects.ByType<NetworkManager>(); |
There was a problem hiding this comment.
I modified the code to use NetworkManager.Singleton instead.
It pretty much doesn't. The problem only manifests if we are already in play mode (and have live state that can't be serialized) and then encounter a domain reload. The issues with Fast Enter Play Mode are mostly with statics which won't be automatically reset across play mode entry/exit. Here the issue is not with domain reloads happening (or not) when entering/exiting play mode, it's when a domain reload occurs during play mode. |
Purpose of this PR
When the domain is reloaded while in play mode, a lot (most?) of the state in
NetworkManageris lost because it's not serializable. This state gets lost without any opportunity to properly clean it up which results in memory leaks and sockets not being closed. (The last one is particularly problematic because the zombie sockets will cause issues as long as the editor is open.)While ideally NGO would serialize all of its state and survive domain reloads, that's probably not very realistic in the short term as that would require extensive changes to the code. What we can do relatively easily however is properly shut down the
NetworkManagerwhen we do detect a domain reload. This at least avoids leaking resources.Jira ticket
UUM-146443
Changelog
NetworkManagerwill now perform a proper shutdown when the domain is reloaded in play mode, instead of being left in some half-initialized state that leaks memory and socket handles.Documentation
Testing & QA
Functional Testing
Manual testing :
Manual testing doneAutomated tests:
Covered by existing automated testsCovered by new automated testsDoes the change require QA team to:
Review automated tests?Execute manual tests?Provide feedback about the PR?Backports
Probably not worth it.