mirror of
https://github.com/apache/nuttx.git
synced 2026-05-11 07:48:59 +08:00
291199f833
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>