Extracted from #25592.
Example:
while (true) {
try t.checkCancel();
// <---------------- what if the signal arrives here?
switch (posix.errno(posix.system.mkdirat(dir.handle, sub_path_posix, mode))) {
.SUCCESS => return,
.INTR => continue,
If the signal arrives between checkCancel and the syscall, it will be missed, causing the syscall to be uninterruptible.
To fix this, make the atomic canceling thread id also have some more enum states:
- will check for cancellation later
- cancel request (by thread id)
- uninterruptible
- about to enter interruptible syscall
Then, requestCancel will do a compare exchange to request cancellation, handling previous value:
- will check for cancellation later -> cancellation has been acknowledged
- cancel request -> redundant cancellation request, nothing to do
- uninterruptible -> cannot cancel, nothing to do
- about to enter interruptible syscall -> send the IO signal to interrupt the syscall, then repeat the cancellation request (after a delay?)
Simultaneously, checkCancel will transition the state.
This ensures that signals are only sent when there is a blocking syscall involved, and the repeated attempts handle the signal being delivered during the race window.
Extracted from #25592.
Example:
If the signal arrives between
checkCanceland the syscall, it will be missed, causing the syscall to be uninterruptible.To fix this, make the atomic canceling thread id also have some more enum states:
Then,
requestCancelwill do a compare exchange to request cancellation, handling previous value:Simultaneously,
checkCancelwill transition the state.This ensures that signals are only sent when there is a blocking syscall involved, and the repeated attempts handle the signal being delivered during the race window.