Skip to content

Commit 55b12cd

Browse files
authored
Merge pull request #77 from c-jimenez/fix/multithreading_issues
Fix/multithreading issues
2 parents 7289a97 + 0ef619b commit 55b12cd

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242

4343
# Initializes the CodeQL tools for scanning.
4444
- name: Initialize CodeQL
45-
uses: github/codeql-action/init@v1
45+
uses: github/codeql-action/init@v2
4646
with:
4747
languages: ${{ matrix.language }}
4848
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -66,4 +66,4 @@ jobs:
6666
make clang-native
6767
6868
- name: Perform CodeQL Analysis
69-
uses: github/codeql-action/analyze@v1
69+
uses: github/codeql-action/analyze@v2

src/chargepoint/reservation/ReservationManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ void ReservationManager::checkExpiries()
311311
if ((connector->status == ChargePointStatus::Reserved) && (connector->reservation_expiry_date <= now))
312312
{
313313
// End reservation
314-
endReservation(connector->id, false);
314+
m_worker_pool.run<void>(std::bind(&ReservationManager::endReservation, this, connector->id, false));
315315
}
316316
}
317317
}

src/chargepoint/status/StatusManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ StatusManager::StatusManager(const ocpp::config::IChargePointConfig& sta
6767
m_boot_notification_timer(timer_pool, "Boot notification"),
6868
m_heartbeat_timer(timer_pool, "Heartbeat")
6969
{
70-
m_boot_notification_timer.setCallback(std::bind(&StatusManager::bootNotificationProcess, this));
71-
m_heartbeat_timer.setCallback(std::bind(&StatusManager::heartBeatProcess, this));
70+
m_boot_notification_timer.setCallback([this] { m_worker_pool.run<void>(std::bind(&StatusManager::bootNotificationProcess, this)); });
71+
m_heartbeat_timer.setCallback([this] { m_worker_pool.run<void>(std::bind(&StatusManager::heartBeatProcess, this)); });
7272

7373
trigger_manager.registerHandler(ocpp::types::MessageTrigger::BootNotification, *this);
7474
trigger_manager.registerHandler(ocpp::types::MessageTrigger::Heartbeat, *this);
@@ -178,7 +178,9 @@ bool StatusManager::updateConnectorStatus(unsigned int conn
178178
connector->status_timer.stop();
179179
if (connector->status != connector->last_notified_status)
180180
{
181-
connector->status_timer.setCallback([connector_id, this] { statusNotificationProcess(connector_id); });
181+
connector->status_timer.setCallback(
182+
[this, connector_id]
183+
{ m_worker_pool.run<void>(std::bind(&StatusManager::statusNotificationProcess, this, connector_id)); });
182184
connector->status_timer.start(std::chrono::milliseconds(duration), true);
183185
}
184186
}

src/rpc/RpcClient.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace rpc
3030

3131
/** @brief Constructor */
3232
RpcClient::RpcClient(ocpp::websockets::IWebsocketClient& websocket, const std::string& protocol)
33-
: RpcBase(), m_protocol(protocol), m_websocket(websocket), m_listener(nullptr), m_started(false)
33+
: RpcBase(), m_protocol(protocol), m_websocket(websocket), m_listener(nullptr), m_started(false), m_stop_mutex()
3434
{
3535
m_websocket.registerListener(*this);
3636
}
@@ -71,15 +71,22 @@ bool RpcClient::stop()
7171
{
7272
bool ret = false;
7373

74-
// Check if already started
75-
if (m_started)
74+
// Check if someone is already stopping the client
75+
// May happen in local controller mode when disconnection
76+
// can be triggered from central system side and charge point side
77+
if (m_stop_mutex.try_lock())
7678
{
77-
// Disconnect from websocket
78-
ret = m_websocket.disconnect();
79+
// Check if already started
80+
if (m_started)
81+
{
82+
// Disconnect from websocket
83+
ret = m_websocket.disconnect();
7984

80-
// Stop processing
81-
RpcBase::stop();
82-
m_started = false;
85+
// Stop processing
86+
RpcBase::stop();
87+
m_started = false;
88+
}
89+
m_stop_mutex.unlock();
8390
}
8491

8592
return ret;

src/rpc/RpcClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class RpcClient : public RpcBase, public ocpp::websockets::IWebsocketClient::ILi
115115
IListener* m_listener;
116116
/** @brief Started state */
117117
bool m_started;
118+
/** @brief Mutex for concurrent stop access */
119+
std::mutex m_stop_mutex;
118120
};
119121

120122
} // namespace rpc

src/websockets/libwebsockets/LibWebsocketClient.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.
1818

1919
#include "LibWebsocketClient.h"
2020

21+
#include <csignal>
2122
#include <cstdint>
2223
#include <functional>
2324
#include <iostream>
@@ -234,6 +235,12 @@ void LibWebsocketClient::process()
234235
// Save this pointer for further callbacks
235236
client = this;
236237

238+
// Mask SIG_PIPE signal
239+
sigset_t set;
240+
sigemptyset(&set);
241+
sigaddset(&set, SIGPIPE);
242+
pthread_sigmask(SIG_BLOCK, &set, NULL);
243+
237244
// Event loop
238245
int ret = 0;
239246
while (!m_end && (ret >= 0))

src/websockets/libwebsockets/LibWebsocketServer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.
1818

1919
#include "LibWebsocketServer.h"
2020

21+
#include <csignal>
2122
#include <cstdint>
2223
#include <functional>
2324
#include <iostream>
@@ -222,6 +223,12 @@ void LibWebsocketServer::process()
222223
// Save this pointer for further callbacks
223224
server = this;
224225

226+
// Mask SIG_PIPE signal
227+
sigset_t set;
228+
sigemptyset(&set);
229+
sigaddset(&set, SIGPIPE);
230+
pthread_sigmask(SIG_BLOCK, &set, NULL);
231+
225232
// Event loop
226233
int ret = 0;
227234
while (!m_end && (ret >= 0))

0 commit comments

Comments
 (0)