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: concurrency conflict in MemPool.TryRemoveUnVerified #3500

Conversation

nan01ab
Copy link
Contributor

@nan01ab nan01ab commented Sep 23, 2024

Description

Fixes #3499

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

src/Neo/Ledger/MemoryPool.cs Outdated Show resolved Hide resolved
src/Neo/Ledger/MemoryPool.cs Outdated Show resolved Hide resolved
shargon
shargon previously approved these changes Sep 24, 2024
Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

Require testing

@nan01ab
Copy link
Contributor Author

nan01ab commented Sep 24, 2024

Require testing

A unit test added

@shargon
Copy link
Member

shargon commented Sep 24, 2024

Require testing

A unit test added

In real environment, please @superboyiii could you test it?

@Jim8y
Copy link
Contributor

Jim8y commented Sep 25, 2024

Require testing

A unit test added

In real environment, please @superboyiii could you test it?

SuperBoy is on travel, wont be back until 26th.

@superboyiii
Copy link
Member

In real environment, please @superboyiii could you test it?

Sure.

@Hecate2
Copy link
Contributor

Hecate2 commented Sep 26, 2024

I have doubts on this. Neo uses Akka actors to avoid locks. It was said that a single Akka actor processes only 1 message at any time. This means that no multiple instances of an actor is running. And TryRemoveUnVerified (along with HeaderCache.Count in the following PR #3501 , and everything in MemPool) is used only by the Blockchain actor, in a "single-threaded" way.

At the definition of _txRwLock in MemoryPool.cs there are the following comments:

The data structures are only modified from the `Blockchain` actor; so operations guaranteed to be
performed by the blockchain actor do not need to acquire the read lock; they only need the write
lock for write operations.

I am even doubting why write locks are needed, and whether the existing locks are necessary.

@nan01ab
Copy link
Contributor Author

nan01ab commented Sep 26, 2024

I have doubts on this. Neo uses Akka actors to avoid locks. It was said that a single Akka actor processes only 1 message at any time. This means that no multiple instances of an actor is running. And TryRemoveUnVerified (along with HeaderCache.Count in the following PR #3501 , and everything in MemPool) is used only by the Blockchain actor, in a "single-threaded" way.

At the definition of _txRwLock in MemoryPool.cs there are the following comments:

The data structures are only modified from the `Blockchain` actor; so operations guaranteed to be
performed by the blockchain actor do not need to acquire the read lock; they only need the write
lock for write operations.

I am even doubting why write locks are needed, and whether the existing locks are necessary.

MemPool and HeaderCache is written only by one actor, but may read by multi.
Lock is not for write-write conflict, but for write-read.

@@ -747,6 +747,25 @@ public void TestUpdatePoolForBlockPersisted()
_unit.VerifiedCount.Should().Be(0);
}

[TestMethod]
public void TestTryRemoveUnVerified()
Copy link
Contributor

Choose a reason for hiding this comment

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

how could this test the conflict?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Race condition is hard to test.

Copy link
Member

@superboyiii superboyiii left a comment

Choose a reason for hiding this comment

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

Tested OK.

@Jim8y Jim8y requested a review from shargon September 29, 2024 07:32
@NGDAdmin NGDAdmin merged commit 6e74e33 into neo-project:master Sep 30, 2024
6 of 7 checks passed

_unit.SortedTxCount.Should().Be(0);

foreach (var tx in txs)
Copy link
Member

Choose a reason for hiding this comment

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

maybe _unit.UnVerifiedCount.Should().Be(32); before the loop

Copy link
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

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

good change

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

Successfully merging this pull request may close these issues.

[Bug]: concurrency conflict in MemPool.TryRemoveUnVerified
7 participants