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

Fix plugin exception #3426

Merged
merged 20 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Akka.Actor;
using Akka.Configuration;
using Akka.IO;
using Akka.Util.Internal;
using Neo.IO.Actors;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
Expand All @@ -22,12 +21,11 @@
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we have this requirement added?


namespace Neo.Ledger
{
Expand Down Expand Up @@ -472,10 +470,10 @@ private void Persist(Block block)
Context.System.EventStream.Publish(application_executed);
all_application_executed.Add(application_executed);
}
_ = InvokeCommittingAsync(system, block, snapshot, all_application_executed);
InvokeCommitting(system, block, snapshot, all_application_executed);
snapshot.Commit();
}
_ = InvokeCommittedAsync(system, block);
InvokeCommitted(system, block);
system.MemPool.UpdatePoolForBlockPersisted(block, system.StoreView);
extensibleWitnessWhiteList = null;
block_cache.Remove(block.PrevHash);
Expand All @@ -484,39 +482,40 @@ private void Persist(Block block)
Debug.Assert(header.Index == block.Index);
}

internal static async Task InvokeCommittingAsync(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<ApplicationExecuted> applicationExecutedList)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<ApplicationExecuted> applicationExecutedList)
{
await InvokeHandlersAsync(Committing?.GetInvocationList(), h => ((CommittingHandler)h)(system, block, snapshot, applicationExecutedList));
InvokeHandlers(Committing?.GetInvocationList(), h => ((CommittingHandler)h)(system, block, snapshot, applicationExecutedList));
}

internal static async Task InvokeCommittedAsync(NeoSystem system, Block block)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeCommitted(NeoSystem system, Block block)
{
await InvokeHandlersAsync(Committed?.GetInvocationList(), h => ((CommittedHandler)h)(system, block));
InvokeHandlers(Committed?.GetInvocationList(), h => ((CommittedHandler)h)(system, block));
}

private static async Task InvokeHandlersAsync(Delegate[] handlers, Action<Delegate> handlerAction)
private static void InvokeHandlers(Delegate[] handlers, Action<Delegate> handlerAction)
{
if (handlers == null) return;

var exceptions = new ConcurrentBag<Exception>();
var tasks = handlers.Select(handler => Task.Run(() =>
foreach (var handler in handlers)
{
try
{
// skip stopped plugin.
if (handler.Target is Plugin { IsStopped: true })
{
return;
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the stopped plugin keep crashing nothing will be reported?

}

handlerAction(handler);
}
catch (Exception ex) when (handler.Target is Plugin plugin)
{
Utility.Log(nameof(plugin), LogLevel.Error, ex);
switch (plugin.ExceptionPolicy)
{
case UnhandledExceptionPolicy.StopNode:
exceptions.Add(ex);
throw;
case UnhandledExceptionPolicy.StopPlugin:
//Stop plugin on exception
Expand All @@ -526,20 +525,11 @@ private static async Task InvokeHandlersAsync(Delegate[] handlers, Action<Delega
// Log the exception and continue with the next handler
break;
default:
throw new InvalidCastException($"The exception policy {plugin.ExceptionPolicy} is not valid.");
throw new InvalidCastException(
$"The exception policy {plugin.ExceptionPolicy} is not valid.");
}

Utility.Log(nameof(plugin), LogLevel.Error, ex);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
})).ToList();

await Task.WhenAll(tasks);

exceptions.ForEach(e => throw e);
}
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions tests/Neo.UnitTests/Plugins/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public TestNonPlugin()
Blockchain.Committed += OnCommitted;
}

private void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
private static void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
throw new NotImplementedException("Test exception from OnCommitting");
}

private void OnCommitted(NeoSystem system, Block block)
private static void OnCommitted(NeoSystem system, Block block)
{
throw new NotImplementedException("Test exception from OnCommitted");
}
Expand All @@ -53,7 +53,7 @@ internal class TestPlugin : Plugin
private readonly UnhandledExceptionPolicy _exceptionPolicy;
protected internal override UnhandledExceptionPolicy ExceptionPolicy => _exceptionPolicy;

public TestPlugin(UnhandledExceptionPolicy exceptionPolicy = UnhandledExceptionPolicy.StopPlugin) : base()
public TestPlugin(UnhandledExceptionPolicy exceptionPolicy = UnhandledExceptionPolicy.StopPlugin)
{
Blockchain.Committing += OnCommitting;
Blockchain.Committed += OnCommitted;
Expand Down
55 changes: 27 additions & 28 deletions tests/Neo.UnitTests/Plugins/UT_Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
using Neo.Plugins;
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace Neo.UnitTests.Plugins
{
[TestClass]
public class UT_Plugin
{
private static readonly object locker = new();
private static readonly object s_locker = new();

[TestInitialize]
public void TestInitialize()
Expand Down Expand Up @@ -94,7 +93,7 @@ public void TestGetVersion()
[TestMethod]
public void TestSendMessage()
{
lock (locker)
lock (s_locker)
{
Plugin.Plugins.Clear();
Plugin.SendMessage("hey1").Should().BeFalse();
Expand All @@ -112,14 +111,14 @@ public void TestGetConfiguration()
}

[TestMethod]
public async Task TestOnException()
public void TestOnException()
{
_ = new TestPlugin();
// Ensure no exception is thrown
try
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
await Blockchain.InvokeCommittedAsync(null, null);
Blockchain.InvokeCommitting(null, null, null, null);
Blockchain.InvokeCommitted(null, null);
}
catch (Exception ex)
{
Expand All @@ -130,27 +129,27 @@ public async Task TestOnException()
_ = new TestNonPlugin();

// Ensure exception is thrown
await Assert.ThrowsExceptionAsync<NotImplementedException>(async () =>
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
});

await Assert.ThrowsExceptionAsync<NotImplementedException>(async () =>
{
await Blockchain.InvokeCommittedAsync(null, null);
});
Assert.ThrowsException<NotImplementedException>(() =>
{
Blockchain.InvokeCommitting(null, null, null, null);
});

Assert.ThrowsException<NotImplementedException>(() =>
{
Blockchain.InvokeCommitted(null, null);
});
}

[TestMethod]
public async Task TestOnPluginStopped()
public void TestOnPluginStopped()
{
var pp = new TestPlugin();
Assert.AreEqual(false, pp.IsStopped);
// Ensure no exception is thrown
try
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
await Blockchain.InvokeCommittedAsync(null, null);
Blockchain.InvokeCommitting(null, null, null, null);
Blockchain.InvokeCommitted(null, null);
}
catch (Exception ex)
{
Expand All @@ -161,16 +160,16 @@ public async Task TestOnPluginStopped()
}

[TestMethod]
public async Task TestOnPluginStopOnException()
public void TestOnPluginStopOnException()
{
// pp will stop on exception.
var pp = new TestPlugin();
Assert.AreEqual(false, pp.IsStopped);
// Ensure no exception is thrown
try
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
await Blockchain.InvokeCommittedAsync(null, null);
Blockchain.InvokeCommitting(null, null, null, null);
Blockchain.InvokeCommitted(null, null);
}
catch (Exception ex)
{
Expand All @@ -185,8 +184,8 @@ public async Task TestOnPluginStopOnException()
// Ensure no exception is thrown
try
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
await Blockchain.InvokeCommittedAsync(null, null);
Blockchain.InvokeCommitting(null, null, null, null);
Blockchain.InvokeCommitted(null, null);
}
catch (Exception ex)
{
Expand All @@ -197,19 +196,19 @@ public async Task TestOnPluginStopOnException()
}

[TestMethod]
public async Task TestOnNodeStopOnPluginException()
public void TestOnNodeStopOnPluginException()
{
// node will stop on pp exception.
var pp = new TestPlugin(UnhandledExceptionPolicy.StopNode);
Assert.AreEqual(false, pp.IsStopped);
await Assert.ThrowsExceptionAsync<NotImplementedException>(async () =>
Assert.ThrowsException<NotImplementedException>(() =>
{
await Blockchain.InvokeCommittingAsync(null, null, null, null);
Blockchain.InvokeCommitting(null, null, null, null);
});

await Assert.ThrowsExceptionAsync<NotImplementedException>(async () =>
Assert.ThrowsException<NotImplementedException>(() =>
{
await Blockchain.InvokeCommittedAsync(null, null);
Blockchain.InvokeCommitted(null, null);
});

Assert.AreEqual(false, pp.IsStopped);
Expand Down