@@ -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