Skip to content

Commit d7cf23a

Browse files
committed
Fix incorrect WaitForStateUpdate behavior
This routine must throw an exception if the thread is terminated during a wait. Ensure that `BasicThread::OnStop` signals the state mutex when it's called to cause any waiters to wake up.
1 parent 6155307 commit d7cf23a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/autowiring/BasicThread.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ bool BasicThread::OnStart(void) {
145145

146146
void BasicThread::OnStop(bool graceful) {
147147
// If we were never started, we need to set our completed flag to true
148-
if (!m_wasStarted)
148+
if (!m_wasStarted) {
149+
std::lock_guard<std::mutex> lk(m_state->m_lock);
149150
m_state->m_completed = true;
151+
}
152+
153+
// State condition must be notified, in the event that anything is blocking
154+
m_state->m_stateCondition.notify_all();
150155

151156
// Always invoke stop handler:
152157
OnStop();

src/autowiring/test/BasicThreadTest.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,23 @@ TEST_F(BasicThreadTest, IsMainThread) {
7575
);
7676
ASSERT_FALSE(secondaryIsMain.get()) << "Secondary thread incorrectly identified as the main thread";
7777
}
78+
79+
namespace {
80+
class WaitsForStateUpdate :
81+
public BasicThread
82+
{
83+
public:
84+
void Run(void) override {
85+
WaitForStateUpdate([] { return false; });
86+
}
87+
};
88+
}
89+
90+
TEST_F(BasicThreadTest, WaitForStateUpdateExits) {
91+
AutoRequired<WaitsForStateUpdate> wfsu;
92+
AutoCurrentContext ctxt;
93+
ctxt->Initiate();
94+
95+
ctxt->SignalShutdown();
96+
ASSERT_TRUE(ctxt->Wait(std::chrono::seconds{ 10 }));
97+
}

0 commit comments

Comments
 (0)