summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-ast2500/recipes-phosphor/sensors/dbus-sensors/0020-ExitAirTemp-fix-use-weak_ptr-to-in-async-handler.patch
blob: c0f8c5c92037ecee41b63ed78abe513c4e869e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
From efb007d288530ce6ec8a319488422fbccb521edd Mon Sep 17 00:00:00 2001
From: Zhikui Ren <zhikui.ren@intel.com>
Date: Tue, 9 Mar 2021 20:45:35 -0800
Subject: ExitAirTemp fix: use weak_ptr to in async handler

Replace shared_ptr with weak_ptr in async handler's capture.
CFMSensor and ExitAirTempSensor are properly deleted before
new instances are created.

Tested:
Run dc cycle test and no more memory leak or dbus connection timeout.

Signed-off-by Zhikui Ren <zhikui.ren@intel.com>
---
 src/ExitAirTempSensor.cpp | 102 +++++++++++++++++++++++++++++---------
 1 file changed, 79 insertions(+), 23 deletions(-)

diff --git a/src/ExitAirTempSensor.cpp b/src/ExitAirTempSensor.cpp
index 4661aeb..1ee9301 100644
--- a/src/ExitAirTempSensor.cpp
+++ b/src/ExitAirTempSensor.cpp
@@ -208,10 +208,16 @@ CFMSensor::CFMSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
 void CFMSensor::setupMatches()
 {
 
-    std::shared_ptr<CFMSensor> self = shared_from_this();
+    std::weak_ptr<CFMSensor> weakRef = weak_from_this();
     setupSensorMatch(
         matches, *dbusConnection, "fan_tach",
-        [self](const double& value, sdbusplus::message::message& message) {
+        [weakRef](const double& value, sdbusplus::message::message& message) {
+            std::shared_ptr<CFMSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             self->tachReadings[message.get_path()] = value;
             if (self->tachRanges.find(message.get_path()) ==
                 self->tachRanges.end())
@@ -226,8 +232,15 @@ void CFMSensor::setupMatches()
         });
 
     dbusConnection->async_method_call(
-        [self](const boost::system::error_code ec,
-               const std::variant<double> cfmVariant) {
+        [weakRef](const boost::system::error_code ec,
+                  const std::variant<double> cfmVariant) {
+            std::shared_ptr<CFMSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
+
             uint64_t maxRpm = 100;
             if (!ec)
             {
@@ -252,7 +265,13 @@ void CFMSensor::setupMatches()
         "freedesktop.DBus.Properties',path='" +
             std::string(cfmSettingPath) + "',arg0='" +
             std::string(cfmSettingIface) + "'",
-        [self](sdbusplus::message::message& message) {
+        [weakRef](sdbusplus::message::message& message) {
+            std::shared_ptr<CFMSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             boost::container::flat_map<std::string, std::variant<double>>
                 values;
             std::string objectName;
@@ -298,18 +317,24 @@ void CFMSensor::createMaxCFMIface(void)
 void CFMSensor::addTachRanges(const std::string& serviceName,
                               const std::string& path)
 {
-    std::shared_ptr<CFMSensor> self = shared_from_this();
+    std::weak_ptr<CFMSensor> weakRef = weak_from_this();
     dbusConnection->async_method_call(
-        [self, path](const boost::system::error_code ec,
-                     const boost::container::flat_map<std::string,
-                                                      BasicVariantType>& data) {
+        [weakRef,
+         path](const boost::system::error_code ec,
+               const boost::container::flat_map<std::string, BasicVariantType>&
+                   data) {
             if (ec)
             {
                 std::cerr << "Error getting properties from " << path << "\n";
                 std::cerr << ec.message() << "\n";
                 return;
             }
-
+            std::shared_ptr<CFMSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             double max = loadVariant<double>(data, "MaxValue");
             double min = loadVariant<double>(data, "MinValue");
             self->tachRanges[path] = std::make_pair(min, max);
@@ -544,13 +569,19 @@ void ExitAirTempSensor::setupMatches(void)
     constexpr const std::array<const char*, 2> matchTypes = {
         "power", inletTemperatureSensor};
 
-    std::shared_ptr<ExitAirTempSensor> self = shared_from_this();
+    std::weak_ptr<ExitAirTempSensor> weakRef = weak_from_this();
     for (const std::string& type : matchTypes)
     {
         setupSensorMatch(
             matches, *dbusConnection, type,
-            [self, type](const double& value,
-                         sdbusplus::message::message& message) {
+            [weakRef, type](const double& value,
+                            sdbusplus::message::message& message) {
+                std::shared_ptr<ExitAirTempSensor> self = weakRef.lock();
+                if (!self)
+                {
+                    // we have been deleted
+                    return;
+                }
                 if (type == "power")
                 {
                     std::string path = message.get_path();
@@ -579,8 +610,14 @@ void ExitAirTempSensor::setupMatches(void)
             });
     }
     dbusConnection->async_method_call(
-        [self](boost::system::error_code ec,
-               const std::variant<double>& value) {
+        [weakRef](boost::system::error_code ec,
+                  const std::variant<double>& value) {
+            std::shared_ptr<ExitAirTempSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             if (ec)
             {
                 // sensor not ready yet
@@ -593,7 +630,13 @@ void ExitAirTempSensor::setupMatches(void)
         std::string("/xyz/openbmc_project/sensors/") + inletTemperatureSensor,
         properties::interface, properties::get, sensorValueInterface, "Value");
     dbusConnection->async_method_call(
-        [self](boost::system::error_code ec, const GetSubTreeType& subtree) {
+        [weakRef](boost::system::error_code ec, const GetSubTreeType& subtree) {
+            std::shared_ptr<ExitAirTempSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             if (ec)
             {
                 std::cerr << "Error contacting mapper\n";
@@ -614,8 +657,15 @@ void ExitAirTempSensor::setupMatches(void)
                 {
                     const std::string& path = item.first;
                     self->dbusConnection->async_method_call(
-                        [self, path](boost::system::error_code ec,
-                                     const std::variant<double>& value) {
+                        [weakRef, path](boost::system::error_code ec,
+                                        const std::variant<double>& value) {
+                            std::shared_ptr<ExitAirTempSensor> self =
+                                weakRef.lock();
+                            if (!self)
+                            {
+                                // we have been deleted
+                                return;
+                            }
                             if (ec)
                             {
                                 std::cerr << "Error getting value from " << path
@@ -644,18 +694,24 @@ void ExitAirTempSensor::setupMatches(void)
 void ExitAirTempSensor::addPowerRanges(const std::string& serviceName,
                                        const std::string& path)
 {
-    std::shared_ptr<ExitAirTempSensor> self = shared_from_this();
+    std::weak_ptr<ExitAirTempSensor> weakRef = weak_from_this();
     dbusConnection->async_method_call(
-        [self, path](const boost::system::error_code ec,
-                     const boost::container::flat_map<std::string,
-                                                      BasicVariantType>& data) {
+        [weakRef,
+         path](const boost::system::error_code ec,
+               const boost::container::flat_map<std::string, BasicVariantType>&
+                   data) {
             if (ec)
             {
                 std::cerr << "Error getting properties from " << path << "\n";
                 std::cerr << ec.message() << "\n";
                 return;
             }
-
+            std::shared_ptr<ExitAirTempSensor> self = weakRef.lock();
+            if (!self)
+            {
+                // we have been deleted
+                return;
+            }
             double max = loadVariant<double>(data, "MaxValue");
             double min = loadVariant<double>(data, "MinValue");
             self->powerRanges[path] = std::make_pair(min, max);
-- 
2.17.1