diff --git a/src/runtime2/scheduler.rs b/src/runtime2/scheduler.rs index 4aea4d03e2928f658de0827159de084f48b5e5dc..491e9f411457e73f065101ee360593593b2cdc71 100644 --- a/src/runtime2/scheduler.rs +++ b/src/runtime2/scheduler.rs @@ -89,7 +89,7 @@ impl Scheduler { CompScheduling::Sleep => { self.mark_component_as_sleeping(comp_key, component); }, CompScheduling::Exit => { component.component.on_shutdown(&scheduler_ctx); - self.mark_component_as_exiting(&scheduler_ctx, component); + self.mark_component_as_exiting(&scheduler_ctx, comp_key, component); } } } @@ -120,16 +120,14 @@ impl Scheduler { /// Marks the component as exiting by removing the reference it holds to /// itself. Afterward the component will enter "normal" sleeping mode (if it /// has not yet been destroyed) - fn mark_component_as_exiting(&self, sched_ctx: &SchedulerCtx, component: &mut RuntimeComp) { + fn mark_component_as_exiting(&self, sched_ctx: &SchedulerCtx, comp_key: CompKey, component: &mut RuntimeComp) { // If we didn't yet decrement our reference count, do so now - let comp_key = unsafe{ component.ctx.id.upgrade() }; - if !component.exiting { component.exiting = true; - let old_count = component.public.num_handles.fetch_sub(1, Ordering::AcqRel); - let new_count = old_count - 1; - if new_count == 0 { + let maybe_comp_key = component.ctx.remove_self_reference(); + if let Some(_comp_key) = maybe_comp_key { + debug_assert_eq!(_comp_key.0, comp_key.0); sched_ctx.runtime.destroy_component(comp_key); return; }