Suppose an application is blocked at a cancellation point, for example read
, and a signal is received and a signal handler invoked. Glibc/NPTL implements cancellation points by enabling asynchronous cancellation for the duration of the syscall, so as far as I can tell, asynchronous cancellation will remain in effect for the entire duration of the signal handler. This would of course be horribly wrong, as there are plenty of functions that are not async-cancel-safe but which are required to be safe to call from signal handlers.
This leaves me with two questions:
- Am I wrong or is the glibc/NPTL behavior really this dangerously broken? If so, is such dangerous behavior conformant?
- What, according to POSIX, is supposed to happen if a signal handler is invoked while the process is executing a function which is a cancellation point?
Edit: I've almost convinced myself that any thread which is a potential target of pthread_cancel
must ensure that functions which are cancellation points can never be called from a signal handler in that thread's context:
On the one hand, any signal handler that can be invoked in a thread that might be cancelled and which uses any async-cancel-unsafe functions must disable cancellation before calling any function which is a cancellation point. This is because, from the perspective of the code interrupted by the signal, any such cancellation would be equivalent to asynchronous cancellation. On the other hand, a signal handler cannot disable cancellation, unless the code that will be running when the signal handler is invoked only uses async-signal-safe functions, because pthread_setcancelstate
is not async-signal-safe.