Skip to content

Conversation

Copy link

Copilot AI commented Jan 9, 2026

When StartHost() fails during server setup (e.g., port already in use), the mode is set to Host but NetworkServer.localConnection remains null. Calling StopHost() then throws NullReferenceException when attempting to invoke OnServerDisconnect(NetworkServer.localConnection), preventing proper cleanup and leaving the NetworkManager in an inconsistent state.

Changes

  • NetworkManager.StopClient(): Added null check before accessing localConnection

    // Before
    if (mode == NetworkManagerMode.Host)
        OnServerDisconnect(NetworkServer.localConnection);
    
    // After
    if (mode == NetworkManagerMode.Host && NetworkServer.localConnection != null)
        OnServerDisconnect(NetworkServer.localConnection);
  • NetworkManagerTest.cs: Added tests for StopHost() and StopClient() behavior when localConnection is null

Behavior

After this change, calling StopHost() or StopClient() after a failed StartHost() will:

  • Skip the OnServerDisconnect() call when localConnection is null
  • Continue with normal disconnect flow via NetworkClient.Disconnect()
  • Properly reset mode to Offline via OnClientDisconnectInternal()
Original prompt

This section details on the original issue you should resolve

<issue_title>Null Ref in StopHost/Client if StartHost/Client failed before</issue_title>
<issue_description>Describe the bug
Calling the StopHost method (internally StopClient) results in a NullReferenceException (NetworkServer.localConnection is null) if it is called after the StartHost has previously failed.

In other words: It is not possible to reset the mode to offline if server or client creation fails (e.g. if the socket address, protocol, network address or connection is already in use (System.Net.Sockets.SocketException).

[IMPORTANT] How can we reproduce the issue, step by step:

  1. Start as Host with instance A.
  2. Start a second Host with instance B.
  3. An exception (System.Net.Sockets.SocketException, code 0x80004005) will occur on instance B, which is correct.
  4. The NetworkManager.singleton.mode of instance B is still host.
  5. Call StopHost on instance B to set the mode to offline.
  6. A NullReferenceException will occur because the NetworkServer.localConnection is null in the StopClient method that is called internally. This should be caught.
  7. The mode on instance B remains Host instead of offline.

Expected behavior
After calling StopHost/Client, the NetworkManager.singleton.mode should be offline. Even if an exception occurs when calling.

Is there any other way to reset the mode to offline?

Desktop (please complete the following information):

  • OS: Windows
  • Build target: all
  • Unity version: 2022.3.62f1
  • Mirror branch: 96.0.1 from Unity Asset Store</issue_description>

Comments on the Issue (you are @copilot in this section)

@MrGadget1024 Sounds like a bug that we're not resetting that when StartHost fails as you describe. Will look into it when we get a chance.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix null reference in StopHost/Client method after failure Fix NullReferenceException in StopClient when StartHost fails Jan 9, 2026
Copilot AI requested a review from MrGadget1024 January 9, 2026 19:58
@MrGadget1024 MrGadget1024 marked this pull request as ready for review January 9, 2026 20:47
@MrGadget1024 MrGadget1024 requested a review from miwarnec January 9, 2026 21:14
@MrGadget1024 MrGadget1024 added bug Something isn't working Awaiting Review labels Jan 9, 2026
@MrGadget1024 MrGadget1024 changed the title Fix NullReferenceException in StopClient when StartHost fails fix: NullReferenceException in StopClient when StartHost fails Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Awaiting Review bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null Ref in StopHost/Client if StartHost/Client failed before

2 participants