Skip to content

Commit

Permalink
Fix exception in CleanupOldPayloads from concurrent use of List (#5385)
Browse files Browse the repository at this point in the history
* Fix exception in CleanupOldPayloads from concurrent use of List

* formatting
  • Loading branch information
benaadams authored Mar 9, 2023
1 parent a3d2c44 commit 7373d3c
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Timers;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Merge.Plugin.Handlers;

Expand All @@ -29,7 +28,6 @@ public class PayloadPreparationService : IPayloadPreparationService
private readonly PostMergeBlockProducer _blockProducer;
private readonly IBlockImprovementContextFactory _blockImprovementContextFactory;
private readonly ILogger _logger;
private readonly List<string> _payloadsToRemove = new();

// by default we will cleanup the old payload once per six slot. There is no need to fire it more often
public const int SlotsPerOldPayloadCleanup = 6;
Expand Down Expand Up @@ -151,28 +149,31 @@ private IBlockImprovementContext CreateBlockImprovementContext(string payloadId,

private void CleanupOldPayloads(object? sender, EventArgs e)
{
if (_logger.IsTrace) _logger.Trace("Started old payloads cleanup");
foreach (KeyValuePair<string, IBlockImprovementContext> payload in _payloadStorage)
try
{
DateTimeOffset now = DateTimeOffset.UtcNow;
if (payload.Value.StartDateTime + _cleanupOldPayloadDelay <= now)
if (_logger.IsTrace) _logger.Trace("Started old payloads cleanup");
foreach (KeyValuePair<string, IBlockImprovementContext> payload in _payloadStorage)
{
if (_logger.IsDebug) _logger.Info($"A new payload to remove: {payload.Key}, Current time {now:t}, Payload timestamp: {payload.Value.CurrentBestBlock?.Timestamp}");
_payloadsToRemove.Add(payload.Key);
DateTimeOffset now = DateTimeOffset.UtcNow;
if (payload.Value.StartDateTime + _cleanupOldPayloadDelay <= now)
{
if (_logger.IsDebug) _logger.Info($"A new payload to remove: {payload.Key}, Current time {now:t}, Payload timestamp: {payload.Value.CurrentBestBlock?.Timestamp}");

if (_payloadStorage.TryRemove(payload.Key, out IBlockImprovementContext? context))
{
context.Dispose();
if (_logger.IsDebug) _logger.Info($"Cleaned up payload with id={payload.Key} as it was not requested");
}
}
}
}

foreach (string payloadToRemove in _payloadsToRemove)
if (_logger.IsTrace) _logger.Trace($"Finished old payloads cleanup");
}
catch (Exception ex)
{
if (_payloadStorage.TryRemove(payloadToRemove, out IBlockImprovementContext? context))
{
context.Dispose();
if (_logger.IsDebug) _logger.Info($"Cleaned up payload with id={payloadToRemove} as it was not requested");
}
_logger.Error($"Exception in old payloads cleanup: {ex}");
}

_payloadsToRemove.Clear();
if (_logger.IsTrace) _logger.Trace($"Finished old payloads cleanup");
}

private Block? LogProductionResult(Task<Block?> t)
Expand Down

0 comments on commit 7373d3c

Please sign in to comment.