fix scheduler memory error on exit

This commit is contained in:
2026-02-20 18:42:21 -06:00
parent ce2e11429f
commit eeb2f038a1
2 changed files with 35 additions and 5 deletions

View File

@@ -330,6 +330,35 @@ void actor_free(cell_rt *actor)
}
lockless_shdel(actors, actor->id);
// Purge any pending timers referencing this actor from the timer heap
// to prevent the timer thread from accessing freed memory.
{
pthread_mutex_lock(&engine.lock);
int n = arrlen(timer_heap);
int w = 0;
for (int r = 0; r < n; r++) {
if (timer_heap[r].actor != actor)
timer_heap[w++] = timer_heap[r];
}
arrsetlen(timer_heap, w);
for (int i = w / 2 - 1; i >= 0; i--) {
int j = i;
while (1) {
int left = 2 * j + 1, right = 2 * j + 2, smallest = j;
if (left < w && timer_heap[left].execute_at_ns < timer_heap[smallest].execute_at_ns)
smallest = left;
if (right < w && timer_heap[right].execute_at_ns < timer_heap[smallest].execute_at_ns)
smallest = right;
if (smallest == j) break;
timer_node tmp = timer_heap[j];
timer_heap[j] = timer_heap[smallest];
timer_heap[smallest] = tmp;
j = smallest;
}
}
pthread_mutex_unlock(&engine.lock);
}
// Do not go forward with actor destruction until the actor is completely free
pthread_mutex_lock(actor->msg_mutex);
pthread_mutex_lock(actor->mutex);