What is the process for handling race conditions when an interrupt is generated right before the system goes to deep sleep mode under Linux?
For example, I have a GPIO rising edge interrupt enabled that my program waits on using epoll_wait() system call. My program determines that no pending action exists and goes to deep sleep by writing "mem" to /sys/power/state. While in deep sleep, a rising edge occurs on a pin that triggers an interrupt. This wakes up the system and my program resumes. At that point, epoll_wait() returns with an event indicating that an interrupt occurred. I set a flag in the program indicating that there is a pending action that prevents the system from going to sleep until the action is complete.
However, if an interrupt occurs at the beginning of writing "mem" to /sys/power/state, there appear to be no mechanism in place to prevent the system from going to sleep. An interrupt might be handled by the kernel driver right before "mem" is written, but after handling the interrupt the system will still go to sleep without giving user space program a chance to react to the interrupt.
How should this condition be handled?