fix scheduler memory error on exit
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user