Skip to content

Null Ref in StopHost/Client if StartHost/Client failed beforeΒ #4041

@jonasd683

Description

@jonasd683

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions