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

Commit

Permalink
Merge pull request #227 from p-org/AndExecuteRaceFix
Browse files Browse the repository at this point in the history
Fixed race condition in AndExecute APIs
  • Loading branch information
pdeligia authored Aug 4, 2017
2 parents 6ac3966 + 79a2dc8 commit e792781
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 7 additions & 3 deletions Libraries/Core/Library/Machine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,13 +660,14 @@ private bool GetNextEvent(out EventInfo nextEventInfo)
/// is no next event to process or if the machine is halted.
/// </summary>
/// <param name="returnEarly">Returns after handling just one event</param>
internal async Task RunEventHandler(bool returnEarly = false)
internal async Task<bool> RunEventHandler(bool returnEarly = false)
{
if (this.IsHalted)
{
return;
return true;
}

bool completed = false;
EventInfo nextEventInfo = null;
while (!this.IsHalted && base.Runtime.IsRunning)
{
Expand All @@ -691,6 +692,7 @@ internal async Task RunEventHandler(bool returnEarly = false)
}
else
{
completed = true;
this.IsRunning = false;
break;
}
Expand Down Expand Up @@ -726,9 +728,11 @@ internal async Task RunEventHandler(bool returnEarly = false)
// Return after handling the first event?
if (returnEarly)
{
return;
return false;
}
}

return completed;
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions Libraries/Core/Runtime/StateMachineRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,16 @@ private void RunMachineEventHandler(Machine machine, Event e, bool isFresh)
/// <param name="isFresh">Is a new machine</param>
private async Task RunMachineEventHandlerAsync(Machine machine, Event e, bool isFresh)
{
bool completed;

try
{
if (isFresh)
{
await machine.GotoStartState(e);
}

await machine.RunEventHandler(true);
completed = await machine.RunEventHandler(true);
}
catch (AssertionFailureException)
{
Expand All @@ -469,7 +471,10 @@ private async Task RunMachineEventHandlerAsync(Machine machine, Event e, bool is
return;
}

RunMachineEventHandler(machine, null, false);
if (!completed)
{
RunMachineEventHandler(machine, null, false);
}
}

#endregion
Expand Down

0 comments on commit e792781

Please sign in to comment.