This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6f98c6d38c978739ecb5c9253b0ec9b1044e6a9e
Author: yushuailong <[email protected]>
AuthorDate: Mon Sep 14 20:18:31 2026 +0800

    sched/irq: Make threaded IRQ detach safe without a task.
    
    Only call kthread_delete for a valid positive PID and always clear the IRQ
    thread slot afterward.  This makes detach on an unused IRQ, including a
    repeated detach, a safe no-op instead of deleting the calling task.
    
    Assisted-by: OpenAI Codex
    Signed-off-by: yushuailong <[email protected]>
---
 sched/irq/irq_attach_thread.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/sched/irq/irq_attach_thread.c b/sched/irq/irq_attach_thread.c
index a7bf55fdfd5..799c4556604 100644
--- a/sched/irq/irq_attach_thread.c
+++ b/sched/irq/irq_attach_thread.c
@@ -161,16 +161,19 @@ int irq_attach_thread(int irq, xcpt_t isr, xcpt_t 
isrthread, FAR void *arg,
     {
       ret = ndx;
     }
-  else if(isrthread == NULL)
+  else if (isrthread == NULL)
     {
       /* If the isrthread is NULL, then the ISR is being detached. */
 
       irq_detach(irq);
-      DEBUGASSERT(irq_thread_pid[ndx] != 0);
-      kthread_delete(irq_thread_pid[ndx]);
+      if (irq_thread_pid[ndx] > 0)
+        {
+          kthread_delete(irq_thread_pid[ndx]);
+        }
+
       irq_thread_pid[ndx] = 0;
     }
-  else if(irq_thread_pid[ndx] != 0)
+  else if (irq_thread_pid[ndx] != 0)
     {
       ret = -EINVAL;
     }

Reply via email to