diff --git a/src/Nethermind/Nethermind.Network/PeerManager.cs b/src/Nethermind/Nethermind.Network/PeerManager.cs index 5c382cf7d9a..cd2db4aa34f 100644 --- a/src/Nethermind/Nethermind.Network/PeerManager.cs +++ b/src/Nethermind/Nethermind.Network/PeerManager.cs @@ -219,27 +219,53 @@ private class CandidateSelection private async Task RunPeerUpdateLoop() { Channel taskChannel = Channel.CreateBounded(1); - using ArrayPoolList tasks = Enumerable.Range(0, _outgoingConnectParallelism).Select(async idx => + using ArrayPoolList tasks = new(_outgoingConnectParallelism); + for (int idx = 0; idx < _outgoingConnectParallelism; idx++) + { + tasks.Add(RunWorker(idx)); + } + + async Task RunWorker(int workerIdx) + { + await foreach (Peer peer in taskChannel.Reader.ReadAllAsync(_cancellationTokenSource.Token)) { - await foreach (Peer peer in taskChannel.Reader.ReadAllAsync(_cancellationTokenSource.Token)) + try { - try - { - await SetupOutgoingPeerConnection(peer); - } - catch (TaskCanceledException) - { - if (_logger.IsDebug) _logger.Debug($"Connect worker {idx} cancelled"); - break; - } - catch (Exception e) + await SetupOutgoingPeerConnection(peer); + } + catch (TaskCanceledException) + { + if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} cancelled"); + break; + } + catch (Exception e) + { + if (_logger.IsError) _logger.Error($"Error setting up connection to {peer}, {e}"); + } + } + if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} completed"); + } + { + await foreach (Peer peer in taskChannel.Reader.ReadAllAsync(_cancellationTokenSource.Token)) { - // This is strictly speaking not related to the connection, but something outside of it. - if (_logger.IsError) _logger.Error($"Error setting up connection to {peer}, {e}"); + try + { + await SetupOutgoingPeerConnection(peer); + } + catch (TaskCanceledException) + { + if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} cancelled"); + break; + } + catch (Exception e) + { + // This is strictly speaking not related to the connection, but something outside of it. + if (_logger.IsError) _logger.Error($"Error setting up connection to {peer}, {e}"); + } } - } - if (_logger.IsDebug) _logger.Debug($"Connect worker {idx} completed"); - }).ToPooledList(_outgoingConnectParallelism); + if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} completed"); + })); + } int loopCount = 0; long previousActivePeersCount = 0; diff --git a/src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs b/src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs index 83b496552b3..6ee02e6b3dd 100644 --- a/src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs +++ b/src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs @@ -6,7 +6,6 @@ using System.Buffers.Binary; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; @@ -146,9 +145,11 @@ public void Start( try { - using ArrayPoolListRef tasks = Enumerable.Range(0, trieVisitContext.MaxDegreeOfParallelism) - .Select(_ => Task.Run(BatchedThread)) - .ToPooledListRef(trieVisitContext.MaxDegreeOfParallelism); + using ArrayPoolListRef tasks = new(trieVisitContext.MaxDegreeOfParallelism); + for (int i = 0; i < trieVisitContext.MaxDegreeOfParallelism; i++) + { + tasks.Add(Task.Run(BatchedThread)); + } Task.WaitAll(tasks.AsSpan()); }