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
NetworkVariable and NetworkList throw NullReferenceException if modified during application quit #2705
Comments
|
I have the same issue in 1.6.0. |
|
@A1win @cbwan Modifying a NetworkVariableBase derived class during OnNetworkSpawn (as long as the instance has write permissions) is typically the better path to approach and if you have some values you wish to preserve (i.e. just despawning but not destroying) then you might be better setting those values in a normal property that is applied during the re-spawning of the instance. Otherwise, I would highly recommend using NetworkManager.ShutdownInProgress prior modifying any NetworkVariableBase derived class (or invoking a method on a component of a different NetworkObject) from within OnNetworkDespawn. |
|
@NoelStephensUnity Thank you for your answer. In my case I am not modifying the network variable during OnNetworkSpawn. |
|
Although it is possible that in my particular case the application quit and the disconnection of all the clients happen at the exact same frame. When I get the clients to quit before the host quits I don't have the error. |
|
@NoelStephensUnity In my case, I don't intentionally modify the NetworkVariable or NetworkList during shutdown, but there are some pretty common use cases where it makes sense to modify them when a different NetworkObject despawns. For example:
These kind of use cases happen commonly throughout the game, but it just happens that they also modify a NetworkVariable or a NetworkList when the application is shutting down, because everything is despawning at that point. Adding a check for This only happens during application quit and doesn't seem to cause any other problems than a logged error, so it's not necessarily critical, but probably worth fixing eventually anyway. 🙂 |
I wouldn't think that you should have to check all places, just when despawning? I guess it would depend on how things are put together. Soo... what I think the issue is at hand is you are getting an exception for a situation that I don't think you should be getting an exception for... What I think we could change to avoid this exception: It shouldn't throw an exception when:
Under these conditions, silently exiting without setting anything would be the best option (TBD). Let me mark this for import as it is a regression oriented bug where we were allowing it to proceed under conditions that would lead to an exception in other areas of the code. |
Description
When
Shutdown()is called on the server during Application Quit and a NetworkObject'sOnNetworkDespawncauses a different NetworkObject to modify aNetworkListorNetworkVariablethat's attached to it, aNullReferenceExceptionis thrown.For
NetworkList.cs, the exception gets thrown in theMarkNetworkObjectDirtyfunction on line78.For
NetworkVariableBase.cs, it gets thrown onSetDirtyfunction on line98.This is an edge case of #2606 that was fixed in Netcode
1.5.2. Previously, this error always occurred duringShutdown()even if the shutdown was not caused by application quit.As a less than ideal workaround, it seems to be possible to check for
NetworkManager.ShutdownInProgressbefore making modifications to NetworkVariables or NetworkLists during application quit.Reproduce Steps
NetworkVariableattached to it.NetworkListattached to it.OnNetworkDespawnfunction that modifies theNetworkVariableof NetworkObject A.OnNetworkDespawnfunction that modifies theNetworkListof NetworkObject B.NetworkVariableBase.csandNetworkList.cs.I'm not sure if this is a reliable way to reproduce the issue and I don't currently have a small sample project to reproduce it in.
Actual Outcome
NetworkVariableBase and NetworkList throw a NullReferenceException when modified during application quit.
Expected Outcome
No exceptions are thrown when modifying a NetworkVariable or a NetworkList during application quit.
Environment
2022.3.7f11.5.2The text was updated successfully, but these errors were encountered: