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

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


The following commit(s) were added to refs/heads/master by this push:
     new 83967601aa fs/close: move inode to last to void race condition
83967601aa is described below

commit 83967601aa0b8a492d9a312dd3e5cb88858df168
Author: dongjiuzhu1 <dongjiuz...@xiaomi.com>
AuthorDate: Fri Apr 25 11:07:05 2025 +0800

    fs/close: move inode to last to void race condition
    
    race condition:
    
    A Thread:                                    B Thread:
    close
    file_close
    filep->inode = NULL
                             context switch
                           ------------------->  open to alloc same fd
                                                 read from IO
    filep->f_tag = 0       <-------------------
    filep->f_san = 0       ------------------->  ioctl(fd, ...)
                                                 fdcheck trigger assert
    
    Signed-off-by: dongjiuzhu1 <dongjiuz...@xiaomi.com>
---
 fs/vfs/fs_close.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/vfs/fs_close.c b/fs/vfs/fs_close.c
index 7b460e68ed..6d2f060707 100644
--- a/fs/vfs/fs_close.c
+++ b/fs/vfs/fs_close.c
@@ -162,10 +162,6 @@ int file_close(FAR struct file *filep)
   ret = file_close_without_clear(filep);
   if (ret >= 0 && filep->f_inode)
     {
-      /* Reset the user file struct instance so that it cannot be reused. */
-
-      filep->f_inode = NULL;
-
 #ifdef CONFIG_FDCHECK
       filep->f_tag_fdcheck = 0;
 #endif
@@ -173,6 +169,10 @@ int file_close(FAR struct file *filep)
 #ifdef CONFIG_FDSAN
       filep->f_tag_fdsan = 0;
 #endif
+
+      /* Reset the user file struct instance so that it cannot be reused. */
+
+      filep->f_inode = NULL;
     }
 
   return ret;

Reply via email to