fix delegates lifetime: unsubscribe stale handlers on network despawn#14
Merged
Lordfirespeed merged 1 commit intolc-sigurd:mainfrom Mar 31, 2026
Merged
Conversation
Author
|
I could combine two lists of delegates into one with a huge signature or an internal struct, but it doesn't matter too much. |
Author
|
Polite ping? |
Lordfirespeed
requested changes
Sep 14, 2025
a4f9c8d to
1c7cb9b
Compare
1c7cb9b to
a9b09fe
Compare
Author
|
just squashed for now |
a9b09fe to
1552fd8
Compare
Author
|
Reverted |
0fc402a to
aab7f5e
Compare
Author
|
The latest patch includes a helper class which handles event registration and clean-up. I tested it with a modified |
CSync leaves stale change handlers after quitting the hosted game. When the host quits the game back to the main menu, ConfigSyncBehaviour despawns and gets destroyed by Unity (not by .NET GC), but any delegates connected to events in OnNetworkSpawn method remain and go stale. So next time a player hosts a lobby without shutting down and re-launching the game completely, when any of subscribed config files or config entries change, stale delegates execute and try to assign to a dead NetworkList _deltas[index]. Of course it results in an exception being logged to the console by BepInEx (which wraps event handler invocation in a try-catch). To fix this, store subscribed delegates, and properly unsubscribe them during despawn. Note that NetworkList and _syncEnabled variable can not be cleared/reset due to Unity issue which only got resolved in the most recent version. Lethal Company v73 updated Unity Netcode for GameObjects (NGO) to 1.12.0, but the fix is available for NGO 1.14.0+ only. References: - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates - Unity-Technologies/com.unity.netcode.gameobjects#3502 Fixes lc-sigurd#13
aab7f5e to
7fb4f03
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
CSync leaves stale change handlers after quitting the hosted game.
When the host quits the game back to the main menu, ConfigSyncBehaviour despawns and gets destroyed by Unity (not by .NET GC), but any delegates connected to events in
OnNetworkSpawnmethod remain and go stale.So next time a player hosts a lobby without shutting down and re-launching the game completely, when any of subscribed config files or config entries change, stale delegates execute and try to assign to a dead NetworkList
_deltas[index]. Of course it results in an exception being logged to the console by BepInEx (which wraps event handler invocation in a try-catch).To fix this, store subscribed delegates, and properly unsubscribe them during despawn.
Note that NetworkList and
_syncEnabledvariable can not be cleared/reset due to Unity issue which only got resolved in the most recent version.References:
Fixes #13