Linux Signalfd Is Useless
15 hours ago
- #UNIX
- #Linux-API
- #signal-handling
- UNIX signals are problematic due to their ability to interrupt a program at any point, imposing strict restrictions on signal handlers.
- Signal handlers can't use many common functions like stdio or malloc because they might be holding locks when interrupted.
- The self-pipe trick is a common workaround to defer signal handling work until outside the handler.
- System calls interrupted by signals return EINTR, causing issues as few libraries handle restarting these calls properly.
- Linux's signalfd API was introduced to simplify signal handling by delivering signals via a file descriptor, but it doesn't change signal disposition mechanics.
- Masking signals for signalfd inheritance causes issues in child processes, which inherit the mask and may not reset it.
- Signal coalescing is another issue, where multiple instances of the same signal are reported as one, losing information.
- A proposed better solution involves using SA_NODEFER with a dedicated signal-handling thread to avoid EINTR issues, though signal coalescing can still occur.
- Signalfd could be improved by integrating it more deeply with signal disposition, ensuring signals don't need to be masked and preventing inheritance issues.