Skip to content

Commit 823dfc3

Browse files
committed
[rpc - client] Add mutex for stop() method since it can be called concurrently when used in local controller
1 parent d8066c7 commit 823dfc3

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

0 commit comments

Comments
 (0)