Skip to content
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

Naked delegates should have event modifier keyword #3137

Open
StephenHodgson opened this issue Nov 22, 2024 · 3 comments
Open

Naked delegates should have event modifier keyword #3137

StephenHodgson opened this issue Nov 22, 2024 · 3 comments
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feedback Issue contains feedback and not specific bug or feature requests.

Comments

@StephenHodgson
Copy link

Delegate events like the one shown above should have event keyword modifiers.

 public event OnValueChangedDelegate OnValueChanged; 

A "naked" delegate refers to a delegate that is declared without the event keyword. This means the delegate is exposed directly, allowing external code to not only subscribe and unsubscribe to it but also to reassign or invoke it directly. This can lead to unintended side effects and potential bugs.

For example, in the provided code snippet, the delegates are correctly declared as events:

public event Action OnSpawnedLocal;
public event Action OnSpawnedAll;
public event Action<NetworkPlayer> OnDisconnected;

If these were declared as naked delegates, they would look like this:

public Action OnSpawnedLocal;
public Action OnSpawnedAll;
public Action<NetworkPlayer> OnDisconnected;

Issues with naked delegates:

  1. Reassignment: External code can reassign the delegate, potentially removing all existing subscribers.

    networkPlayer.OnSpawnedLocal = SomeMethod; // Reassigns the delegate, removing all previous subscribers.
  2. Invocation: It breaks encapsulation, making it harder to control how and when the delegate is invoked.

    networkPlayer.OnSpawnedLocal?.Invoke(); // Invokes the delegate from outside the class.
  3. Dangling Pointers: Events in C# are multicast delegates which can have multiple and anonymous methods subscribe to the same event. Using the event keyword will help properly manage those subscriptions and unsubscriptions. This way if someone forgets to unsubscribe, the dangling pointer will get properly cleaned up by garbage collector.

  • If a delegate holds a reference to an object that should be garbage collected, it can prevent the object from being collected, leading to memory leaks.
  • If the target method or object of a delegate has been collected, invoking the delegate can result in a NullReferenceException or other runtime errors.

TR;DL:

I've seen this throughout the whole codebase and highly encourage the use of the event keyword for all usages of delegate callbacks.
Using the event keyword ensures that the delegate can only be invoked from within the class that declares it, maintaining proper encapsulation and control over the event's lifecycle and avoiding memory pressure from forgetting to unassign the delegates after use.

@StephenHodgson StephenHodgson added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feedback Issue contains feedback and not specific bug or feature requests. labels Nov 22, 2024
@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented Dec 10, 2024

Other than:

  • NetworkVariable.OnValueChanged (which you only want to subscribe to when it is spawned and always unsubscribe when despawned)
    • This is one area I would have to look through to determine how useful it would be vs the API change impacts
  • NetworkTransform.OnClientRequestChange (which is a very legacy delegate callback, rarely used, and when used it is for very specific conditions)
  • NetworkManager.ConnectionApprovalCallback (which is intentionally not an event)
  • NetworkObject.OnMigratedToNewScene (could be converted to an event, but there would be an API impact)

Everything else is either an event or internal to the SDK or tests... but I don't see a whole bunch of the public API Actions/delegates that aren't events (quick pass are the 4 above) and if it is internal or part of a test then we are pretty good at cleaning up delegate callbacks

However, I do appreciate the insights you shared and I will definitely keep that pattern in mind.

Other than the 4 above listed, is this suggestion really more focused on making NetworkVariable.OnValueChanged to be an event or have you found the other 3 troublesome too? I just want to make sure I am not missing something else in the API that you are seeing.

@StephenHodgson
Copy link
Author

StephenHodgson commented Dec 10, 2024

NetworkVariable.OnValueChanged

I think mainly just this one is the most concerning since anyone can stomp delegate subscriptions with a simple typo.

@StephenHodgson
Copy link
Author

StephenHodgson commented Dec 10, 2024

NetworkManager.ConnectionApprovalCallback (which is intentionally not an event)

Why is that? Are you expecting people to set their own delegate for a callback? Better to use a Func<T> for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feedback Issue contains feedback and not specific bug or feature requests.
Projects
None yet
Development

No branches or pull requests

2 participants