Skip to content

fix: unified transport and bootstrap memory leaks - #4219

Merged
EmandM merged 2 commits into
develop-3.x.xfrom
fix/unified-transport-leaks
Oct 26, 2026
Merged

EmandM merged 2 commits into
develop-3.x.xfrom
fix/unified-transport-leaks

Conversation

@NoelStephensUnity

Copy link
Copy Markdown
Member

Purpose of this PR

Fixes the native and managed memory a hybrid (unified) session left behind on shutdown. Over the unified PlayMode suite, leaked persistent allocations at editor exit drop from 3210 (8.87 MB of NativeArray) to 6. The 6 left are test-only and are fixed separately.

The leak is most visible with domain reload disabled, where nothing reclaims it between play sessions.

PR Scope:

  • UnifiedNetcodeTransport disposes each connection's send queue when the connection is removed or replaced, and disposes all of them on Shutdown. A server now also removes a client's connection once that client disconnects.
  • DisconnectRemoteClient ignores a client whose connection is already gone, instead of throwing.
  • The safety handle created for each received packet is released (editor and development builds only).
  • NetworkManager reuses an existing UnifiedNetcodeTransport instead of adding another component on every start.
  • UnifiedBootstrap resets its statics on load and no longer holds the last NetworkManager after initialization, so a disabled domain reload does not keep the previous session alive.

Out of scope:

  • The 6 remaining test allocations: the Pose cases of the RPC NativeArray/NativeList serialization tests never run and never dispose their collections (separate PR).

Jira ticket

None.

Documentation

  • No documentation changes or additions were necessary.

Testing & QA (How your changes can be verified during release Playtest)

Functional Testing

Manual testing :

  • Manual testing done

Automated tests:

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

Unified PlayMode suite: 283 passed, 0 failed.

Does the change require QA team to:

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

Up-port

Not needed: hybrid mode exists on develop-3.x.x only.

Backports

None.

Dispose each connection's send queue when the connection is removed or the
transport shuts down, release the safety handle created per received packet,
reuse the UnifiedNetcodeTransport component across restarts, and reset the
UnifiedBootstrap statics on load so a disabled domain reload does not keep
the previous NetworkManager alive.
@NoelStephensUnity
NoelStephensUnity requested a review from a team as a code owner October 26, 2026 21:29

@u-pr u-pr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Harness Review

LGTM

The change addresses native allocation cleanup and bootstrap lifetimes, with a remaining concern around transport state across session restarts.

Reviewed commit b4d1f32

🤖 Helpful? 👍/👎

{
unifiedTransport = gameObject.AddComponent<UnifiedNetcodeTransport>();
}
NetworkConfig.NetworkTransport = unifiedTransport;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2

After a client-to-server/host restart, a remote connection matching the previous server’s transport ID is rejected as already connected. UnifiedNetcodeTransport.Initialize replaces m_Connections but preserves m_ServerClientId from OnClientConnectedToServer. NetworkConnectionManager.TransportIdToClientId treats that retained ID as the server, so ConnectEventHandler rejects the connection. Previously, creating another transport restored the -1 sentinel. Reset m_ServerClientId to -1 in UnifiedNetcodeTransport.Initialize before reusing the component.

🤖 Helpful? 👍/👎

@EmandM
EmandM enabled auto-merge (squash) October 26, 2026 21:33
@codecov-github-com

codecov-github-com Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 0% with 43 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...time/Transports/Unified/UnifiedNetcodeTransport.cs 0.00% 28 Missing ⚠️
...cts/Runtime/Components/Helpers/UnifiedBootstrap.cs 0.00% 9 Missing ⚠️
...netcode.gameobjects/Runtime/Core/NetworkManager.cs 0.00% 6 Missing ⚠️
@@                Coverage Diff                @@
##           develop-3.x.x    #4219      +/-   ##
=================================================
- Coverage          76.03%   75.95%   -0.08%     
=================================================
  Files                159      159              
  Lines              27344    27378      +34     
=================================================
+ Hits               20791    20795       +4     
- Misses              6553     6583      +30     
Flag Coverage Δ
NGOv2_project_testproject_ubuntu 75.01% <0.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...netcode.gameobjects/Runtime/Core/NetworkManager.cs 72.11% <0.00%> (-0.27%) ⬇️
...cts/Runtime/Components/Helpers/UnifiedBootstrap.cs 0.00% <0.00%> (ø)
...time/Transports/Unified/UnifiedNetcodeTransport.cs 0.00% <0.00%> (ø)

... and 1 file with indirect coverage changes

Components Coverage Δ
com.unity.netcode.gameobjects 75.95% <33.33%> (-0.08%) ⬇️

ℹ️ Need help interpreting these results?

@EmandM
EmandM merged commit 9f88aea into develop-3.x.x Oct 26, 2026
26 checks passed
@EmandM
EmandM deleted the fix/unified-transport-leaks branch October 26, 2026 23:27
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