[Lldb-commits] [lldb] [lldb] Give thread specific async interrupt requests a lifetime (PR #224272)
Raphael Isemann via lldb-commits
lldb-commits at lists.llvm.org
Thu Sep 17 04:35:20 PDT 2026
https://github.com/Teemperor created https://github.com/llvm/llvm-project/pull/224272
ThreadPlanSingleThreadTimeout asks Process to interrupt the inferior when single thread stepping doesn't make progress within the timeout. That request was an immediately visible state Process::m_interrupt_tid change and a queued eBroadcastBitInterrupt event that was never invalidated, so if the inferior stopped on its own between the timer firing and the private state thread acting on the event, two things went wrong:
1. The natural stop (a trace or breakpoint stop) was rewritten into eStopReasonInterrupt by ProcessGDBRemote::HandleThreadAsyncInterrupt(), because m_interrupt_tid was already set. The timeout plan then claimed a stop it had not caused, and the real stop reason was lost.
2. The still queued interrupt event was processed after the next resume and halted the process a second time. By then the timeout plan had been popped and re-pushed in state WaitTimeout, so no plan explained the stop and it was reported to the user, aborting the step in the middle of a source line.
On a heavily loaded machine, where processing one internal stop can take longer than the timeout, this makes TestSingleThreadStepTimeout.py fail with e.g. "Stepped to line 63 instead of expected 65".
This patch gives the request a lifetime to fix this issue. SendAsyncInterrupt() now only records the requesting thread and the current stop ID in m_pending_interrupt. The request becomes visible as m_interrupt_tid when the private state thread actually interrupts the inferior for it, so a stop we did not cause is no longer attributed to the requestor.
A thread specific request is dropped if the process stopped on its own since it was made. Whatever the requestor wanted to interrupt is over at that point, and interrupting the next resume instead only produces a stop nobody can explain. The timeout plan arms a new timer on every resume, so dropping a request costs at most one more timeout period.
assisted-by: claude
>From 77f60f4f9c6307bbdd53cbc6ab3e08fa3c0e93c0 Mon Sep 17 00:00:00 2001
From: Raphael Isemann <rise at apple.com>
Date: Thu, 17 Sep 2026 09:43:03 +0100
Subject: [PATCH] [lldb] Give thread specific async interrupt requests a
lifetime
ThreadPlanSingleThreadTimeout asks Process to interrupt the inferior
when single thread stepping doesn't make progress within the timeout.
That request was an immediately visible state Process::m_interrupt_tid
change and a queued eBroadcastBitInterrupt event that was never
invalidated, so if the inferior stopped on its own between the timer
firing and the private state thread acting on the event, two things
went wrong:
1. The natural stop (a trace or breakpoint stop) was rewritten into
eStopReasonInterrupt by ProcessGDBRemote::HandleThreadAsyncInterrupt(),
because m_interrupt_tid was already set. The timeout plan then claimed
a stop it had not caused, and the real stop reason was lost.
2. The still queued interrupt event was processed after the next resume
and halted the process a second time. By then the timeout plan had been
popped and re-pushed in state WaitTimeout, so no plan explained the
stop and it was reported to the user, aborting the step in the middle
of a source line.
On a heavily loaded machine, where processing one internal stop can
take longer than the timeout, this makes TestSingleThreadStepTimeout.py
fail with e.g. "Stepped to line 63 instead of expected 65".
This patch gives the request a lifetime to fix this issue.
SendAsyncInterrupt() now only records the requesting thread and the
current stop ID in m_pending_interrupt. The request becomes visible as
m_interrupt_tid when the private state thread actually interrupts the
inferior for it, so a stop we did not cause is no longer attributed to
the requestor.
A thread specific request is dropped if the process stopped on its own
since it was made. Whatever the requestor wanted to interrupt is over
at that point, and interrupting the next resume instead only produces a
stop nobody can explain. The timeout plan arms a new timer on every
resume, so dropping a request costs at most one more timeout period.
assisted-by: claude
---
lldb/include/lldb/Target/Process.h | 19 ++++++++++++++++-
lldb/source/Target/Process.cpp | 34 ++++++++++++++++++++++++++----
2 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index 38c4c427ecb09..5c66194e3edf9 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -53,6 +53,7 @@
#include "lldb/Utility/Broadcaster.h"
#include "lldb/Utility/Event.h"
#include "lldb/Utility/Listener.h"
+#include "lldb/Utility/Locked.h"
#include "lldb/Utility/NameMatches.h"
#include "lldb/Utility/Policy.h"
#include "lldb/Utility/ProcessAddress.h"
@@ -1304,7 +1305,10 @@ class Process : public std::enable_shared_from_this<Process>,
/// Send an async interrupt request.
///
/// If \a thread is specified the async interrupt stop will be attributed to
- /// the specified thread.
+ /// the specified thread. Such a thread specific request is only honored for
+ /// the execution that is in flight when it is made: if the process stops on
+ /// its own before we get around to interrupting it, the request is dropped
+ /// instead of interrupting a later resume.
///
/// \param[in] thread
/// The thread the async interrupt will be attributed to.
@@ -3593,6 +3597,19 @@ void PruneThreadPlans();
/// can be LLDB_INVALID_THREAD_ID to indicate
/// user level async interrupt.
+ /// An async interrupt requested through SendAsyncInterrupt() that the
+ /// private state thread hasn't acted on yet: the tid of the requesting
+ /// thread (std::nullopt for a user level interrupt), together with the stop
+ /// ID the process had at the time of the request. The request only becomes
+ /// visible as m_interrupt_tid once we actually interrupt the inferior for
+ /// it, and a thread specific request is dropped if the process stopped on
+ /// its own in the meantime. See Process::RunPrivateStateThread().
+ struct PendingInterrupt {
+ std::optional<lldb::tid_t> tid;
+ uint32_t stop_id = 0;
+ };
+ Guarded<PendingInterrupt, std::mutex> m_pending_interrupt;
+
/// This is set at the beginning of Process::Finalize() to stop functions
/// from looking up or creating things during or after a finalize call.
std::atomic<bool> m_finalizing;
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index dd91d15cc8ca9..114cf77133dc5 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -10,6 +10,7 @@
#include <memory>
#include <mutex>
#include <optional>
+#include <utility>
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/ScopedPrinter.h"
@@ -4286,10 +4287,14 @@ void Process::ControlPrivateStateThread(uint32_t signal) {
}
void Process::SendAsyncInterrupt(Thread *thread) {
+ // Don't make the request visible as m_interrupt_tid yet: until the private
+ // state thread actually interrupts the inferior for us, the request can
+ // still turn out to be obsolete, and a stop that we didn't cause must not be
+ // reported as our async interrupt.
+ std::optional<lldb::tid_t> tid;
if (thread != nullptr)
- m_interrupt_tid = thread->GetProtocolID();
- else
- m_interrupt_tid = LLDB_INVALID_THREAD_ID;
+ tid = thread->GetProtocolID();
+ *m_pending_interrupt.Lock() = {tid, GetStopID()};
if (PrivateStateThreadIsRunning())
m_private_state_broadcaster.BroadcastEvent(Process::eBroadcastBitInterrupt,
nullptr);
@@ -4460,7 +4465,25 @@ Process::RunPrivateStateThread(PrivateStateThread::Purpose purpose) {
continue;
} else if (event_sp->GetType() == eBroadcastBitInterrupt) {
- if (GetPublicState() == eStateAttaching) {
+ // Claim the request this event was sent for. A thread specific request
+ // (i.e. one coming from a thread plan rather than from the user) is only
+ // meaningful for the execution that was in flight when it was made: if
+ // the process has stopped since then, whatever the requestor wanted to
+ // interrupt is over. Interrupting the next resume instead would stop the
+ // process for a reason no thread plan can explain, which surfaces to the
+ // user as a spurious stop.
+ auto [interrupt_tid, stop_id] =
+ std::exchange(*m_pending_interrupt.Lock(), {});
+ const bool interrupt_is_stale = interrupt_tid && stop_id != GetStopID();
+
+ if (interrupt_is_stale) {
+ LLDB_LOG(log,
+ "Process::RunPrivateStateThread ignoring stale interrupt "
+ "requested for thread {0:x}, the process stopped on its own "
+ "since.",
+ *interrupt_tid);
+ } else if (GetPublicState() == eStateAttaching) {
+ m_interrupt_tid = interrupt_tid.value_or(LLDB_INVALID_THREAD_ID);
LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") woke up with an interrupt while attaching - "
@@ -4475,6 +4498,9 @@ Process::RunPrivateStateThread(PrivateStateThread::Purpose purpose) {
Status error = HaltPrivate();
BroadcastEvent(eBroadcastBitInterrupt, nullptr);
} else if (StateIsRunningState(m_last_broadcast_state)) {
+ // Only now that we are actually interrupting the inferior does the
+ // stop we are about to cause belong to the requesting thread.
+ m_interrupt_tid = interrupt_tid.value_or(LLDB_INVALID_THREAD_ID);
LLDB_LOGF(log,
"Process::%s (arg = %p, pid = %" PRIu64
") woke up with an interrupt - Halting.",
More information about the lldb-commits
mailing list