Skip to content

Commit 57387cb

Browse files
author
Jos Hickson
authored
Merge pull request #5 from jhickson/stoprace
Fixed a race condition when stopping very soon after starting.
2 parents d33beeb + 5e4e8e6 commit 57387cb

5 files changed

Lines changed: 15 additions & 4 deletions

File tree

Winton.Extensions.Threading.Actor/Internal/StateMachine/ActiveActorState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ protected override void StopImpl()
6464

6565
protected override void EnterImpl()
6666
{
67+
Context.StartCompletionSource.SetResult(true);
68+
6769
foreach (var task in Context.InitialWorkQueue)
6870
{
6971
Context.StartTask(task);

Winton.Extensions.Threading.Actor/Internal/StateMachine/ActorContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public ActorContext(IActorTaskFactory actorTaskFactory)
2525

2626
public List<Task> InitialWorkQueue { get; } = new List<Task>();
2727

28+
public List<Task> InitialWorkToBeCancelledQueue { get; } = new List<Task>();
29+
2830
public IActorTaskFactory ActorTaskFactory { get; }
2931

3032
public ActorStartWork StartWork

Winton.Extensions.Threading.Actor/Internal/StateMachine/InitialActorState.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ protected override void StartImpl()
1919
}
2020
else
2121
{
22-
Context.StartCompletionSource.SetResult(true);
2322
Context.SetState<ActiveActorState>();
2423
}
2524
}

Winton.Extensions.Threading.Actor/Internal/StateMachine/StartingActorState.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected override void EnterImpl()
3434
{
3535
await task.ConfigureAwait(false);
3636

37-
Context.StartCompletionSource.SetResult(true);
3837
Context.SetState<ActiveActorState>();
3938

4039
if (ReceivedStopSignal)
@@ -62,7 +61,14 @@ protected override void EnterImpl()
6261

6362
protected override void ScheduleImpl(Task task)
6463
{
65-
Context.InitialWorkQueue.Add(task);
64+
if (!ReceivedStopSignal)
65+
{
66+
Context.InitialWorkQueue.Add(task);
67+
}
68+
else
69+
{
70+
Context.InitialWorkToBeCancelledQueue.Add(task);
71+
}
6672
}
6773

6874
private bool ReceivedStopSignal { get; set; } = false;

Winton.Extensions.Threading.Actor/Internal/StateMachine/StoppedActorState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System.Threading.Tasks;
23

34
namespace Winton.Extensions.Threading.Actor.Internal.StateMachine
@@ -24,12 +25,13 @@ protected override void StopImpl()
2425

2526
protected override void EnterImpl()
2627
{
27-
foreach (var task in Context.InitialWorkQueue)
28+
foreach (var task in Context.InitialWorkQueue.Concat(Context.InitialWorkToBeCancelledQueue))
2829
{
2930
task.Cancel();
3031
}
3132

3233
Context.InitialWorkQueue.Clear();
34+
Context.InitialWorkToBeCancelledQueue.Clear();
3335
}
3436
}
3537
}

0 commit comments

Comments
 (0)