Skip to content

std.Io.Threaded cancellation race condition (signal received between checkCancel and syscall) #25751

@andrewrk

Description

@andrewrk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorenhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions