nuttx/fs
dongjiuzhu1 291199f833 fs/epoll: add TLS cleanup handler to release epoll fd reference on thread exit
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>
2025-12-31 08:49:04 -03:00
..
aio
binfs
cromfs
driver
event
fat
hostfs
inode
littlefs littlefs: fix statfs underflow during active writes 2025-12-29 14:23:04 +08:00
mmap
mnemofs
mount
mqueue fs/mqueue: fix inverted 'created' flag in file_mq_vopen 2025-12-31 02:27:37 +08:00
nfs
nxffs
partition
procfs
romfs
rpmsgfs
semaphore
shm
smartfs
socket
spiffs
tmpfs
unionfs
userfs
v9fs
vfs fs/epoll: add TLS cleanup handler to release epoll fd reference on thread exit 2025-12-31 08:49:04 -03:00
zipfs
CMakeLists.txt
fs_heap.c
fs_heap.h
fs_initialize.c
Kconfig
Makefile