Skip to content

Commit

Permalink
Fix PeriodicTimer scheduler deadlock during start (#6946)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Stannard <[email protected]>
  • Loading branch information
Arkatufus and Aaronontheweb authored Oct 5, 2023
1 parent a26f9bd commit 6f527f9
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions src/core/Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ private void Start()
var t = TimeSpan.FromTicks(_tickDuration);
_timer = new PeriodicTimer(t);

#pragma warning disable CS4014 // Intentional, running detached async Task
RunAsync(_cts.Token); // start the clock
#pragma warning restore CS4014 // Intentional, running detached async Task
Task.Run(() => RunAsync(_cts.Token)); // start the clock
}
}
else if (_workerState == WORKER_STATE_SHUTDOWN)
Expand Down Expand Up @@ -502,39 +500,46 @@ private Task<IEnumerable<SchedulerRegistration>> Stop()

public void Dispose()
{
#if NET6_0_OR_GREATER
_timer?.Dispose();
#endif
var stopped = Stop();
if (!stopped.Wait(_shutdownTimeout))
{
Log.Warning("Failed to shutdown scheduler within {0}", _shutdownTimeout);
return;
}

// Execute all outstanding work items
foreach (var task in stopped.Result)
try
{
try
var stopped = Stop();
if (!stopped.Wait(_shutdownTimeout))
{
task.Action.Run();
Log.Warning("Failed to shutdown scheduler within {0}", _shutdownTimeout);
return;
}
catch (SchedulerException)
{
// ignore, this is from terminated actors
}
catch (Exception ex)
{
Log.Error(ex, "Exception while executing timer task.");
}
finally

// Execute all outstanding work items
foreach (var task in stopped.Result)
{
// free the object from bucket
task.Reset();
try
{
task.Action.Run();
}
catch (SchedulerException)
{
// ignore, this is from terminated actors
}
catch (Exception ex)
{
Log.Error(ex, "Exception while executing timer task.");
}
finally
{
// free the object from bucket
task.Reset();
}
}
}

_unprocessedRegistrations.Clear();
finally
{
_unprocessedRegistrations.Clear();

#if NET6_0_OR_GREATER
_timer?.Dispose();
_cts.Dispose();
#endif
}
}

/// <summary>
Expand Down

0 comments on commit 6f527f9

Please sign in to comment.