[BUG]: EventManager incorrectly blames the last registered plugin for exceptions thrown by other plugins
Before You Report
- [x] I have searched for existing reports of this bug, and this bug report is unique.
Version
1.1.6
Description
When an exception occurs inside an event handler, EventManager.InvokeEvent catches it and logs an error using FormatErrorMessage(eventHandler, e). However, because eventHandler is a MulticastDelegate, accessing its .Method and .Target properties returns the information of the last delegate added to the invocation list, not the one that actually threw the exception.
This causes a severe "blame-shifting" issue where perfectly functioning plugins are blamed in the server console for exceptions thrown by poorly written plugins, making debugging and ecosystem maintenance incredibly toxic and confusing for server owners.
To Reproduce
Compile and load the following Proof of Concept plugin. BadPlugin loads first and throws an error. GoodPlugin loads last and does nothing wrong.
Proof of Concept Code
using System;
using LabApi.Events.Arguments.PlayerEvents;
using LabApi.Events.Handlers;
using LabApi.Features.Console;
using LabApi.Loader.Features.Plugins;
using LabApi.Loader.Features.Plugins.Enums;
namespace BugRepro.BlameShifting
{
public class BadPlugin : Plugin
{
public override string Name => "1_BadPlugin";
public override string Description => "Throws an error.";
public override string Author => "QA";
public override Version Version => new Version(1, 0, 0);
public override Version RequiredApiVersion => new Version(1, 0, 0);
public override LoadPriority Priority => LoadPriority.Highest;
public override void Enable() => PlayerEvents.Joined += OnJoined;
public override void Disable() => PlayerEvents.Joined -= OnJoined;
private void OnJoined(PlayerJoinedEventArgs ev)
{
throw new Exception("This is a deliberate error from BadPlugin!");
}
}
public class GoodPlugin : Plugin
{
public override string Name => "2_GoodPlugin";
public override string Description => "Does nothing wrong.";
public override string Author => "QA";
public override Version Version => new Version(1, 0, 0);
public override Version RequiredApiVersion => new Version(1, 0, 0);
public override LoadPriority Priority => LoadPriority.Lowest;
public override void Enable() => PlayerEvents.Joined += OnJoined;
public override void Disable() => PlayerEvents.Joined -= OnJoined;
private void OnJoined(PlayerJoinedEventArgs ev)
{
Logger.Info("GoodPlugin executed perfectly.");
}
}
}
Server Log Output:
[ERROR] [LabApi] 'Exception' occurred while invoking 'OnJoined' on 'BugRepro.BlameShifting.GoodPlugin': 'This is a deliberate error from BadPlugin!', stack trace:
As seen above, GoodPlugin is blamed for BadPlugin's exception.
Expected Behavior
The logger should point to the actual plugin/method that threw the exception.
Additional Information
Proposed Fix:
In LabApi\Events\EventManager.cs, inside the catch block of both InvokeEvent methods, pass sub (the specific delegate being invoked in the loop) to FormatErrorMessage instead of eventHandler.
Change this:
Logger.Error(FormatErrorMessage(eventHandler, e));
To this:
Logger.Error(FormatErrorMessage(sub, e));
This issue has been resolved internally, the fix will be included in a future release.