mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 18:15:06 +00:00
Kernel: Always return from Thread::wait_on
We need to always return from Thread::wait_on, even when a thread is being killed. This is necessary so that the kernel call stack can clean up and release references held by it. Then, right before transitioning back to user mode, we check if the thread is supposed to die, and at that point change the thread state to Dying to prevent further scheduling of this thread. This addresses some possible resource leaks similar to #3073
This commit is contained in:
parent
1f7190d3bd
commit
49d5232f33
4 changed files with 28 additions and 33 deletions
|
@ -365,6 +365,21 @@ bool Scheduler::pick_next()
|
|||
|
||||
ScopedSpinLock lock(g_scheduler_lock);
|
||||
|
||||
if (current_thread->should_die() && current_thread->state() == Thread::Running) {
|
||||
// Rather than immediately killing threads, yanking the kernel stack
|
||||
// away from them (which can lead to e.g. reference leaks), we always
|
||||
// allow Thread::wait_on to return. This allows the kernel stack to
|
||||
// clean up and eventually we'll get here shortly before transitioning
|
||||
// back to user mode (from Processor::exit_trap). At this point we
|
||||
// no longer want to schedule this thread. We can't wait until
|
||||
// Scheduler::enter_current because we don't want to allow it to
|
||||
// transition back to user mode.
|
||||
#ifdef SCHEDULER_DEBUG
|
||||
dbg() << "Scheduler[" << Processor::current().id() << "]: Thread " << *current_thread << " is dying";
|
||||
#endif
|
||||
current_thread->set_state(Thread::Dying);
|
||||
}
|
||||
|
||||
// Check and unblock threads whose wait conditions have been met.
|
||||
Scheduler::for_each_nonrunnable([&](Thread& thread) {
|
||||
thread.consider_unblock(now_sec, now_usec);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue