Skip to content

Commit b43864b

Browse files
committed
API - properly stop and join on background worker threads
1 parent c9c8c6a commit b43864b

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

app/api/include/api/sonicpi_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ class SonicPiAPI
339339

340340
std::mutex m_osc_mtx;
341341
bool m_shutdown_engaged = false;
342+
std::atomic<bool> m_keep_alive = { true };
342343
LogOption m_logOption;
343344

344345

app/api/src/sonicpi_api.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ bool SonicPiAPI::StartBootDaemon()
282282
LOG(INFO, "Setting up Boot Daemon keep alive loop");
283283
m_bootDaemonSockPingLoopThread = std::thread([&]() {
284284
auto keep_alive_msg = std::string{ "keep-alive\n" };
285-
while(true)
285+
while(m_keep_alive.load())
286286
{
287287
LOG(DBG, "SND keep_alive");
288288
m_bootDaemonSock->send(reinterpret_cast<const std::byte*>(keep_alive_msg.c_str()), keep_alive_msg.size());
@@ -302,6 +302,8 @@ SonicPiAPI::~SonicPiAPI()
302302

303303
void SonicPiAPI::Shutdown()
304304
{
305+
std::lock_guard<std::mutex> lg(m_osc_mtx);
306+
305307
if(m_shutdown_engaged)
306308
{
307309
LOG(INFO, "Shutdown already initiated...");
@@ -310,14 +312,6 @@ void SonicPiAPI::Shutdown()
310312

311313
LOG(INFO, "Initiating Shutdown");
312314

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-
321315
m_shutdown_engaged = true;
322316

323317
switch(m_state)
@@ -349,11 +343,8 @@ void SonicPiAPI::Shutdown()
349343
LOG(INFO, "Stopping OSC server...");
350344
StopOscServer();
351345

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);
357348
}
358349

359350
if (m_bootDaemonSock)
@@ -366,7 +357,17 @@ void SonicPiAPI::Shutdown()
366357

367358
m_state = State::Reset;
368359
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...");
369365

366+
if (m_coutbuf)
367+
{
368+
std::cout.rdbuf(m_coutbuf); // reset to stdout before exiting
369+
m_coutbuf = nullptr;
370+
}
370371
}
371372

372373
bool SonicPiAPI::StartOscServer()
@@ -456,7 +457,7 @@ bool SonicPiAPI::PingUntilServerCreated()
456457

457458
int timeout = 60;
458459
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)
460461
{
461462
std::this_thread::sleep_for(1s);
462463
LOG(INFO, ".");
@@ -518,17 +519,6 @@ bool SonicPiAPI::Init(const fs::path& root)
518519
return false;
519520
}
520521

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-
532522
// A new Guid for each initialization
533523
#if defined(__APPLE__)
534524
m_guid = random_string(32);
@@ -555,6 +545,7 @@ bool SonicPiAPI::Init(const fs::path& root)
555545
return false;
556546
}
557547

548+
558549
// Make the log folder and check we can write to it.
559550
// This is /usr/home/.sonic-pi/log
560551
m_homeDirWriteable = true;
@@ -589,6 +580,17 @@ bool SonicPiAPI::Init(const fs::path& root)
589580
std::cout.rdbuf(m_stdlog.rdbuf());
590581
}
591582

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+
592594
LOG(INFO, "Welcome to Sonic Pi");
593595
LOG(INFO, "===================");
594596

@@ -598,7 +600,7 @@ bool SonicPiAPI::Init(const fs::path& root)
598600
LOG(INFO, "Home dir NOT writable: ");
599601
}
600602

601-
LOG(INFO, "Log PAth: " + GetPath(SonicPiPath::LogPath).string());
603+
LOG(INFO, "Log Path: " + GetPath(SonicPiPath::LogPath).string());
602604

603605
// Start the Boot Daemon
604606
if (!StartBootDaemon())

0 commit comments

Comments
 (0)