Repository navigation
arch/sim: Deliver a signal after the interrupt, not in it - #21737
Merged
Merged
Conversation
up_schedule_sigaction() ran the signal handler at once when the target was the running task, also when an interrupt handler called it. A timer that signals a running task (for example alarm() while the task spins) then ran the handler of the task inside sim_doirq(), in interrupt context. A handler that calls exit() exits the task from the interrupt handler and breaks the scheduler state: the next blocking call of the shell fails the assertion !is_idle_task(rtcb) in nxsem_wait_slow(). The LTP tests pthread_spin_lock 1-1 and 3-2 do this. Other architectures change the return context of the interrupt, so that the task runs the handler after the interrupt. Do the same in the simulator: in an interrupt handler, up_schedule_sigaction() keeps sigdeliver, and sim_doirq() calls sim_sigdeliver() when the interrupted task resumes, as up_switch_context() already does for a task that it switches back in. A signal that another task sends to a task that an interrupt preempted now also runs when that task resumes, not at its next context switch. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
requested review from
GUIDINGLI,
jerpelea and
xiaoxiang781216
as code owners
November 12, 2026 00:39
|
xiaoxiang781216
approved these changes
Nov 12, 2026
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
This was referenced Nov 12, 2026
Merged
Merged
acassis
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
depends-on: /pull/21729
Summary
On the simulator, a signal that an interrupt handler sends to the running task runs the signal handler of the task inside the interrupt handler.
up_schedule_sigaction()callssigdeliver()at once when the target is the running task, also when an interrupt handler calls it (for example the watchdog ofalarm()). The handler then runs insim_doirq(), in interrupt context.A handler that calls
exit()exits the task from the interrupt handler and breaks the scheduler state. The next blocking call of the shell then failsDEBUGASSERT(!is_idle_task(rtcb))innxsem_wait_slow()(semaphore/sem_wait.c:257,task: sh). The LTP testpthread_spin_lock/3-2does this: it locks a spin lock that is not initialized and leaves withexit()from itsSIGALRMhandler. Since #21693 (spin locks with atomics), the lock of the test spins until the alarm, sosim/citestnow fails this case.pthread_spin_lock/1-1fails in the same way, with the old spin locks too (the CI skips it).Other architectures change the return context of the interrupt, so that the task runs the handler after the interrupt. The simulator now does the same:
up_schedule_sigaction()runs the handler at once only out of an interrupt handler. In an interrupt handler, it keepssigdeliver.sim_doirq()callssim_sigdeliver()when the interrupted task resumes, asup_switch_context()already does for a task that it switches back in. A signal that another task sends to a task that an interrupt preempted now also runs when that task resumes, not at its next context switch.Impact
The simulator only (
arch/sim), all host platforms. Signal handlers no longer run in interrupt context. Builds withoutCONFIG_ENABLE_ALL_SIGNALSdo not change. Other architectures, the kernel and libc do not change. No ABI change. No documentation change.Testing
Linux x86_64 host, CI image
ghcr.io/apache/nuttx/apache-nuttx-ci-linux,sim:citestwith make, LTP cases run from nsh (ltp_interfaces_pthread_spin_*):pthread_spin_lock_3_2Test PASSED, thenAssertion failed : at file: semaphore/sem_wait.c:257 task: shpthread_spin_lock_1_1Test PASSED, then the same assertionpthread_spin_lock_1_2,pthread_spin_trylock_1_1destroy_1_1,destroy_3_1,init_1_1,init_4_1,lock_3_1,trylock_4_1,unlock_1_1,unlock_1_2unlock_3_1(expects that a thread can unlock a lock it does not hold; the CI skips it)ostestThe two TIMEOUT cases on master do not fail themselves: the simulator had stopped at the assertion of the case before. With #21693 reverted on master,
pthread_spin_lock_3_2passes (the old lock takes any lock word other than 1 as free, so the test exits before the alarm), andpthread_spin_lock_1_1fails with the same assertion.CI suite (ntfc 0.0.3, nuttx-ntfc-testing release-0.0.2, as the
Linux (sim-01)job),sim:citest:ltp_interfaces_pthread_spin_lock_3_2: "Device crashed"), 298 skippedsim:smp(4 CPUs) withCONFIG_PTHREAD_SPINLOCKS, built without-Werror(ostestthere has a-Wundeferror on master:CONFIG_ARCH_INTERRUPTSTACK):ostestpthread_spin_lock/3-2ashello, lock word set to0xa5Assertion failed : at file: sched/sched_smp.c:229on CPU2On SMP, the alarm stops at an older defect before it gets to the simulator: the timer interrupt on CPU2 queues the signal for the thread that runs on CPU1, and
nxsig_queue_action()then callsnxsched_smp_call_single(), which must not run in an interrupt handler (DEBUGASSERT(!up_interrupt_context())). That code is common to all architectures and this PR does not change it.-Werrorbuild ofsim:nshwithCONFIG_ENABLE_PARTIAL_SIGNALS(nosim_sigdeliver()): builds.tools/checkpatch.sh -c -u -m -gpasses.