Skip to content

Commit b76fd1e

Browse files
authored
[lldb] Avoid deadlock by unlocking before invoking callbacks (llvm#86888)
Avoid deadlocks in the Alarm class by releasing the lock before invoking callbacks. This deadlock manifested itself in the ProgressManager: 1. On the main thread, the ProgressManager acquires its lock in ProgressManager::Decrement and calls Alarm::Create. 2. On the main thread, the Alarm acquires its lock in Alarm::Create. 3. On the alarm thread, the Alarm acquires its lock after waiting on the condition variable and calls ProgressManager::Expire. 4. On the alarm thread, the ProgressManager acquires its lock in ProgressManager::Expire. Note how the two threads are acquiring the locks in different orders. Deadlocks can be avoided by always acquiring locks in the same order, but since the two mutexes here are private implementation details, belong to different classes, that's not straightforward. Luckily, we don't need to have the Alarm mutex locked when invoking the callbacks. That exactly how this patch solves the issue.
1 parent 14ba782 commit b76fd1e

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

lldb/source/Host/common/Alarm.cpp

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -154,54 +154,60 @@ lldb::thread_result_t Alarm::AlarmThread() {
154154
//
155155
// Below we only deal with the timeout expiring and fall through for dealing
156156
// with the rest.
157-
std::unique_lock<std::mutex> alarm_lock(m_alarm_mutex);
158-
if (next_alarm) {
159-
if (!m_alarm_cv.wait_until(alarm_lock, *next_alarm, predicate)) {
160-
// The timeout for the next alarm expired.
161-
162-
// Clear the next timeout to signal that we need to recompute the next
163-
// timeout.
164-
next_alarm.reset();
165-
166-
// Iterate over all the callbacks. Call the ones that have expired
167-
// and remove them from the list.
168-
const TimePoint now = std::chrono::system_clock::now();
169-
auto it = m_entries.begin();
170-
while (it != m_entries.end()) {
171-
if (it->expiration <= now) {
172-
it->callback();
173-
it = m_entries.erase(it);
174-
} else {
175-
it++;
157+
llvm::SmallVector<Callback, 1> callbacks;
158+
{
159+
std::unique_lock<std::mutex> alarm_lock(m_alarm_mutex);
160+
if (next_alarm) {
161+
if (!m_alarm_cv.wait_until(alarm_lock, *next_alarm, predicate)) {
162+
// The timeout for the next alarm expired.
163+
164+
// Clear the next timeout to signal that we need to recompute the next
165+
// timeout.
166+
next_alarm.reset();
167+
168+
// Iterate over all the callbacks. Call the ones that have expired
169+
// and remove them from the list.
170+
const TimePoint now = std::chrono::system_clock::now();
171+
auto it = m_entries.begin();
172+
while (it != m_entries.end()) {
173+
if (it->expiration <= now) {
174+
callbacks.emplace_back(std::move(it->callback));
175+
it = m_entries.erase(it);
176+
} else {
177+
it++;
178+
}
176179
}
177180
}
181+
} else {
182+
m_alarm_cv.wait(alarm_lock, predicate);
178183
}
179-
} else {
180-
m_alarm_cv.wait(alarm_lock, predicate);
181-
}
182184

183-
// Fall through after waiting on the condition variable. At this point
184-
// either the predicate is true or we woke up because an alarm expired.
185+
// Fall through after waiting on the condition variable. At this point
186+
// either the predicate is true or we woke up because an alarm expired.
185187

186-
// The alarm thread is shutting down.
187-
if (m_exit) {
188-
exit = true;
189-
if (m_run_callbacks_on_exit) {
190-
for (Entry &entry : m_entries)
191-
entry.callback();
188+
// The alarm thread is shutting down.
189+
if (m_exit) {
190+
exit = true;
191+
if (m_run_callbacks_on_exit) {
192+
for (Entry &entry : m_entries)
193+
callbacks.emplace_back(std::move(entry.callback));
194+
}
192195
}
193-
continue;
194-
}
195196

196-
// A new alarm was added or an alarm expired. Either way we need to
197-
// recompute when this thread should wake up for the next alarm.
198-
if (m_recompute_next_alarm || !next_alarm) {
199-
for (Entry &entry : m_entries) {
200-
if (!next_alarm || entry.expiration < *next_alarm)
201-
next_alarm = entry.expiration;
197+
// A new alarm was added or an alarm expired. Either way we need to
198+
// recompute when this thread should wake up for the next alarm.
199+
if (m_recompute_next_alarm || !next_alarm) {
200+
for (Entry &entry : m_entries) {
201+
if (!next_alarm || entry.expiration < *next_alarm)
202+
next_alarm = entry.expiration;
203+
}
204+
m_recompute_next_alarm = false;
202205
}
203-
m_recompute_next_alarm = false;
204206
}
207+
208+
// Outside the lock, call the callbacks.
209+
for (Callback &callback : callbacks)
210+
callback();
205211
}
206212
return {};
207213
}

0 commit comments

Comments
 (0)