Skip to content

IPersistencePlugin May Lose Data Because of the Disposing Order in NeoSystem.cs #2624

@dusmart

Description

@dusmart

Describe the bug
When we shut down the neo-node by Ctrl-C or exit command or any other unexpected method, we will probably lose one block's data for every IPersistencePlugin if it's syncing block and using OnCommit() for data recording. If OnPersist() is used for data recording, no data will be lost while an exception will be thrown.

Background
We're trying to build an archive node by adding an IPersistencePlugin that records all data at any block height. When we add the Dispose method, the DB will always meet a segmentation fault when we stop the neo-node.

To Reproduce
Steps to reproduce the behavior:

  1. Open the neo-node, install any IPersistencePlugin with Dispose() method that releases the essential resouces.
  2. Run ./neo-node
  3. Type exit after it starts syncing block

Expected behavior
The plugin will cause an exception because of using resources after releasing them.

Screenshots

   at Neo.Plugins.Storage.Store.Put(Byte[] key, Byte[] value)
   at Neo.Plugins.ArchiveStore.<>c__DisplayClass3_0.<OnPersist>b__0(Trackable v)
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Neo.Plugins.ArchiveStore.OnPersist(NeoSystem ns, Block block, DataCache snapshot, IReadOnlyList`1 ae)
   at Neo.Ledger.Blockchain.Persist(Block block)
   at Neo.Ledger.Blockchain.OnImport(IEnumerable`1 blocks, Boolean verify)
   at Akka.Actor.UntypedActor.Receive(Object message)
   at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
   at Akka.Actor.ActorCell.ReceiveMessage(Object message)
   at Akka.Actor.ActorCell.Invoke(Envelope envelope)
   at Akka.Dispatch.Mailbox.ProcessMailbox(Int32 left, Int64 deadlineTicks)
   at Akka.Dispatch.Mailbox.ProcessMailbox()
   at Akka.Dispatch.Mailbox.<Run>b__36_0()
   at Akka.Actor.ActorCell.UseThreadContext(Action action)
   at Akka.Dispatch.Mailbox.Run()
   at Akka.Dispatch.ThreadPoolExecutorService.<>c.<.cctor>b__3_0(Object t)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

[1]    56384 segmentation fault  ./neo-cli

neo/src/neo/NeoSystem.cs

Lines 164 to 174 in d092510

public void Dispose()
{
foreach (var p in Plugin.Plugins)
p.Dispose();
EnsureStoped(LocalNode);
// Dispose will call ActorSystem.Terminate()
ActorSystem.Dispose();
ActorSystem.WhenTerminated.Wait();
HeaderCache.Dispose();
store.Dispose();
}

Expected code
Move EnsureStoped(LocalNode); before plugins' Dispose().
At least make sure all IPersistencePlugins dispose after the LocalNode is stoped.

Maybe ApplicationLogs Plugin can add the Dispose() method to release DB safely after this issue is solved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions