Repository navigation
fix: Ensure all loaded scenes are registered on session owner promotion #4214
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
Merged
EmandM
merged 6 commits into
develop-3.x.x
from
fix/scene-validation-after-host-migration
Oct 26, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
00c2276
fix: Ensure all loaded scenes are registered on session owner promotion
EmandM 692c602
Update CHANGELOG
EmandM dd802a2
Merge branch 'develop-3.x.x' into fix/scene-validation-after-host-mig…
EmandM 84d252d
Make complicated booleans make more sense
EmandM 3d6fd74
chore: retrigger CI
NoelStephensUnity 68b178e
Merge develop-3.x.x into fix/scene-validation-after-host-migration
netcode-ci-service File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
testproject/Assets/Tests/Runtime/NetworkSceneManager/SessionOwnerPromotionSceneTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System.Collections; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine.SceneManagement; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace TestProject.RuntimeTests | ||
| { | ||
| /// <summary> | ||
| /// Validates that a client promoted to session owner registers the scenes it loaded outside of | ||
| /// <see cref="NetworkSceneManager"/> so it can synchronize newly joining clients. | ||
| /// Runs against the DAHost by default and against the live CMB service when USE_CMB_SERVICE is set. | ||
| /// </summary> | ||
| internal class SessionOwnerPromotionSceneTests : NetcodeIntegrationTest | ||
| { | ||
| private const string k_LocallyLoadedScene = "EmptyScene"; | ||
| protected override int NumberOfClients => 2; | ||
|
|
||
| private Scene m_OriginalActiveScene; | ||
| private Scene m_LocallyLoadedScene; | ||
|
|
||
| public SessionOwnerPromotionSceneTests() : base(HostOrServer.DAHost) { } | ||
|
|
||
| protected override void OnOneTimeSetup() | ||
| { | ||
| m_OriginalActiveScene = SceneManager.GetActiveScene(); | ||
| base.OnOneTimeSetup(); | ||
| } | ||
|
|
||
| protected override IEnumerator OnTearDown() | ||
| { | ||
| SceneManager.SetActiveScene(m_OriginalActiveScene); | ||
| if (m_LocallyLoadedScene.IsValid() && m_LocallyLoadedScene.isLoaded) | ||
| { | ||
| var scene = m_LocallyLoadedScene; | ||
| SceneManager.UnloadSceneAsync(scene); | ||
| yield return WaitForConditionOrTimeOut(() => !scene.isLoaded); | ||
| } | ||
| yield return base.OnTearDown(); | ||
| } | ||
|
|
||
| private NetworkManager m_InitialSessionOwner; | ||
|
|
||
| /// <summary> | ||
| /// Avoids <see cref="NetcodeIntegrationTest.GetAuthorityNetworkManager"/> since it fails the test when | ||
| /// invoked before a new session owner has been promoted. | ||
| /// </summary> | ||
| private bool IsNewSessionOwnerPromoted(StringBuilder errorLog) | ||
| { | ||
| // Have to do our own loop looking for a session owner as GetAuthorityNetworkManager will fail the test if no session owner is found. | ||
| // It is expected that no session owner will be found for a frame or two during session owner promotion. | ||
| NetworkManager sessionOwner = null; | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| if (networkManager.LocalClient.IsSessionOwner) | ||
| { | ||
| sessionOwner = networkManager; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (sessionOwner == null || sessionOwner == m_InitialSessionOwner) | ||
| { | ||
| errorLog.AppendLine("No new session owner has been promoted."); | ||
| return false; | ||
| } | ||
|
|
||
| var allClientsSynchronized = true; | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| if (networkManager.CurrentSessionOwner != sessionOwner.LocalClientId) | ||
| { | ||
| errorLog.AppendLine($"[Client-{networkManager.LocalClientId}] Incorrect {nameof(NetworkManager.CurrentSessionOwner)}! Expected: {sessionOwner.LocalClientId}. Has: {networkManager.CurrentSessionOwner}."); | ||
| allClientsSynchronized = false; | ||
| } | ||
| } | ||
| return allClientsSynchronized; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Loads and activates a scene without using <see cref="NetworkSceneManager"/>, promotes a new session owner, | ||
| /// and then verifies the promoted session owner can synchronize late joining clients. | ||
| /// </summary> | ||
| [UnityTest] | ||
| public IEnumerator PromotedSessionOwnerSynchronizesLocallyLoadedActiveScene() | ||
| { | ||
| // Load and activate a scene outside of NetworkSceneManager after the session has started, | ||
| // so no NetworkManager instance has a network scene handle registered for it. | ||
| var loadOperation = SceneManager.LoadSceneAsync(k_LocallyLoadedScene, LoadSceneMode.Additive); | ||
| Assert.NotNull(loadOperation, "Failed to load test scene!"); | ||
|
|
||
| yield return WaitForConditionOrTimeOut(() => loadOperation.isDone); | ||
| AssertOnTimeout($"Timed out waiting for {k_LocallyLoadedScene} to load!"); | ||
|
|
||
| // Set the newly loaded scene as the active scene | ||
| // Now all clients have this unregistered scene as their active scene. | ||
| m_LocallyLoadedScene = SceneManager.GetSceneByName(k_LocallyLoadedScene); | ||
| SceneManager.SetActiveScene(m_LocallyLoadedScene); | ||
|
|
||
| // Now all clients have this unregistered scene as their active scene. | ||
| m_InitialSessionOwner = GetAuthorityNetworkManager(); | ||
|
|
||
| // Sanity check: Double-check the newly loaded scene is unregistered on all clients | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| var isSceneRegistered = networkManager.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(m_LocallyLoadedScene.handle); | ||
| Assert.That(isSceneRegistered, Is.False, $"[Client-{networkManager.LocalClientId}] Scene loaded by the UnityEngine SceneManager should not have been registered!"); | ||
| } | ||
|
|
||
| if (m_UseCmbService) | ||
| { | ||
| // the CMB service promotes a client when the current session owner disconnects | ||
| yield return StopOneClient(m_InitialSessionOwner); | ||
| } | ||
| else | ||
| { | ||
| // The DAHost can manually promote another client as the session owner. | ||
| m_InitialSessionOwner.PromoteSessionOwner(GetNonAuthorityNetworkManager().LocalClientId); | ||
| } | ||
|
|
||
| yield return WaitForConditionOrTimeOut(IsNewSessionOwnerPromoted); | ||
| AssertOnTimeout("Session owner promotion failed!"); | ||
|
|
||
| // Connect a late-joining client to verify the new session owner handles the previously unregistered scene. | ||
| yield return CreateAndStartNewClient(); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
testproject/Assets/Tests/Runtime/NetworkSceneManager/SessionOwnerPromotionSceneTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
This test can pass if a future change removes promotion-time scene registration and the late joiner never synchronizes
EmptyScene.AddActiveSceneToClientSynchronizationnow warns and returns when the active scene has no mapping.EmptyScenecontains noNetworkObject, andCreateAndStartNewClientchecks connection and player clones rather than scene synchronization. Assert that the promoted owner registers the scene after promotion. Assert that the late joiner has a synchronizedEmptyScenemapped to the promoted owner's canonical handle.🤖 Helpful? 👍/👎
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.
This is a regression test for a bug where subsequent joins fail. I don't really care if the scene fails to synchronize, only that subsequent joiners can successfully join.