@@ -282,7 +282,7 @@ bool SonicPiAPI::StartBootDaemon()
282
282
LOG (INFO, " Setting up Boot Daemon keep alive loop" );
283
283
m_bootDaemonSockPingLoopThread = std::thread ([&]() {
284
284
auto keep_alive_msg = std::string{ " keep-alive\n " };
285
- while (true )
285
+ while (m_keep_alive. load () )
286
286
{
287
287
LOG (DBG, " SND keep_alive" );
288
288
m_bootDaemonSock->send (reinterpret_cast <const std::byte*>(keep_alive_msg.c_str ()), keep_alive_msg.size ());
@@ -302,6 +302,8 @@ SonicPiAPI::~SonicPiAPI()
302
302
303
303
void SonicPiAPI::Shutdown ()
304
304
{
305
+ std::lock_guard<std::mutex> lg (m_osc_mtx);
306
+
305
307
if (m_shutdown_engaged)
306
308
{
307
309
LOG (INFO, " Shutdown already initiated..." );
@@ -310,14 +312,6 @@ void SonicPiAPI::Shutdown()
310
312
311
313
LOG (INFO, " Initiating Shutdown" );
312
314
313
- std::lock_guard<std::mutex> lg (m_osc_mtx);
314
-
315
- if (m_shutdown_engaged)
316
- {
317
- LOG (INFO, " Shutdown already initiated.." );
318
- return ;
319
- }
320
-
321
315
m_shutdown_engaged = true ;
322
316
323
317
switch (m_state)
@@ -349,11 +343,8 @@ void SonicPiAPI::Shutdown()
349
343
LOG (INFO, " Stopping OSC server..." );
350
344
StopOscServer ();
351
345
352
- if (m_coutbuf)
353
- {
354
- std::cout.rdbuf (m_coutbuf); // reset to stdout before exiting
355
- m_coutbuf = nullptr ;
356
- }
346
+ LOG (INFO, " Stopping daemon keep alive loop" );
347
+ m_keep_alive.store (false );
357
348
}
358
349
359
350
if (m_bootDaemonSock)
@@ -366,7 +357,17 @@ void SonicPiAPI::Shutdown()
366
357
367
358
m_state = State::Reset;
368
359
LOG (INFO, " API State set to: Reset..." );
360
+ LOG (INFO, " Waiting for Daemon keep alive loop to have stopped..." );
361
+ m_bootDaemonSockPingLoopThread.join ();
362
+ m_pingerThread.join ();
363
+
364
+ LOG (INFO, " API Shutdown complete..." );
369
365
366
+ if (m_coutbuf)
367
+ {
368
+ std::cout.rdbuf (m_coutbuf); // reset to stdout before exiting
369
+ m_coutbuf = nullptr ;
370
+ }
370
371
}
371
372
372
373
bool SonicPiAPI::StartOscServer ()
@@ -456,7 +457,7 @@ bool SonicPiAPI::PingUntilServerCreated()
456
457
457
458
int timeout = 60 ;
458
459
LOG (INFO, " Waiting for Sonic Pi Spider Server to respond..." );
459
- while (m_spOscServer->waitForServer () && timeout-- > 0 )
460
+ while (m_keep_alive. load () && m_spOscServer->waitForServer () && timeout-- > 0 )
460
461
{
461
462
std::this_thread::sleep_for (1s);
462
463
LOG (INFO, " ." );
@@ -518,17 +519,6 @@ bool SonicPiAPI::Init(const fs::path& root)
518
519
return false ;
519
520
}
520
521
521
-
522
- // Start again, shutdown if we fail init
523
- m_state = State::Invalid;
524
- auto exitScope = sg::make_scope_guard ([&]() {
525
- if (m_state == State::Invalid)
526
- {
527
- LOG (DBG, " Init failure, calling shutdown" );
528
- Shutdown ();
529
- }
530
- });
531
-
532
522
// A new Guid for each initialization
533
523
#if defined(__APPLE__)
534
524
m_guid = random_string (32 );
@@ -555,6 +545,7 @@ bool SonicPiAPI::Init(const fs::path& root)
555
545
return false ;
556
546
}
557
547
548
+
558
549
// Make the log folder and check we can write to it.
559
550
// This is /usr/home/.sonic-pi/log
560
551
m_homeDirWriteable = true ;
@@ -589,6 +580,17 @@ bool SonicPiAPI::Init(const fs::path& root)
589
580
std::cout.rdbuf (m_stdlog.rdbuf ());
590
581
}
591
582
583
+
584
+ // Start again, shutdown if we fail init
585
+ m_state = State::Invalid;
586
+ auto exitScope = sg::make_scope_guard ([&]() {
587
+ if (m_state == State::Invalid)
588
+ {
589
+ LOG (DBG, " Init failure, calling shutdown" );
590
+ Shutdown ();
591
+ }
592
+ });
593
+
592
594
LOG (INFO, " Welcome to Sonic Pi" );
593
595
LOG (INFO, " ===================" );
594
596
@@ -598,7 +600,7 @@ bool SonicPiAPI::Init(const fs::path& root)
598
600
LOG (INFO, " Home dir NOT writable: " );
599
601
}
600
602
601
- LOG (INFO, " Log PAth : " + GetPath (SonicPiPath::LogPath).string ());
603
+ LOG (INFO, " Log Path : " + GetPath (SonicPiPath::LogPath).string ());
602
604
603
605
// Start the Boot Daemon
604
606
if (!StartBootDaemon ())
0 commit comments