Skip to content

Conversation

@gitToki
Copy link
Contributor

@gitToki gitToki commented Sep 19, 2025

Changes

  • Fixed ArrayPool buffer leak in Get() method - Moved ArrayPool<byte>.Shared.Return(array) from try block to finally block to ensure cleanup even when TrieException is thrown
  • Fixed ArrayPool buffer leak in GetNodeByKey() method - Moved ArrayPool<byte>.Shared.Return(array) from try block to finally block to ensure cleanup even when TrieException is thrown

To give a little bit more detail about the why, the Get() and GetNodeByKey() methods in PatriciaTree rent buffers from ArrayPool but only returned them in the success path.

When GetNew() throws a TrieException, the rented buffers weren't returned to the pool, it could've cause gradual memory pressure under sustained load (even if the array get GC'ed at some point).

This was only affecting RPC endpoints when handling requests with keys larger than the 64byte stack allocation min.
For the p2p path, the peer would be banned before causing any significant harm.

@emlautarom1
Copy link
Contributor

The code does not compile as it is:

  Nethermind.Trie failed with 2 error(s) (0.2s)
    /home/emlautarom1/Development/Nethermind/nethermind/src/Nethermind/Nethermind.Trie/PatriciaTree.cs(362,21): error CS0103: The name 'array' does not exist in the current context
    /home/emlautarom1/Development/Nethermind/nethermind/src/Nethermind/Nethermind.Trie/PatriciaTree.cs(362,70): error CS0103: The name 'array' does not exist in the current context

@emlautarom1
Copy link
Contributor

emlautarom1 commented Sep 19, 2025

There is a similar case:

: array = ArrayPool<byte>.Shared.Rent(nibblesCount))

if (array is not null) ArrayPool<byte>.Shared.Return(array);

If you fix it then we can proceed.

@gitToki
Copy link
Contributor Author

gitToki commented Sep 19, 2025

Thanks for the review

@emlautarom1 emlautarom1 merged commit 1a8b639 into NethermindEth:master Sep 19, 2025
79 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants