Skip to content

fix: Shut down NetworkManager when reloading the domain#4068

Open
simon-lemay-unity wants to merge 4 commits into
develop-2.0.0from
fix/shutdown-on-domain-reload
Open

fix: Shut down NetworkManager when reloading the domain#4068
simon-lemay-unity wants to merge 4 commits into
develop-2.0.0from
fix/shutdown-on-domain-reload

Conversation

@simon-lemay-unity

Copy link
Copy Markdown
Contributor

Purpose of this PR

When the domain is reloaded while in play mode, a lot (most?) of the state in NetworkManager is 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 NetworkManager when we do detect a domain reload. This at least avoids leaking resources.

Jira ticket

UUM-146443

Changelog

  • Fixed: NetworkManager will 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

  • No documentation changes or additions were necessary.

Testing & QA

Functional Testing

Manual testing :

  • Manual testing done

Automated tests:

  • Covered by existing automated tests
  • Covered by new automated tests

Does the change require QA team to:

  • Review automated tests?
  • Execute manual tests?
  • Provide feedback about the PR?

Backports

Probably not worth it.

@simon-lemay-unity simon-lemay-unity requested a review from a team as a code owner July 6, 2026 21:05
{
if (Application.isPlaying)
{
#if UNITY_2023_1_OR_NEWER

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.

NGOv2.X offers support from 6000.0 editor so I don't think this if is needed

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.

The same check is used elsewhere in the same file, but happy to remove it if it's not needed.

@michalChrobot michalChrobot Jul 8, 2026

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.

Yeah, remove it. I guess we just missed some stuff to cleanup. I will follow up on it

@EmandM EmandM left a comment

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'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>();

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.

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.

Suggested change
var networkManager = Object.FindAnyObjectByType<NetworkManager>();
var networkManager = FindObjects.ByType<NetworkManager>();

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.

I modified the code to use NetworkManager.Singleton instead.

@simon-lemay-unity

Copy link
Copy Markdown
Contributor Author

Also, how does Fast Enter Play Mode interact with this problem?

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.

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.

3 participants