Skip to content

Commit fb21e64

Browse files
fix: fix CancellationTokenSource leak in StartingSyncPivotUpdater (#10859)
* Update StartingSyncPivotUpdater.cs * Apply suggestion from @LukaszRozmej Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> * Apply suggestion from @LukaszRozmej Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> * Apply suggestion from @LukaszRozmej Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> * Update StartingSyncPivotUpdater.cs --------- Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com>
1 parent be56248 commit fb21e64

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/Nethermind/Nethermind.Merge.Plugin/Synchronization/StartingSyncPivotUpdater.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Nethermind.Consensus.Validators;
1010
using Nethermind.Core;
1111
using Nethermind.Core.Crypto;
12+
using Nethermind.Core.Extensions;
1213
using Nethermind.Logging;
1314
using Nethermind.Merge.Plugin.Handlers;
1415
using Nethermind.State.Snap;
@@ -18,7 +19,7 @@
1819

1920
namespace Nethermind.Merge.Plugin.Synchronization;
2021

21-
public class StartingSyncPivotUpdater
22+
public class StartingSyncPivotUpdater : IDisposable
2223
{
2324
private const string Pivot = "pivot";
2425
private readonly IBlockTree _blockTree;
@@ -29,7 +30,7 @@ public class StartingSyncPivotUpdater
2930
protected readonly IBeaconSyncStrategy _beaconSyncStrategy;
3031
protected readonly ILogger _logger;
3132

32-
private readonly CancellationTokenSource _cancellation = new();
33+
private CancellationTokenSource? _cancellation = new();
3334

3435
private static int _maxAttempts;
3536
private int _attemptsLeft;
@@ -69,7 +70,7 @@ private async void OnSyncModeChanged(object? sender, SyncModeChangedEventArgs sy
6970
{
7071
if ((syncMode.Current & SyncMode.UpdatingPivot) != 0 && Interlocked.CompareExchange(ref _updateInProgress, 1, 0) == 0)
7172
{
72-
if (await TrySetFreshPivot(_cancellation.Token))
73+
if (await TrySetFreshPivot(_cancellation!.Token))
7374
{
7475
_syncModeSelector.Changed -= OnSyncModeChanged;
7576
}
@@ -236,4 +237,10 @@ protected void UpdateAndPrintPotentialNewPivot(Hash256 finalizedBlockHash)
236237
_alreadyAnnouncedNewPivotHash = finalizedBlockHash;
237238
}
238239
}
240+
241+
public void Dispose()
242+
{
243+
_syncModeSelector.Changed -= OnSyncModeChanged;
244+
CancellationTokenExtensions.CancelDisposeAndClear(ref _cancellation);
245+
}
239246
}

0 commit comments

Comments
 (0)