Skip to content

Commit 9a20ea9

Browse files
committed
[occ] Try to make plugin unsubscribe from FairMQ at right time
1 parent 3e8163f commit 9a20ea9

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

occ/plugin/OccFMQCommon.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ std::tuple<OccLite::nopb::TransitionResponse, ::grpc::Status> doTransition(fair:
148148

149149
m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
150150
DEFER({
151-
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
151+
if ( !newStates.empty() && newStates.back() != "EXITING") {
152+
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
153+
}
152154
});
153155

154156
try {
@@ -220,6 +222,7 @@ std::tuple<OccLite::nopb::TransitionResponse, ::grpc::Status> doTransition(fair:
220222
}
221223

222224
if (newStates.back() == "EXITING") {
225+
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
223226
m_pluginServices->ReleaseDeviceControl(FMQ_CONTROLLER_NAME);
224227
OLOG(debug) << "releasing device control";
225228
}

occ/plugin/OccLiteServer.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ OccLite::Service::EventStream(::grpc::ServerContext* context, const OccLite::nop
122122
std::mutex writer_mu;
123123
std::condition_variable finished;
124124
std::mutex finished_mu;
125+
std::string last_known_state;
125126

126127
auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
127128
std::lock_guard<std::mutex> lock(writer_mu);
128129
auto state = fair::mq::PluginServices::ToStr(reachedState);
130+
last_known_state = state;
129131

130132
OLOG(debug) << "[EventStream] new state: " << state;
131133

@@ -147,7 +149,9 @@ OccLite::Service::EventStream(::grpc::ServerContext* context, const OccLite::nop
147149

148150
m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
149151
DEFER({
150-
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
152+
if (last_known_state == "EXITING") {
153+
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
154+
}
151155
});
152156

153157
{

occ/plugin/OccPluginServer.cxx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ OccPluginServer::EventStream(grpc::ServerContext* context,
5858
std::mutex writer_mu;
5959
std::condition_variable finished;
6060
std::mutex finished_mu;
61+
std::string last_known_state;
6162

6263
auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
6364
std::lock_guard<std::mutex> lock(writer_mu);
6465
auto state = fair::mq::PluginServices::ToStr(reachedState);
66+
last_known_state = state
6567

6668
OLOG(debug) << "[EventStream] new state: " << state;
6769

@@ -83,7 +85,9 @@ OccPluginServer::EventStream(grpc::ServerContext* context,
8385

8486
m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
8587
DEFER({
86-
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
88+
if (last_known_state == "EXITING") {
89+
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
90+
}
8791
});
8892

8993
{
@@ -106,10 +110,12 @@ OccPluginServer::StateStream(grpc::ServerContext* context,
106110
std::mutex writer_mu;
107111
std::condition_variable finished;
108112
std::mutex finished_mu;
113+
std::string last_known_state;
109114

110115
auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
111116
std::lock_guard<std::mutex> lock(writer_mu);
112117
auto state = fair::mq::PluginServices::ToStr(reachedState);
118+
last_known_state = state
113119
pb::StateType sType = isIntermediateFMQState(state) ? pb::STATE_INTERMEDIATE : pb::STATE_STABLE;
114120

115121
pb::StateStreamReply response;
@@ -132,7 +138,9 @@ OccPluginServer::StateStream(grpc::ServerContext* context,
132138

133139
m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
134140
DEFER({
135-
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
141+
if (last_known_state == "EXITING") {
142+
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
143+
}
136144
});
137145

138146
{

0 commit comments

Comments
 (0)