Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit e792781

Browse files
authored
Merge pull request #227 from p-org/AndExecuteRaceFix
Fixed race condition in AndExecute APIs
2 parents 6ac3966 + 79a2dc8 commit e792781

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Libraries/Core/Library/Machine.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,14 @@ private bool GetNextEvent(out EventInfo nextEventInfo)
660660
/// is no next event to process or if the machine is halted.
661661
/// </summary>
662662
/// <param name="returnEarly">Returns after handling just one event</param>
663-
internal async Task RunEventHandler(bool returnEarly = false)
663+
internal async Task<bool> RunEventHandler(bool returnEarly = false)
664664
{
665665
if (this.IsHalted)
666666
{
667-
return;
667+
return true;
668668
}
669669

670+
bool completed = false;
670671
EventInfo nextEventInfo = null;
671672
while (!this.IsHalted && base.Runtime.IsRunning)
672673
{
@@ -691,6 +692,7 @@ internal async Task RunEventHandler(bool returnEarly = false)
691692
}
692693
else
693694
{
695+
completed = true;
694696
this.IsRunning = false;
695697
break;
696698
}
@@ -726,9 +728,11 @@ internal async Task RunEventHandler(bool returnEarly = false)
726728
// Return after handling the first event?
727729
if (returnEarly)
728730
{
729-
return;
731+
return false;
730732
}
731733
}
734+
735+
return completed;
732736
}
733737

734738
/// <summary>

Libraries/Core/Runtime/StateMachineRuntime.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,16 @@ private void RunMachineEventHandler(Machine machine, Event e, bool isFresh)
448448
/// <param name="isFresh">Is a new machine</param>
449449
private async Task RunMachineEventHandlerAsync(Machine machine, Event e, bool isFresh)
450450
{
451+
bool completed;
452+
451453
try
452454
{
453455
if (isFresh)
454456
{
455457
await machine.GotoStartState(e);
456458
}
457459

458-
await machine.RunEventHandler(true);
460+
completed = await machine.RunEventHandler(true);
459461
}
460462
catch (AssertionFailureException)
461463
{
@@ -469,7 +471,10 @@ private async Task RunMachineEventHandlerAsync(Machine machine, Event e, bool is
469471
return;
470472
}
471473

472-
RunMachineEventHandler(machine, null, false);
474+
if (!completed)
475+
{
476+
RunMachineEventHandler(machine, null, false);
477+
}
473478
}
474479

475480
#endregion

0 commit comments

Comments
 (0)