Skip to content

Commit

Permalink
Make sure unmuted actors can't run on two schedulers concurrently
Browse files Browse the repository at this point in the history
Prior to this commit, an actor with no pending messages being
unmuted could end up running concurrently on two scheduler
threads due to a race condition. The specific issue is that we
`unmute the actor` and then we `schedule the actor`. It is
entirely possible that the unmuted actor could get scheduled
on a different scheduler thread if another actor sent it a
message at the same time as we schedule it to run on the
current scheduler thread.

This commit changes the logic to check whether the actor
has any pending messages or not prior to unmuting it and
will only reschedule the actor if the actor actually had
pending messages to process already waiting in its queue.
  • Loading branch information
dipinhora committed Jun 26, 2022
1 parent b27e0bd commit dbfbd9a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/libponyrt/sched/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1488,10 +1488,23 @@ bool ponyint_sched_unmute_senders(pony_ctx_t* ctx, pony_actor_t* actor)
{
needs_unmuting = ponyint_actorstack_pop(needs_unmuting, &to_unmute);

// check if the actor has any messages waiting to be processed
// note: this must be done before the actor is unmuted to
// avoid a race condition
bool should_reschedule = !ponyint_messageq_isempty(&to_unmute->q);

// unmute actor
ponyint_unmute_actor(to_unmute);
ponyint_sched_add(ctx, to_unmute);
DTRACE2(ACTOR_SCHEDULED, (uintptr_t)sched, (uintptr_t)to_unmute);
actors_rescheduled++;

// only reschedule if the actor had messages waiting to be processed
// before we unnmuted it to ensure it cannot be concurrently
// scheduled on multiple scheduler threads
if(should_reschedule)
{
ponyint_sched_add(ctx, to_unmute);
DTRACE2(ACTOR_SCHEDULED, (uintptr_t)sched, (uintptr_t)to_unmute);
actors_rescheduled++;
}

ponyint_sched_start_global_unmute(ctx->scheduler->index, to_unmute);
}
Expand Down

0 comments on commit dbfbd9a

Please sign in to comment.