Skip to content

Commit e25ce33

Browse files
committed
API - ensure mutex is unlocked
also obtain lock in all places m_state is modified
1 parent 0d18053 commit e25ce33

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

app/api/src/sonicpi_api.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ SonicPiAPI::~SonicPiAPI()
303303
void SonicPiAPI::Shutdown()
304304
{
305305
LOG(INFO, "Shutdown");
306+
std::lock_guard<std::mutex> lg(m_osc_mtx);
306307

307308
switch(m_state)
308309
{
@@ -396,7 +397,7 @@ bool SonicPiAPI::WaitUntilReady()
396397
{
397398
if (m_state == State::Created)
398399
{
399-
return true;
400+
return true;
400401
}
401402

402403
int num_tries = 60;
@@ -424,10 +425,12 @@ bool SonicPiAPI::WaitUntilReady()
424425
bool SonicPiAPI::PingUntilServerCreated()
425426
{
426427
LOG(INFO, "Pinging Spider Server until a response is received...");
428+
std::lock_guard<std::mutex> lg(m_osc_mtx);
429+
427430
if (m_state == State::Created)
428431
{
429-
return true;
430432
LOG(ERR, "Error! No need to ping server as it's already created!");
433+
return true;
431434
}
432435

433436
if (m_state != State::Initializing)
@@ -495,6 +498,8 @@ bool SonicPiAPI::Init(const fs::path& root)
495498

496499
m_pClient->Report(message);
497500
LOG(ERR, "Call shutdown before init!");
501+
m_state = State::Error;
502+
m_osc_mtx.unlock();
498503
return false;
499504
}
500505

@@ -523,12 +528,15 @@ bool SonicPiAPI::Init(const fs::path& root)
523528
message.text = "Could not find root path: " + root.string();
524529

525530
m_pClient->Report(message);
531+
m_state = State::Error;
532+
m_osc_mtx.unlock();
526533
return false;
527534
}
528535

529536
if (!InitializePaths(root))
530537
{
531538
// oh no, something went wrong :-(
539+
m_osc_mtx.unlock();
532540
return false;
533541
}
534542

@@ -577,16 +585,17 @@ bool SonicPiAPI::Init(const fs::path& root)
577585

578586
LOG(INFO, "Log PAth: " + GetPath(SonicPiPath::LogPath).string());
579587

580-
581588
// Start the Boot Daemon
582589
if (!StartBootDaemon())
583590
{
591+
m_osc_mtx.unlock();
584592
return false;
585593
}
586594

587595
// Start the OC Server
588596
if(!StartOscServer())
589597
{
598+
m_osc_mtx.unlock();
590599
return false;
591600
}
592601

@@ -596,6 +605,7 @@ bool SonicPiAPI::Init(const fs::path& root)
596605
LOG(INFO, "API State set to: Initializing...");
597606

598607
m_osc_mtx.unlock();
608+
599609
LOG(INFO, "Going to start pinging server...");
600610
m_pingerThread = std::thread([&]() {
601611
PingUntilServerCreated();

0 commit comments

Comments
 (0)