From bafaff61c9b7b870cacbf61f6fb1a1f26e5d27b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 6 Apr 2022 14:55:20 +0200 Subject: [PATCH] SystemMonitor: Fallback to invalid model index if there's no main thread In the process model we check the thread with tid=pid to figure out the main thread of a process. This is used to construct the process view tree with non-main threads listed as children of the process row. However, there are sometimes circumstances where there is no main thread, even though the process should have been removed from the internal list by then. As a safe fallback, let's default to an invalid model index if we can't figure out what the main thread of a process is. --- Userland/Applications/SystemMonitor/ProcessModel.cpp | 8 ++++++-- Userland/Applications/SystemMonitor/ProcessModel.h | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/SystemMonitor/ProcessModel.cpp b/Userland/Applications/SystemMonitor/ProcessModel.cpp index 98f31612de..03322e6d24 100644 --- a/Userland/Applications/SystemMonitor/ProcessModel.cpp +++ b/Userland/Applications/SystemMonitor/ProcessModel.cpp @@ -344,7 +344,9 @@ GUI::ModelIndex ProcessModel::index(int row, int column, GUI::ModelIndex const& if (row >= static_cast(m_processes.size())) return {}; auto corresponding_thread = m_processes[row].main_thread(); - return create_index(row, column, corresponding_thread.ptr()); + if (!corresponding_thread.has_value()) + return {}; + return create_index(row, column, corresponding_thread.release_value().ptr()); } // Thread under process. auto const& parent_thread = *static_cast(parent.internal_data()); @@ -375,8 +377,10 @@ GUI::ModelIndex ProcessModel::parent_index(GUI::ModelIndex const& index) const return {}; // FIXME: We can't use first_matching here (not even a const version) because Optional cannot contain references. auto const& parent = thread.current_state.process; + if (!parent.main_thread().has_value()) + return {}; - return create_index(m_processes.find_first_index(parent).release_value(), index.column(), parent.main_thread().ptr()); + return create_index(m_processes.find_first_index(parent).release_value(), index.column(), parent.main_thread().value().ptr()); } Vector ProcessModel::matches(StringView searching, unsigned flags, GUI::ModelIndex const&) diff --git a/Userland/Applications/SystemMonitor/ProcessModel.h b/Userland/Applications/SystemMonitor/ProcessModel.h index 93f7618f46..d8daecbeef 100644 --- a/Userland/Applications/SystemMonitor/ProcessModel.h +++ b/Userland/Applications/SystemMonitor/ProcessModel.h @@ -210,9 +210,9 @@ private: return this->pid == other.pid; } - NonnullRefPtr main_thread() const + Optional> main_thread() const { - return *threads.first_matching([this](auto const thread) { return thread->current_state.tid == pid; }).value(); + return threads.first_matching([this](auto const thread) { return thread->current_state.tid == pid; }); } // Return anything but the main thread; therefore, valid indices are anything up to threads.size()-1 exclusive.