@@ -247,11 +247,10 @@ class O2PrimaryServerDevice final : public fair::mq::Device
247247 }
248248 }
249249
250- // launches a thread that listens for status requests from outside asynchronously
250+ // launches a thread that listens for status/config/shutdown requests from outside asynchronously
251251 void launchInfoThread ()
252252 {
253253 static std::vector<std::thread> threads;
254-
255254 auto sendErrorReply = [](fair::mq::Channel& channel) {
256255 LOG (error) << " UNKNOWN REQUEST" ;
257256 std::unique_ptr<fair::mq::Message> reply (channel.NewSimpleMessage ((int )(404 )));
@@ -260,7 +259,9 @@ class O2PrimaryServerDevice final : public fair::mq::Device
260259
261260 LOG (info) << " LAUNCHING STATUS THREAD" ;
262261 auto lambda = [this , sendErrorReply]() {
263- while (mState != O2PrimaryServerState::Stopped) {
262+ bool canShutdown{false };
263+ // Exit only when both: serving stopped and allowed from outside.
264+ while (!(mState == O2PrimaryServerState::Stopped && canShutdown)) {
264265 auto & channel = GetChannels ().at (" o2sim-primserv-info" ).at (0 );
265266 if (!channel.IsValid ()) {
266267 LOG (error) << " channel primserv-info not valid" ;
@@ -285,6 +286,11 @@ class O2PrimaryServerDevice final : public fair::mq::Device
285286 }
286287 } else if (request_payload == (int )O2PrimaryServerInfoRequest::Config) {
287288 HandleConfigRequest (channel);
289+ } else if (request_payload == (int )O2PrimaryServerInfoRequest::AllowShutdown) {
290+ LOG (info) << " Got info that we may shutdown" ;
291+ std::unique_ptr<fair::mq::Message> ack (channel.NewSimpleMessage (200 ));
292+ channel.Send (ack);
293+ canShutdown = true ;
288294 } else {
289295 sendErrorReply (channel);
290296 }
@@ -518,10 +524,13 @@ class O2PrimaryServerDevice final : public fair::mq::Device
518524
519525 void PostRun () override
520526 {
527+ // We shouldn't shut down immediately when all events have been served
528+ // Instead we also need to wait until the info thread running some communication server
529+ // with other processes is finished.
521530 while (!mInfoThreadStopped ) {
522531 LOG (info) << " Waiting info thread" ;
523532 using namespace std ::chrono_literals;
524- std::this_thread::sleep_for (100ms );
533+ std::this_thread::sleep_for (1000ms );
525534 }
526535 }
527536
@@ -534,7 +543,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device
534543 if (mEventCounter >= mMaxEvents && mNeedNewEvent ) {
535544 workavailable = false ;
536545 }
537- if (!(mState == O2PrimaryServerState::ReadyToServe || mState == O2PrimaryServerState::WaitingEvent)) {
546+ if (!(mState . load () == O2PrimaryServerState::ReadyToServe || mState . load () == O2PrimaryServerState::WaitingEvent)) {
538547 // send a zero answer
539548 workavailable = false ;
540549 }
0 commit comments