Skip to content

arch/sim: Deliver a signal after the interrupt, not in it - #21737

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:spin-lock-uninit-up
Nov 12, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:spin-lock-uninit-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

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() calls sigdeliver() at once when the target is the running task, also when an interrupt handler calls it (for example the watchdog of alarm()). The handler then runs in 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 then fails DEBUGASSERT(!is_idle_task(rtcb)) in nxsem_wait_slow() (semaphore/sem_wait.c:257, task: sh). The LTP test pthread_spin_lock/3-2 does this: it locks a spin lock that is not initialized and leaves with exit() from its SIGALRM handler. Since #21693 (spin locks with atomics), the lock of the test spins until the alarm, so sim/citest now fails this case. pthread_spin_lock/1-1 fails 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 keeps sigdeliver.
  • 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.

Impact

The simulator only (arch/sim), all host platforms. Signal handlers no longer run in interrupt context. Builds without CONFIG_ENABLE_ALL_SIGNALS do 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:citest with make, LTP cases run from nsh (ltp_interfaces_pthread_spin_*):

LTP case master this PR
pthread_spin_lock_3_2 handler prints Test PASSED, then Assertion failed : at file: semaphore/sem_wait.c:257 task: sh PASS, exit status 0
pthread_spin_lock_1_1 handler prints Test PASSED, then the same assertion PASS, exit status 0
pthread_spin_lock_1_2, pthread_spin_trylock_1_1 TIMEOUT PASS, exit status 0
destroy_1_1, destroy_3_1, init_1_1, init_4_1, lock_3_1, trylock_4_1, unlock_1_1, unlock_1_2 PASS PASS
unlock_3_1 (expects that a thread can unlock a lock it does not hold; the CI skips it) FAIL (EPERM) FAIL (EPERM)
ostest PASS, exit status 0 PASS, exit status 0

The 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_2 passes (the old lock takes any lock word other than 1 as free, so the test exits before the alarm), and pthread_spin_lock_1_1 fails 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:

master this PR
ntfc summary 1216 passed, 1 failed (ltp_interfaces_pthread_spin_lock_3_2: "Device crashed"), 298 skipped 1217 passed, 0 failed, 298 skipped

sim:smp (4 CPUs) with CONFIG_PTHREAD_SPINLOCKS, built without -Werror (ostest there has a -Wundef error on master: CONFIG_ARCH_INTERRUPTSTACK):

Test master this PR
ostest PASS, exit status 0 PASS, exit status 0
copy of pthread_spin_lock/3-2 as hello, lock word set to 0xa5 Assertion failed : at file: sched/sched_smp.c:229 on CPU2 same

On 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 calls nxsched_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.

-Werror build of sim:nsh with CONFIG_ENABLE_PARTIAL_SIGNALS (no sim_sigdeliver()): builds.

tools/checkpatch.sh -c -u -m -g passes.

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]>
@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +24 B (+0.0%, 67,035 B / 262,144 B, total: 26% used)
  • sram: .data +24 B (+0.9%, 2,828 B / 8,192 B, total: 35% used)

esp32-devkitc

  • ROM: .dram0.data +4 B, .flash.rodata +24 B (+0.0%, 130,340 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.data +4 B (+0.0%, 10,044 B / 180,736 B, total: 6% used)
  • drom0_0_seg: .flash.rodata +24 B (+0.2%, 13,860 B / 4,194,272 B, total: 0% used)

hifive1-revb

  • flash: .text +20 B (+0.0%, 86,572 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .data +4 B, .text +20 B (+0.0%, 69,176 B / 131,072 B, total: 53% used)
  • kseg1_datamem: .data +4 B, .sbss -8 B (-0.2%, 2,600 B / 32,256 B, total: 8% used)

qemu-armv8a

  • Code: .rodata +24 B (+0.0%, 364,516 B)
  • Data: .data.g_version +2 B (+0.0%, 77,919 B)

qemu-intel64

  • Data: .rodata +32 B (+0.0%, 126,841 B)

rx65n-rsk2mb

  • RAM: .data +4 B (+0.1%, 6,724 B / 262,144 B, total: 3% used)
  • ROM: .rodata +20 B (+0.0%, 92,192 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +32 B (+0.0%, 391,872 B)

stm32-nucleo-f103rb

  • flash: .text +24 B (+0.1%, 35,400 B / 131,072 B, total: 27% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43128833220

@acassis
acassis merged commit 91cc9f5 into apache:master Nov 12, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants