Fix NvmeSensor assert

As NvmeSensor has many async captures, deleting
the NvmeContext is not enough to make the destructor
fire, we must also manually stop all timers and
connections.

Tested: DC Cycled, assert went away

Change-Id: I64f3eab1451d73d723110a79ed5bb38419982088
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/NVMeSensor.hpp b/include/NVMeSensor.hpp
index 132ef29..5d2927a 100644
--- a/include/NVMeSensor.hpp
+++ b/include/NVMeSensor.hpp
@@ -33,6 +33,7 @@
     virtual ~NVMeContext();
 
     void pollNVMeDevices();
+    void close();
 
     boost::asio::deadline_timer scanTimer;
     int rootBus; // Root bus for this drive
diff --git a/src/NVMeSensor.cpp b/src/NVMeSensor.cpp
index d683174..15a0d14 100644
--- a/src/NVMeSensor.cpp
+++ b/src/NVMeSensor.cpp
@@ -412,7 +412,7 @@
         });
 }
 
-NVMeContext::~NVMeContext()
+void NVMeContext::close()
 {
     scanTimer.cancel();
     mctpResponseTimer.cancel();
@@ -420,6 +420,11 @@
     nvmeMCTP::closeInFd(rootBus);
 }
 
+NVMeContext::~NVMeContext()
+{
+    close();
+}
+
 NVMeSensor::NVMeSensor(sdbusplus::asio::object_server& objectServer,
                        boost::asio::io_service& io,
                        std::shared_ptr<sdbusplus::asio::connection>& conn,
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index 8a1f4bc..1e02a4c 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -42,6 +42,13 @@
         std::move([&io, &objectServer, &dbusConnection](
                       const ManagedObjectType& sensorConfigurations) {
             // todo: it'd be better to only update the ones we care about
+            for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
+            {
+                if (nvmeContextPtr)
+                {
+                    nvmeContextPtr->close();
+                }
+            }
             nvmeDeviceMap.clear();
 
             // iterate through all found configurations