Copilot commented on code in PR #21686:
URL: https://github.com/apache/nuttx/pull/21686#discussion_r4535585302


##########
fs/vfs/fs_epoll.c:
##########
@@ -193,11 +193,8 @@ static int epoll_do_close(FAR struct file *filep)
       return ret;
     }
 
-  eph->crefs--;
-  nxmutex_unlock(&eph->lock);
-  if (eph->crefs <= 0)
+  if (--eph->crefs <= 0)
     {
-      nxmutex_destroy(&eph->lock);
       list_for_every_entry(&eph->setup, epn, epoll_node_t, node)
         {
           file_poll(epn->filep, &epn->pfd, false);

Review Comment:
   The teardown loop now runs while holding `eph->lock` and calls into external 
subsystems (`file_poll()`, and (in the omitted lines) likely `file_close()` / 
related cleanup). Holding `eph->lock` across these calls can introduce 
lock-order inversions or deadlocks if those paths synchronously trigger 
poll/notify callbacks that also attempt to take `eph->lock` (directly or 
indirectly). A safer pattern is: while holding `eph->lock`, detach/move the 
`setup`/`free` lists to local temporaries (and mark the epoll instance as 
closing), then unlock; afterwards perform `file_poll`/`file_close` and free the 
nodes without holding `eph->lock`, and finally destroy/free `eph`.



##########
fs/vfs/fs_epoll.c:
##########
@@ -220,8 +217,14 @@ static int epoll_do_close(FAR struct file *filep)
           fs_heap_free(epn);
         }
 
+      nxmutex_unlock(&eph->lock);
+      nxmutex_destroy(&eph->lock);
       fs_heap_free(eph);
     }

Review Comment:
   The teardown loop now runs while holding `eph->lock` and calls into external 
subsystems (`file_poll()`, and (in the omitted lines) likely `file_close()` / 
related cleanup). Holding `eph->lock` across these calls can introduce 
lock-order inversions or deadlocks if those paths synchronously trigger 
poll/notify callbacks that also attempt to take `eph->lock` (directly or 
indirectly). A safer pattern is: while holding `eph->lock`, detach/move the 
`setup`/`free` lists to local temporaries (and mark the epoll instance as 
closing), then unlock; afterwards perform `file_poll`/`file_close` and free the 
nodes without holding `eph->lock`, and finally destroy/free `eph`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to