Skip to content

[Fix]: concurrent conflict in Cache.CopyTo#3860

Merged
NGDAdmin merged 4 commits intoneo-project:masterfrom
Wi1l-B0t:fix.concurrent-conflicts-in-Cache
Apr 2, 2025
Merged

[Fix]: concurrent conflict in Cache.CopyTo#3860
NGDAdmin merged 4 commits intoneo-project:masterfrom
Wi1l-B0t:fix.concurrent-conflicts-in-Cache

Conversation

@Wi1l-B0t
Copy link
Contributor

Description

Get InnerDictionary.Count in Cache should be protected by lock

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

@Wi1l-B0t Wi1l-B0t added Easy-to-Review a simple edit; just a few lines Waiting for Review labels Mar 31, 2025
@Jim8y Jim8y requested a review from Copilot April 1, 2025 00:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a concurrency issue in the Cache.CopyTo method by ensuring that both the count check and the iteration over cache entries are performed under a read lock.

  • Introduces a read lock for the entire operation.
  • Replaces iteration over the Cache instance with iteration over InnerDictionary.Values.
  • Updates the assignment to extract the underlying cached value.
Comments suppressed due to low confidence (2)

src/Neo.IO/Caching/Cache.cs:164

  • This change from using 'this' to 'InnerDictionary.Values' ensures iteration under the read lock, but please verify that it retains any important logic from the original iterator implementation.
foreach (var item in InnerDictionary.Values)

src/Neo.IO/Caching/Cache.cs:166

  • Ensure that accessing 'item.Value' correctly retrieves the intended TValue, and verify that this change does not inadvertently omit any necessary conversion or handling previously provided by the original iteration.
array[startIndex++] = item.Value;

@Jim8y Jim8y requested review from cschuchardt88 and shargon April 1, 2025 00:55
@shargon
Copy link
Member

shargon commented Apr 1, 2025

@Wi1l-B0t the workflow fails

@Wi1l-B0t
Copy link
Contributor Author

Wi1l-B0t commented Apr 2, 2025

@Wi1l-B0t the workflow fails

CSC : error CS2012: Cannot open 'D:\a\neo\neo\src\Plugins\RpcServer\obj\Debug\net9.0\RpcServer.dll' for writing -- The process cannot access the file 'D:\a\neo\neo\src\Plugins\RpcServer\obj\Debug\net9.0\RpcServer.dll' because it is being used by another process. [D:\a\neo\neo\src\Plugins\RpcServer\RpcServer.csproj]

@NGDAdmin NGDAdmin merged commit 0ff3456 into neo-project:master Apr 2, 2025
6 checks passed
@Wi1l-B0t Wi1l-B0t deleted the fix.concurrent-conflicts-in-Cache branch April 4, 2025 10:01
Jim8y added a commit to Jim8y/neo that referenced this pull request Apr 9, 2025
* master: (163 commits)
  [style] Added some var styles (neo-project#3867)
  [`fix`] stop syncing on block 1465790 (neo-project#3888)
  Optimize block deserialization (neo-project#3879)
  Avoid double `ToArray` on `OnInvMessageReceived` (neo-project#3875)
  style: format long lines (neo-project#3884)
  optimize: return GetFileNameWithoutExtension(Path) if name is not set (neo-project#3883)
  Fix possible null exception (neo-project#3880)
  Remove linkedList (neo-project#3873)
  Optimize Uint160 and Uint256 constructor (neo-project#3872)
  Release the resources (neo-project#3868)
  [Clean] Remove `IRawReadOnlyStore` (neo-project#3869)
  move non-plugins out of plugins (neo-project#3863)
  feature: set name when create wallet (neo-project#3866)
  Native: swap Policy's `[get/set]AttributeFee` implementations (neo-project#3859)
  Fix: concurrent conflict in Cache.CopyTo (neo-project#3860)
  Fix: add default key parameter in help cmd (neo-project#3865)
  [Plugin UT] add more rpcserver UTs (neo-project#3864)
  config: upgrade NeoFS chains protocol configuration (neo-project#3858)
  [`Optimization`]: add exception message to `ArgumentException` (neo-project#3862)
  Native: unify arguments naming of CryptoLib's `verifyWith*` methods (neo-project#3855)
  ...

# Conflicts:
#	benchmarks/Neo.VM.Benchmarks/OpCode/Arrays/OpCode.ReverseN.cs
#	benchmarks/Neo.VM.Benchmarks/Program.cs
#	src/Neo/Neo.csproj
#	src/Neo/ProtocolSettings.cs
#	src/Neo/SmartContract/ApplicationEngine.cs
#	src/Neo/SmartContract/Native/NeoToken.cs
#	src/Neo/SmartContract/Native/RoleManagement.cs
#	tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs
#	tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractPermission.cs
#	tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
#	tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
#	tests/Neo.UnitTests/UT_ProtocolSettings.cs
#	tests/Neo.VM.Tests/UT_ReferenceCounter.cs
cschuchardt88 pushed a commit to cschuchardt88/neo that referenced this pull request Jun 8, 2025
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Easy-to-Review a simple edit; just a few lines Ready to Merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants