Skip to content

Commit

Permalink
Fix really slow statesync tests (#7954)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Dec 23, 2024
1 parent fbc2f09 commit 7c4a5be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ protected ContainerBuilder BuildTestContainerBuilder(DbContext dbContext, int sy

containerBuilder.RegisterBuildCallback((ctx) =>
{
CancelOnDisposeToken tokenHolder = ctx.Resolve<CancelOnDisposeToken>();
Task _ = ctx.Resolve<SyncDispatcher<StateSyncBatch>>().Start(tokenHolder.Token);
Task _ = ctx.Resolve<SyncDispatcher<StateSyncBatch>>().Start(default);
ctx.Resolve<ISyncPeerPool>().Start();
});

Expand All @@ -140,12 +139,14 @@ protected ContainerBuilder BuildTestContainerBuilder(DbContext dbContext, int sy

protected async Task ActivateAndWait(SafeContext safeContext, int timeout = TimeoutLength)
{
DotNetty.Common.Concurrency.TaskCompletionSource dormantAgainSource = new();
// Note: The `RunContinuationsAsynchronously` is very important, or the thread might continue synchronously
// which causes unexpected hang.
TaskCompletionSource dormantAgainSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
safeContext.Feed.StateChanged += (_, e) =>
{
if (e.NewState == SyncFeedState.Dormant)
{
dormantAgainSource.TrySetResult(0);
dormantAgainSource.TrySetResult();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Blockchain.Synchronization;
Expand All @@ -20,13 +19,13 @@ public class SyncDispatcher<T> : IAsyncDisposable
private static readonly TimeSpan ActiveTaskDisposeTimeout = TimeSpan.FromSeconds(10);

private IPeerAllocationStrategyFactory<T> PeerAllocationStrategyFactory { get; }

private ILogger Logger { get; }
private ISyncFeed<T> Feed { get; }
private ISyncDownloader<T> Downloader { get; }
private ISyncPeerPool SyncPeerPool { get; }

private readonly CountdownEvent _activeTasks = new CountdownEvent(1);
private readonly CancellationTokenSource _cancellationTokenSource = new();
private readonly SemaphoreSlim _concurrentProcessingSemaphore;
private readonly TimeSpan _emptyRequestDelay;
private readonly int _allocateTimeoutMs;
Expand Down Expand Up @@ -73,7 +72,8 @@ public async Task Start(CancellationToken cancellationToken)
try
{
_activeTasks.AddCount(1);
await DispatchLoop(cancellationToken);
using CancellationTokenSource linkedSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token);
await DispatchLoop(linkedSource.Token);
}
finally
{
Expand Down Expand Up @@ -313,6 +313,8 @@ public ValueTask DisposeAsync()
return ValueTask.CompletedTask;
}

_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_activeTasks.Signal();
if (!_activeTasks.Wait(ActiveTaskDisposeTimeout))
{
Expand Down

0 comments on commit 7c4a5be

Please sign in to comment.