From b93c479989b9df5c9961b0a044393141581fb998 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 29 Apr 2024 10:01:39 -0500 Subject: [PATCH] Backport `HashedWheelTimerFix` to .NET Standard 2.0 Port https://github.com/akkadotnet/akka.net/pull/7174 to .NET Standard 2.0 --- .../Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs b/src/core/Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs index 24121e73358..e35783828a9 100644 --- a/src/core/Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs +++ b/src/core/Akka/Actor/Scheduler/HashedWheelTimerScheduler.cs @@ -248,8 +248,10 @@ private void ProcessReschedule(long now) private void Start() { - if (_workerState == WORKER_STATE_STARTED) { } // do nothing - else if (_workerState == WORKER_STATE_INIT) + // only read the worker state once so it can't be a moving target for else-branch + var workerStateRead = _workerState; + if (workerStateRead == WORKER_STATE_STARTED) { } // do nothing + else if (workerStateRead == WORKER_STATE_INIT) { _worker ??= new Thread(Run) { IsBackground = true }; if (Interlocked.CompareExchange(ref _workerState, WORKER_STATE_STARTED, WORKER_STATE_INIT) == @@ -258,7 +260,7 @@ private void Start() _worker.Start(); } } - else if (_workerState is WORKER_STATE_SHUTDOWN) + else if (workerStateRead is WORKER_STATE_SHUTDOWN) { throw new SchedulerException("cannot enqueue after timer shutdown"); }