diff --git a/akka-actor/src/main/scala/akka/dispatch/MessageHandling.scala b/akka-actor/src/main/scala/akka/dispatch/MessageHandling.scala index cb102ba30d..6ac852d0cc 100644 --- a/akka-actor/src/main/scala/akka/dispatch/MessageHandling.scala +++ b/akka-actor/src/main/scala/akka/dispatch/MessageHandling.scala @@ -78,18 +78,22 @@ trait MessageDispatcher extends Logging { } else throw new IllegalActorStateException("Can't submit invocations to dispatcher since it's not started") private[akka] final def dispatchFuture(invocation: FutureInvocation): Unit = { - futures add invocation.uuid - if (active.isOff) { active.switchOn { start } } + guard withGuard { + futures add invocation.uuid + if (active.isOff) { active.switchOn { start } } + } invocation.future.onComplete { f => - futures remove invocation.uuid - if (futures.isEmpty && uuids.isEmpty) { - shutdownSchedule match { - case UNSCHEDULED => - shutdownSchedule = SCHEDULED - Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS) - case SCHEDULED => - shutdownSchedule = RESCHEDULED - case RESCHEDULED => //Already marked for reschedule + guard withGuard { + futures remove invocation.uuid + if (futures.isEmpty && uuids.isEmpty) { + shutdownSchedule match { + case UNSCHEDULED => + shutdownSchedule = SCHEDULED + Scheduler.scheduleOnce(shutdownAction, timeoutMs, TimeUnit.MILLISECONDS) + case SCHEDULED => + shutdownSchedule = RESCHEDULED + case RESCHEDULED => //Already marked for reschedule + } } } }