FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[C# Client] Client Crashes on dynamic KeyDown Registration

riffy opened this issue · comments

Description of the problem

When dynamically registering (outside of ctors) / subscribing to Alt.OnKeyDown event, crashes the client.
The ShowMenu function is invoked as an Action, meaning Action?.Invoke -> Alt.OnKeyDown -> crash.

private void ShowMenu()
{
  if (IsActive) return;
  // Register an alt keydown press since keycontroller will be disabled
  Alt.OnKeyDown += CloseMenuOnEscape;
  ....
}

/// <summary>
/// Closes the menu if Escape key is pressed
/// </summary>
private void CloseMenuOnEscape(Key key)
{
  if (key != Key.Escape) return;
  HideMenu();
}

private void HideMenu()
{
  if (!IsActive) return;
  Alt.OnKeyDown -= CloseMenuOnEscape;
  ...
}

The following Error is logged / produced:

[10:56:12][Error] FATAL EXCEPTION:
[10:56:12][Error] System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

   at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()

   at AltV.Net.Client.Extensions.EnumerableExtensions.ForEachCatching[T](IEnumerable`1 enumerable, Action`1 action, String exceptionLocation)

The only other OnKeyDown registration happens in a constructor, which doesn't crash the client:

public KeyController()
{
  Alt.OnKeyDown += OnKeyDown;
  Alt.OnKeyUp += OnKeyUp;
}

Environment

  • Client AsyncResource
  • .NET 6
  • C# Lang vers. 12