From 4c1cbb768b293ea49f9b0eeab2004589d0c2624d Mon Sep 17 00:00:00 2001 From: phrwlk Date: Mon, 9 Feb 2026 22:37:57 +0200 Subject: [PATCH 1/2] fix: clear reference-type elements in Truncate to prevent pool memory leak --- src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs b/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs index 00b95e683953..06a2167b897c 100644 --- a/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs +++ b/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs @@ -228,6 +228,10 @@ public static void Insert( public static void Truncate(int newLength, T[] array, ref int count) { GuardIndex(newLength, count, shouldThrow: true, allowEqualToCount: true); + if (RuntimeHelpers.IsReferenceOrContainsReferences() && newLength < count) + { + Array.Clear(array, newLength, count - newLength); + } count = newLength; } From 019539b02e808b3201d5b7b42f2f41917d6be45d Mon Sep 17 00:00:00 2001 From: phrwlk Date: Tue, 10 Feb 2026 13:23:56 +0200 Subject: [PATCH 2/2] add helper --- .../Collections/ArrayListCore.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs b/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs index 06a2167b897c..518fb7512842 100644 --- a/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs +++ b/src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs @@ -51,6 +51,15 @@ public static void ClearToCount(T[] array, int count) } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ClearTail(T[] array, int newCount, int oldCount) + { + if (RuntimeHelpers.IsReferenceOrContainsReferences() && newCount < oldCount) + { + Array.Clear(array, newCount, oldCount - newCount); + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Add( ArrayPool pool, @@ -108,9 +117,9 @@ public static void ReduceCount( ClearToCount(oldArray, oldCount); pool.Return(oldArray); } - else if (RuntimeHelpers.IsReferenceOrContainsReferences()) + else { - Array.Clear(array, newCount, oldCount - newCount); + ClearTail(array, newCount, oldCount); } [DoesNotReturn] @@ -228,10 +237,7 @@ public static void Insert( public static void Truncate(int newLength, T[] array, ref int count) { GuardIndex(newLength, count, shouldThrow: true, allowEqualToCount: true); - if (RuntimeHelpers.IsReferenceOrContainsReferences() && newLength < count) - { - Array.Clear(array, newLength, count - newLength); - } + ClearTail(array, newLength, count); count = newLength; }