Index: lldb/packages/Python/lldbsuite/test/lldbtest.py =================================================================== --- lldb/packages/Python/lldbsuite/test/lldbtest.py +++ lldb/packages/Python/lldbsuite/test/lldbtest.py @@ -1058,6 +1058,13 @@ lldb.SBDebugger.Destroy(self.dbg) del self.dbg + # Modules are not orphaned during reproducer replay because they're + # leaked on purpose. + if not configuration.is_reproducer(): + # Assert that the global module cache is empty. + self.assertEqual(lldb.SBModule.GetNumberAllocatedModules(), 0) + + # ========================================================= # Various callbacks to allow introspection of test progress # ========================================================= @@ -2021,13 +2028,9 @@ for target in targets: self.dbg.DeleteTarget(target) - # Modules are not orphaned during reproducer replay because they're - # leaked on purpose. if not configuration.is_reproducer(): # Assert that all targets are deleted. - assert self.dbg.GetNumTargets() == 0 - # Assert that the global module cache is empty. - assert lldb.SBModule.GetNumberAllocatedModules() == 0 + self.assertEqual(self.dbg.GetNumTargets(), 0) # Do this last, to make sure it's in reverse order from how we setup. Base.tearDown(self) Index: lldb/source/Core/Debugger.cpp =================================================================== --- lldb/source/Core/Debugger.cpp +++ lldb/source/Core/Debugger.cpp @@ -600,10 +600,14 @@ for (pos = g_debugger_list_ptr->begin(); pos != end; ++pos) { if ((*pos).get() == debugger_sp.get()) { g_debugger_list_ptr->erase(pos); - return; + break; } } } + + // Clean any modules that are orphaned. + bool mandatory = true; + ModuleList::RemoveOrphanSharedModules(mandatory); } DebuggerSP Debugger::FindDebuggerWithInstanceName(ConstString instance_name) {