Destruct thread manager correctly

ThreadManager is created before SensorCollector but it's members were not getting destructed correctly since the destruction logic was owned by sensor collector which would not run if sensor collector is never instantiated
The fix here moves the logic of joining the threads and stopping the scheduler within ThreadManager.

Tested:
 UT

#tlbmc

PiperOrigin-RevId: 744119296
Change-Id: I22964231ba1a1ff29bb043f075aeb98895145b12
diff --git a/tlbmc/collector/sensor_collector.cc b/tlbmc/collector/sensor_collector.cc
index 53a6627..509ad52 100644
--- a/tlbmc/collector/sensor_collector.cc
+++ b/tlbmc/collector/sensor_collector.cc
@@ -239,20 +239,18 @@
 
 }  // namespace
 
-SensorCollector::~SensorCollector() {
-  if (thread_manager_ == nullptr) {
-    return;
-  }
-  thread_manager_->task_scheduler->Stop();
-  for (auto& work_guard : thread_manager_->work_guards) {
-    work_guard.reset();
-  }
+ThreadManager::~ThreadManager() {
+  // Stop the scheduler first.
+  task_scheduler->Stop();
 
-  for (auto& io_context : thread_manager_->io_contexts) {
+  // Stop io_contexts.
+  for (const std::shared_ptr<boost::asio::io_context>& io_context :
+       io_contexts) {
     io_context->stop();
   }
 
-  for (auto& thread : thread_manager_->threads) {
+  // Finally join all threads.
+  for (const std::unique_ptr<ecclesia::ThreadInterface>& thread : threads) {
     thread->Join();
   }
 }
diff --git a/tlbmc/collector/sensor_collector.h b/tlbmc/collector/sensor_collector.h
index 8fc1293..43e8aba 100644
--- a/tlbmc/collector/sensor_collector.h
+++ b/tlbmc/collector/sensor_collector.h
@@ -30,6 +30,8 @@
   explicit ThreadManager(ecclesia::Clock* clock)
       : task_scheduler(std::make_unique<TaskScheduler>(clock)) {}
 
+  ~ThreadManager();
+
   std::vector<std::unique_ptr<ecclesia::ThreadInterface>> threads;
   std::vector<std::shared_ptr<boost::asio::io_context>> io_contexts;
   std::vector<
@@ -100,8 +102,6 @@
 
   nlohmann::json ToJson() const override;
 
-  ~SensorCollector() override;
-
  protected:
   // Protected constructor to allow mocking.
   SensorCollector() = default;