psusensor: Check if the object is still valid in the callback functions
There is a chance that the service will crash if the sensor threshold
is changed. The root cause is that threshold changing will trigger
the deletion of sensors object, when the callback handler is executed
after objects destroyed, the variables of objects are invalid.
Adding a weak_ptr in the callback functions to check if the object
is destroyed already, only access the variables if it is valid.
Tested:
Change/query the PSU sensor threshold using ipmi commands
to trigger the sensor re-creation, the threshold config is correct,
and there is no any psusensor crash log.
Signed-off-by: Yong Li <yong.b.li@linux.intel.com>
Change-Id: Ib8169aeffde18eeaf1a2491d205a808ea15a27d9
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index 90c7950..1b363e1 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -55,7 +55,7 @@
namespace fs = std::filesystem;
-static boost::container::flat_map<std::string, std::unique_ptr<PSUSensor>>
+static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
sensors;
static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
combineEvents;
@@ -732,12 +732,12 @@
<< "\"\n";
}
- sensors[sensorName] = std::make_unique<PSUSensor>(
+ sensors[sensorName] = std::make_shared<PSUSensor>(
sensorPathStr, sensorType, objectServer, dbusConnection, io,
sensorName, std::move(sensorThresholds), *interfacePath,
findSensorType->second, factor, psuProperty->maxReading,
psuProperty->minReading, labelHead, thresholdConfSize);
-
+ sensors[sensorName]->setupRead();
++numCreated;
if constexpr (DEBUG)
{