Skip to content

[BUG] WeakEventManager throws InvalidHandleEventException for closed DynamicMethod handlers, like Expression.Compile() #347

Open

Description

@TheCodeTraveler

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

WeakEventManager.RaiseEvent throws InvalidHandleEventException ("Parameter count mismatch", inner TargetParameterCountException) when the handler is a closed DynamicMethod delegate. For example:

  • a handler from Expression<EventHandler>.Compile()
  • DynamicMethod.CreateDelegate(typeof(EventHandler), target)

EventManagerService.HandleEvent calls dynamicMethod.Invoke(instance, [sender, eventArgs]). A DynamicMethod is static, so Invoke ignores instance. The bound target has to be the first element of the argument array instead.

All three HandleEvent overloads make this call:

The exception message is also misleading: it suggests calling a different HandleEvent overload.

Expected Behavior

The handler runs, the same as when it's subscribed to a regular event with +=.

Steps To Reproduce

  1. Create a console app targeting net10.0 that references AsyncAwaitBestPractices 10.0.0.
  2. Replace Program.cs with the code below and run it.
using System.Linq.Expressions;
using AsyncAwaitBestPractices;

var weakEventManager = new WeakEventManager();
var counter = new Counter();

Expression<EventHandler> lambda = (sender, e) => counter.Hit();
EventHandler handler = lambda.Compile();
weakEventManager.AddEventHandler(handler, "Event");

weakEventManager.RaiseEvent(null, EventArgs.Empty, "Event"); // Throws InvalidHandleEventException

Console.WriteLine($"Handler ran {counter.Count} time(s)");
GC.KeepAlive(handler);

class Counter
{
	public int Count { get; private set; }
	public void Hit() => Count++;
}

Link to public reproduction project repository

Not available: the code above is the complete app.

Environment

  • AsyncAwaitBestPractices 10.0.0 (lib/net10.0). The PR Add .NET 11 Support #346 branch behaves the same on net10.0 and netstandard2.1.
  • .NET SDK 11.0.100-rc.3.26522.148, macOS arm64.
  • It affects every target framework: the .NET 5+ and .NET Standard code paths make the same call.

Anything else?

  • This isn't a regression. Copilot's review of Add .NET 11 Support #346 found it (review), and 10.0.0 already has it.
  • A fix needs a design decision. WeakEventManager holds the delegate's Target only weakly, and for Expression.Compile() that target is a compiler-generated Closure nothing else references, so the subscription can disappear at the next GC. Options:
    • Support these handlers: when instance isn't null and the DynamicMethod takes one more parameter than the arguments, pass instance as the first argument.
    • Or reject them in AddEventHandler with a clear exception.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions