mirror of
https://github.com/apache/nuttx.git
synced 2026-08-03 13:19:01 +00:00
When a thread is terminated via pthread_exit() while blocked in epoll_wait(), the file reference taken at the beginning of epoll_wait() is not properly released, leading to resource leaks. Problem scenario found during libuv test: 1. Echo server thread is blocked in epoll_wait() 2. Main task sends pthread_kill signal to the server thread 3. Signal handler calls pthread_exit() to terminate the thread 4. epoll_wait() is interrupted before reaching the file_put() call 5. The epoll fd reference count remains elevated 6. epoll_do_close() is never called, leaving fds in internal queues 7. File descriptors leak Solution: Register a TLS (Thread Local Storage) cleanup handler using tls_cleanup_push() at the beginning of epoll_wait() blocking section. This ensures that if the thread exits abnormally (via pthread_exit, pthread_cancel, etc.), the cleanup handler (epoll_cleanup) will be called automatically to release the file reference via file_put(). The cleanup handler is properly paired with tls_cleanup_pop() when epoll_wait() completes normally, ensuring the handler is only invoked on abnormal exit. This fix is applied to both epoll_wait() code paths (with and without extended mode) to ensure consistent behavior. Impact: - Prevents epoll fd reference count leaks on thread cancellation - Ensures proper cleanup even when epoll_wait() is interrupted by pthread_exit - Critical for multi-threaded applications using signals and thread termination - Works together with previous fix for teardown/oneshot list cleanup Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com> |
||
|---|---|---|
| .. | ||
| CMakeLists.txt | ||
| fs_chstat.c | ||
| fs_close.c | ||
| fs_dir.c | ||
| fs_dup.c | ||
| fs_dup2.c | ||
| fs_dup3.c | ||
| fs_epoll.c | ||
| fs_eventfd.c | ||
| fs_fchstat.c | ||
| fs_fcntl.c | ||
| fs_fstat.c | ||
| fs_fstatfs.c | ||
| fs_fsync.c | ||
| fs_inotify.c | ||
| fs_ioctl.c | ||
| fs_link.c | ||
| fs_lock.c | ||
| fs_lseek.c | ||
| fs_mkdir.c | ||
| fs_open.c | ||
| fs_poll.c | ||
| fs_pread.c | ||
| fs_pseudofile.c | ||
| fs_pwrite.c | ||
| fs_read.c | ||
| fs_readlink.c | ||
| fs_rename.c | ||
| fs_rmdir.c | ||
| fs_select.c | ||
| fs_sendfile.c | ||
| fs_signalfd.c | ||
| fs_stat.c | ||
| fs_statfs.c | ||
| fs_symlink.c | ||
| fs_syncfs.c | ||
| fs_timerfd.c | ||
| fs_truncate.c | ||
| fs_uio.c | ||
| fs_unlink.c | ||
| fs_write.c | ||
| Kconfig | ||
| Make.defs | ||
| vfs.h | ||