Skip to content

Commit 9631992

Browse files
committed
fix some leaks
1 parent bb3bf8d commit 9631992

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private void ValidateProcessedBlock(Block suggestedBlock, ProcessingOptions opti
9393
{
9494
if (!options.ContainsFlag(ProcessingOptions.NoValidation) && !blockValidator.ValidateProcessedBlock(block, receipts, suggestedBlock, out string? error))
9595
{
96+
block.DisposeAccountChanges();
9697
if (_logger.IsWarn) _logger.Warn(InvalidBlockHelper.GetMessage(suggestedBlock, "invalid block after processing"));
9798
throw new InvalidBlockException(suggestedBlock, error);
9899
}

src/Nethermind/Nethermind.Network/P2P/MessageDictionary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public void Handle(long id, TData data, long size)
100100
{
101101
Interlocked.Decrement(ref _requestCount);
102102
request.ResponseSize = size;
103-
request.CompletionSource.TrySetResult(data);
103+
if (!request.CompletionSource.TrySetResult(data))
104+
data?.TryDispose();
104105
}
105106
else
106107
{

src/Nethermind/Nethermind.Network/P2P/Subprotocols/Eth/V62/Eth62ProtocolHandler.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ protected void Handle(TransactionsMessage msg)
237237
IOwnedReadOnlyList<Transaction> iList = msg.Transactions;
238238
if (!BackgroundTaskScheduler.TryScheduleBackgroundTask(new TransactionsRequest(iList, 0), _handleSlow, "Transactions"))
239239
{
240+
foreach (Transaction tx in iList)
241+
{
242+
tx.ClearPreHash();
243+
}
240244
iList.Dispose();
241245
}
242246
}
@@ -245,43 +249,49 @@ protected virtual ValueTask HandleSlow(TransactionsRequest request, Cancellation
245249
{
246250
IOwnedReadOnlyList<Transaction> transactions = request.Transactions;
247251
ReadOnlySpan<Transaction> transactionsSpan = transactions.AsSpan();
252+
253+
int currentIdx = request.StartIndex;
254+
bool isTrace = Logger.IsTrace;
255+
bool isTransferred = false;
256+
248257
try
249258
{
250-
int startIdx = request.StartIndex;
251-
bool isTrace = Logger.IsTrace;
252-
253-
for (int i = startIdx; i < transactionsSpan.Length; i++)
259+
while (currentIdx < transactionsSpan.Length)
254260
{
255261
if (cancellationToken.IsCancellationRequested)
256262
{
257-
if (i == startIdx)
263+
if (currentIdx == request.StartIndex)
258264
{
259265
// Cancelled before processing any transaction — dispose and bail out.
260266
// Rescheduling would just loop (cancelled again immediately).
261-
transactions.Dispose();
262267
return ValueTask.CompletedTask;
263268
}
264269

265-
// Reschedule remaining transactions with a different start index
266-
if (!BackgroundTaskScheduler.TryScheduleBackgroundTask(new TransactionsRequest(transactions, i), _handleSlow, "Transactions"))
270+
if (BackgroundTaskScheduler.TryScheduleBackgroundTask(new TransactionsRequest(transactions, currentIdx), _handleSlow, "Transactions"))
267271
{
268-
transactions.Dispose();
272+
isTransferred = true;
269273
}
274+
270275
return ValueTask.CompletedTask;
271276
}
272277

273-
PrepareAndSubmitTransaction(transactionsSpan[i], isTrace);
278+
PrepareAndSubmitTransaction(transactionsSpan[currentIdx], isTrace);
279+
currentIdx++;
274280
}
275-
276-
transactions.Dispose();
277281
}
278-
catch
282+
finally
279283
{
280-
transactions.Dispose();
281-
throw;
284+
if (!isTransferred)
285+
{
286+
while (currentIdx < transactionsSpan.Length)
287+
{
288+
transactionsSpan[currentIdx].ClearPreHash();
289+
currentIdx++;
290+
}
291+
transactions.Dispose();
292+
}
282293
}
283294

284-
285295
return ValueTask.CompletedTask;
286296
}
287297

src/Nethermind/Nethermind.Network/P2P/Subprotocols/Eth/V62/Messages/TransactionsMessageSerializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public static IOwnedReadOnlyList<Transaction> DeserializeTxs(ref Rlp.ValueDecode
6262
}
6363
catch
6464
{
65+
foreach (Transaction tx in result)
66+
tx.ClearPreHash();
6567
result.Dispose();
6668
throw;
6769
}

src/Nethermind/Nethermind.Network/P2P/Subprotocols/Eth/V68/Eth68ProtocolHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ protected override ValueTask HandleSlow(TransactionsRequest request, Cancellatio
260260
{
261261
if (!ValidateSizeAndType(transactionsSpan[i]))
262262
{
263+
for (int j = startIdx; j < transactionsSpan.Length; j++)
264+
{
265+
transactionsSpan[j].ClearPreHash();
266+
}
263267
transactions.Dispose();
264268
throw new SubprotocolException("invalid pooled tx type or size");
265269
}

0 commit comments

Comments
 (0)