Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h +++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h @@ -380,12 +380,22 @@ uint32_t IsExecutingPython() const { return m_lock_count > 0; } - uint32_t IncrementLockCount() { return ++m_lock_count; } + uint32_t IncrementLockCount() { + // std::atomic::fetch_add is an atomic post-increment operation so we need + // to add 1 to its return value. + return m_lock_count.fetch_add(1, std::memory_order_relaxed) + 1; + } uint32_t DecrementLockCount() { - if (m_lock_count > 0) - --m_lock_count; - return m_lock_count; + // std::atomic::fetch_sub is an atomic post-decrement operation so we need + // to subtract 1 from its return value. + uint32_t count = m_lock_count.fetch_sub(1, std::memory_order_relaxed) - 1; + if (static_cast(count) < 0) { + // Handle underflow here & reset count to zero. + count = 0; + m_lock_count.store(count, std::memory_order_relaxed); + } + return count; } enum ActiveIOHandler { @@ -421,7 +431,7 @@ bool m_session_is_active; bool m_pty_secondary_is_open; bool m_valid_session; - uint32_t m_lock_count; + std::atomic m_lock_count; PyThreadState *m_command_thread_state; }; Index: lldb/source/Target/Process.cpp =================================================================== --- lldb/source/Target/Process.cpp +++ lldb/source/Target/Process.cpp @@ -2467,6 +2467,7 @@ } void Process::LoadOperatingSystemPlugin(bool flush) { + std::lock_guard guard(m_thread_mutex); if (flush) m_thread_list.Clear(); m_os_up.reset(OperatingSystem::FindPlugin(this, nullptr));