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

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

commit efdb81be7d3dbfc21e70fdc4ff749dfee9bb638e
Author: raiden00pl <[email protected]>
AuthorDate: Thu Oct 22 17:47:04 2026 +0200

    wireless/ieee802154/i8sak: unwind the error exits
    
    A command that exited after requesting the daemon left its event
    receiver registered and the daemon running, and a failed instance setup
    exited with the instance on the active list.
    
    Assisted-by: Claude Code
    Signed-off-by: raiden00pl <[email protected]>
---
 wireless/ieee802154/i8sak/i8sak.h       |  1 +
 wireless/ieee802154/i8sak/i8sak_assoc.c |  3 +--
 wireless/ieee802154/i8sak/i8sak_main.c  | 28 +++++++++++++++++++++++++---
 wireless/ieee802154/i8sak/i8sak_poll.c  |  6 +++---
 wireless/ieee802154/i8sak/i8sak_scan.c  |  4 ++--
 wireless/ieee802154/i8sak/i8sak_tx.c    |  3 +--
 6 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/wireless/ieee802154/i8sak/i8sak.h 
b/wireless/ieee802154/i8sak/i8sak.h
index 3467e9a16..ca44f8faf 100644
--- a/wireless/ieee802154/i8sak/i8sak.h
+++ b/wireless/ieee802154/i8sak/i8sak.h
@@ -191,6 +191,7 @@ struct i8sak_s
 
 void i8sak_requestdaemon(FAR struct i8sak_s *i8sak);
 int i8sak_releasedaemon(FAR struct i8sak_s *i8sak);
+void i8sak_cmd_abort(FAR struct i8sak_s *i8sak, i8sak_eventcallback_t cb);
 
 uint8_t   i8sak_char2nibble (char ch);
 long      i8sak_str2long    (FAR const char *str);
diff --git a/wireless/ieee802154/i8sak/i8sak_assoc.c 
b/wireless/ieee802154/i8sak/i8sak_assoc.c
index ef59b4066..07617a809 100644
--- a/wireless/ieee802154/i8sak/i8sak_assoc.c
+++ b/wireless/ieee802154/i8sak/i8sak_assoc.c
@@ -261,8 +261,7 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
       if (ret != OK)
         {
           i8sak->assoc = false;
-          i8sak_cmd_error(i8sak);
-          break;
+          i8sak_cmd_abort(i8sak, assoc_eventcb);
         }
 
       /* Check if the association was successful */
diff --git a/wireless/ieee802154/i8sak/i8sak_main.c 
b/wireless/ieee802154/i8sak/i8sak_main.c
index 590932fa9..a9792bf45 100644
--- a/wireless/ieee802154/i8sak/i8sak_main.c
+++ b/wireless/ieee802154/i8sak/i8sak_main.c
@@ -239,6 +239,25 @@ void i8sak_requestdaemon(FAR struct i8sak_s *i8sak)
   sem_post(&i8sak->daemonlock);
 }
 
+/****************************************************************************
+ * Name: i8sak_cmd_abort
+ *
+ * Description:
+ *   Remove the receiver of a pending request, release the daemon and exit.
+ *
+ ****************************************************************************/
+
+void i8sak_cmd_abort(FAR struct i8sak_s *i8sak, i8sak_eventcallback_t cb)
+{
+  if (cb != NULL)
+    {
+      i8sak_eventlistener_removereceiver(i8sak, cb);
+    }
+
+  i8sak_releasedaemon(i8sak);
+  i8sak_cmd_error(i8sak);
+}
+
 /****************************************************************************
  * Name: i8sak_releasedaemon
  *
@@ -581,6 +600,7 @@ bool i8sak_str2bool(FAR const char *str)
 
 static void i8sak_switch_instance(FAR char *ifname)
 {
+  FAR struct i8sak_s *prev = g_activei8sak;
   FAR struct i8sak_s *i8sak;
 
   /* Search list of i8sak instances for one associated with the provided
@@ -620,6 +640,9 @@ static void i8sak_switch_instance(FAR char *ifname)
 
       if (i8sak_setup(i8sak, ifname) < 0)
         {
+          sq_rem((FAR sq_entry_t *)i8sak, &g_i8sak_instances);
+          sq_addlast((FAR sq_entry_t *)i8sak, &g_i8sak_free);
+          g_activei8sak = prev;
           exit(EXIT_FAILURE);
         }
     }
@@ -712,7 +735,7 @@ static int i8sak_setup(FAR struct i8sak_s *i8sak, FAR const 
char *ifname)
         {
           fprintf(stderr, "ERROR: cannot open %s, errno=%d\n",
                   i8sak->ifname, errno);
-          i8sak_cmd_error(i8sak);
+          return ERROR;
         }
     }
 #ifdef CONFIG_NET_6LOWPAN
@@ -722,13 +745,12 @@ static int i8sak_setup(FAR struct i8sak_s *i8sak, FAR 
const char *ifname)
       if (fd < 0)
         {
           fprintf(stderr, "ERROR: failed to open socket, errno=%d\n", errno);
-          i8sak_cmd_error(i8sak);
+          return ERROR;
         }
     }
 #endif
   else
     {
-      close(fd);
       return ERROR;
     }
 
diff --git a/wireless/ieee802154/i8sak/i8sak_poll.c 
b/wireless/ieee802154/i8sak/i8sak_poll.c
index 4161db7b8..ebddad125 100644
--- a/wireless/ieee802154/i8sak/i8sak_poll.c
+++ b/wireless/ieee802154/i8sak/i8sak_poll.c
@@ -132,7 +132,7 @@ void i8sak_poll_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
         {
           fprintf(stderr, "ERROR: cannot open %s, errno=%d\n",
                   i8sak->ifname, errno);
-          i8sak_cmd_error(i8sak);
+          i8sak_cmd_abort(i8sak, poll_eventcb);
         }
 
       ieee802154_poll_req(fd, &pollreq);
@@ -144,7 +144,7 @@ void i8sak_poll_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
       if (fd < 0)
         {
           fprintf(stderr, "ERROR: failed to open socket, errno=%d\n", errno);
-          i8sak_cmd_error(i8sak);
+          i8sak_cmd_abort(i8sak, poll_eventcb);
         }
 
       sixlowpan_poll_req(fd, i8sak->ifname, &pollreq);
@@ -161,7 +161,7 @@ void i8sak_poll_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
   if (ret != OK)
     {
       printf("i8sak: poll cancelled\n");
-      i8sak_cmd_error(i8sak);
+      i8sak_cmd_abort(i8sak, poll_eventcb);
     }
 
   i8sak_releasedaemon(i8sak);
diff --git a/wireless/ieee802154/i8sak/i8sak_scan.c 
b/wireless/ieee802154/i8sak/i8sak_scan.c
index 8064c3120..1d546d5c6 100644
--- a/wireless/ieee802154/i8sak/i8sak_scan.c
+++ b/wireless/ieee802154/i8sak/i8sak_scan.c
@@ -167,7 +167,7 @@ void i8sak_scan_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
         {
           fprintf(stderr, "ERROR: cannot open %s, errno=%d\n",
                   i8sak->ifname, errno);
-          i8sak_cmd_error(i8sak);
+          i8sak_cmd_abort(i8sak, scan_eventcb);
         }
 
       ieee802154_scan_req(fd, &scan);
@@ -179,7 +179,7 @@ void i8sak_scan_cmd(FAR struct i8sak_s *i8sak, int argc, 
FAR char *argv[])
       if (fd < 0)
         {
           fprintf(stderr, "ERROR: failed to open socket, errno=%d\n", errno);
-          i8sak_cmd_error(i8sak);
+          i8sak_cmd_abort(i8sak, scan_eventcb);
         }
 
       sixlowpan_scan_req(fd, i8sak->ifname, &scan);
diff --git a/wireless/ieee802154/i8sak/i8sak_tx.c 
b/wireless/ieee802154/i8sak/i8sak_tx.c
index 4f43559f9..787b36d20 100644
--- a/wireless/ieee802154/i8sak/i8sak_tx.c
+++ b/wireless/ieee802154/i8sak/i8sak_tx.c
@@ -260,8 +260,7 @@ void i8sak_tx_cmd(FAR struct i8sak_s *i8sak, int argc, FAR 
char *argv[])
   if (ret < 0)
     {
       fprintf(stderr, "ERROR: Failed to transmit packet: errno=%d\n", errno);
-      close(fd);
-      i8sak_cmd_error(i8sak);
+      i8sak_cmd_abort(i8sak, tx_eventcb);
     }
 
   close(fd);

Reply via email to