From 49c982d9c70c8bc1f42fb27e35649cdf1952a618 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sun, 3 Mar 2024 22:09:17 +0800 Subject: [PATCH] bugfix #144: Do not free kstack when the main thread exits --- os/src/task/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 10793861..d96deb66 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -131,8 +131,13 @@ pub fn exit_current_and_run_next(exit_code: i32) { process_inner.memory_set.recycle_data_pages(); // drop file descriptors process_inner.fd_table.clear(); - // remove all tasks - process_inner.tasks.clear(); + // Remove all tasks except for the main thread itself. + // This is because we are still using the kstack under the TCB + // of the main thread. This TCB, including its kstack, will be + // deallocated when the process is reaped via waitpid. + while process_inner.tasks.len() > 1 { + process_inner.tasks.pop(); + } } drop(process); // we do not have to save task context