std.Io.Threaded: rework cancellation #30033

Merged
andrewrk merged 14 commits from cancellation into master 2025-12-02 17:58:32 +01:00
Owner

Now, before a syscall is entered, beginSyscall is called, which may return error.Canceled. After syscall returns, whether error or success, endSyscall is called. If the syscall returns EINTR then checkCancel is called before looping. requestCancel only sends a signal to potentially interrupt a blocking syscall if the request arrived while the I/O implementation is making a syscall.

A new option is added, robust_cancel which toggles the retry behavior. I did this because retrying is not always needed for every application, and I observed it to be relatively slow.

Musl libc has a neat undocumented extension, PTHREAD_CANCEL_MASKED, which causes the next syscall to return ECANCELED while cleverly avoiding race conditions with a special syscall mechanism. It's wonderful, it's beautiful, it should be immediately standardized by POSIX. However, it does not allow resetting the "canceled" boolean, which means it cannot be used with worker thread pooling. That could be addressed by calling pthread_create/pthread_destroy for each async task, which may not be such a bad idea since it would reduce contention on the global mutex.

Addresses but does not close #30049.

Now, before a syscall is entered, `beginSyscall` is called, which may return `error.Canceled`. After syscall returns, whether error or success, `endSyscall` is called. If the syscall returns `EINTR` then `checkCancel` is called before looping. `requestCancel` only sends a signal to potentially interrupt a blocking syscall if the request arrived while the I/O implementation is making a syscall. A new option is added, `robust_cancel` which toggles the retry behavior. I did this because retrying is not always needed for every application, and I observed it to be relatively slow. Musl libc has a neat undocumented extension, `PTHREAD_CANCEL_MASKED`, which causes the next syscall to return `ECANCELED` while cleverly avoiding race conditions with a special syscall mechanism. It's wonderful, it's beautiful, it should be immediately standardized by POSIX. However, it does not allow resetting the "canceled" boolean, which means it cannot be used with worker thread pooling. That could be addressed by calling `pthread_create`/`pthread_destroy` for each async task, which may not be such a bad idea since it would reduce contention on the global mutex. Addresses but does not close #30049.
andrewrk force-pushed cancellation from f0c7d11281
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to 9ff0ac924c
Some checks failed
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
2025-11-29 01:02:28 +01:00
Compare
andrewrk force-pushed cancellation from 9ff0ac924c
Some checks failed
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
to cd9d84927b
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 35m15s
ci / x86_64-windows-debug (pull_request) Successful in 36m4s
ci / x86_64-freebsd-debug (pull_request) Successful in 45m45s
ci / aarch64-macos-release (pull_request) Successful in 48m18s
ci / aarch64-macos-debug (pull_request) Successful in 56m58s
ci / x86_64-windows-release (pull_request) Successful in 35m40s
ci / aarch64-linux-release (pull_request) Successful in 1h22m19s
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
2025-11-29 01:11:34 +01:00
Compare
Author
Owner

I tuned the requestCancel function with this program:

const std = @import("std");
const Io = std.Io;

pub fn main() !void {
    var debug_allocator = std.heap.DebugAllocator(.{}).init;
    defer _ = debug_allocator.deinit();
    const gpa = debug_allocator.allocator();

    var threaded: std.Io.Threaded = .init(gpa);
    defer threaded.deinit();
    const io = threaded.io();

    var group: std.Io.Group = .init;
    defer group.cancel(io);

    for (0..1_000_000) |i| {
        group.async(io, doWork, .{io});

        if (i % 7 == 0) {
            group.cancel(io);
            group = .init;
        }
    }
}

fn doWork(io: Io) void {
    io.sleep(.fromSeconds(300), .awake) catch {};
}

This program completes in about 50 seconds on my computer, indicating that all the doWork tasks were successfully canceled (otherwise it would have taken at least 5 minutes).

On a heavily loaded Linux 6.17.5, I observed a maximum of 20 attempts not acknowledged before the timeout (including exponential backoff) was sufficient, despite the heavy load.

The time wasted here sleeping is mitigated by the fact that, later on, the system will likely wait for the canceled task, causing it to indefinitely yield until the canceled task finishes, and the task must acknowledge the cancel before it proceeds to that point.

I tuned the `requestCancel` function with this program: ```zig const std = @import("std"); const Io = std.Io; pub fn main() !void { var debug_allocator = std.heap.DebugAllocator(.{}).init; defer _ = debug_allocator.deinit(); const gpa = debug_allocator.allocator(); var threaded: std.Io.Threaded = .init(gpa); defer threaded.deinit(); const io = threaded.io(); var group: std.Io.Group = .init; defer group.cancel(io); for (0..1_000_000) |i| { group.async(io, doWork, .{io}); if (i % 7 == 0) { group.cancel(io); group = .init; } } } fn doWork(io: Io) void { io.sleep(.fromSeconds(300), .awake) catch {}; } ``` This program completes in about 50 seconds on my computer, indicating that all the `doWork` tasks were successfully canceled (otherwise it would have taken at least 5 minutes). On a heavily loaded Linux 6.17.5, I observed a maximum of 20 attempts not acknowledged before the timeout (including exponential backoff) was sufficient, despite the heavy load. The time wasted here sleeping is mitigated by the fact that, later on, the system will likely wait for the canceled task, causing it to indefinitely yield until the canceled task finishes, and the task must acknowledge the cancel before it proceeds to that point.
andrewrk changed title from std.Io.Threaded: fix the cancellation race to std.Io.Threaded: rework cancellation 2025-11-29 18:00:03 +01:00
andrewrk force-pushed cancellation from 93b45fe15c
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to fed3f14367
Some checks failed
ci / x86_64-freebsd-release (pull_request) Failing after 40m55s
ci / x86_64-windows-debug (pull_request) Failing after 38m14s
ci / x86_64-freebsd-debug (pull_request) Failing after 1h2m9s
ci / x86_64-windows-release (pull_request) Failing after 32m24s
ci / aarch64-macos-release (pull_request) Failing after 1h5m22s
ci / aarch64-linux-release (pull_request) Failing after 1h15m48s
ci / aarch64-macos-debug (pull_request) Failing after 1h39m43s
ci / aarch64-linux-debug (pull_request) Failing after 1h44m19s
ci / x86_64-linux-debug (pull_request) Failing after 1h38m18s
ci / x86_64-linux-release (pull_request) Failing after 3h27m23s
ci / loongarch64-linux-debug (pull_request) Failing after 2h20m55s
ci / loongarch64-linux-release (pull_request) Failing after 1h41m10s
ci / s390x-linux-debug (pull_request) Successful in 2h40m35s
ci / s390x-linux-release (pull_request) Successful in 2h4m31s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 7h25m46s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
2025-11-29 19:31:44 +01:00
Compare
Author
Owner
mq test-link
   mq link_test_cases
      mq test-elf
         mq test-elf-emit-relocatable-aarch64-linux-musl-Debug-llvm-no-lld
            mq run exe test
               mq compile exe test Debug aarch64-linux-musl 1 errors
error: fatal linker error: unhandled relocation type R_AARCH64_CONDBR19 at offset 0x4
    note: in /home/ci/.cache/act/7689b16053d0eae8/hostexecutor/zig-global-cache/o/87b4b66df12cc4efdc4db377e7724f61/libc.a(/home/ci/.cache/act/7689b16053d0eae8/hostexecutor/zig-global-cache/o/5dc7a0a32d2131431b7d5fec50c448cb/syscall_cp.o/):.text
    ```
``` mq test-link mq link_test_cases mq test-elf mq test-elf-emit-relocatable-aarch64-linux-musl-Debug-llvm-no-lld mq run exe test mq compile exe test Debug aarch64-linux-musl 1 errors error: fatal linker error: unhandled relocation type R_AARCH64_CONDBR19 at offset 0x4 note: in /home/ci/.cache/act/7689b16053d0eae8/hostexecutor/zig-global-cache/o/87b4b66df12cc4efdc4db377e7724f61/libc.a(/home/ci/.cache/act/7689b16053d0eae8/hostexecutor/zig-global-cache/o/5dc7a0a32d2131431b7d5fec50c448cb/syscall_cp.o/):.text ```
Contributor

@andrewrk sinon/zig@790013b460

Tested with a simple,

extern void target(void);

void test(int x)
{
    asm volatile(
        "cbnz %w0, target\n"
        :
        : "r"(x)
        : "memory"
    );
}
0000000001020000 <target>:
 1020000: d65f03c0     	ret

0000000001020004 <test>:
 1020004: d10043ff     	sub	sp, sp, #0x10
 1020008: b9000fe0     	str	w0, [sp, #0xc]
 102000c: b9400fe8     	ldr	w8, [sp, #0xc]
 1020010: 35ffff88     	cbnz	w8, 0x1020000 <target>
 1020014: 910043ff     	add	sp, sp, #0x10
 1020018: d65f03c0     	ret
@andrewrk https://codeberg.org/sinon/zig/commit/790013b46024f2f4d115dc6dc89ef77d29a8a563 Tested with a simple, ```c extern void target(void); void test(int x) { asm volatile( "cbnz %w0, target\n" : : "r"(x) : "memory" ); } ``` ``` 0000000001020000 <target>: 1020000: d65f03c0 ret 0000000001020004 <test>: 1020004: d10043ff sub sp, sp, #0x10 1020008: b9000fe0 str w0, [sp, #0xc] 102000c: b9400fe8 ldr w8, [sp, #0xc] 1020010: 35ffff88 cbnz w8, 0x1020000 <target> 1020014: 910043ff add sp, sp, #0x10 1020018: d65f03c0 ret ```
andrewrk force-pushed cancellation from fed3f14367
Some checks failed
ci / x86_64-freebsd-release (pull_request) Failing after 40m55s
ci / x86_64-windows-debug (pull_request) Failing after 38m14s
ci / x86_64-freebsd-debug (pull_request) Failing after 1h2m9s
ci / x86_64-windows-release (pull_request) Failing after 32m24s
ci / aarch64-macos-release (pull_request) Failing after 1h5m22s
ci / aarch64-linux-release (pull_request) Failing after 1h15m48s
ci / aarch64-macos-debug (pull_request) Failing after 1h39m43s
ci / aarch64-linux-debug (pull_request) Failing after 1h44m19s
ci / x86_64-linux-debug (pull_request) Failing after 1h38m18s
ci / x86_64-linux-release (pull_request) Failing after 3h27m23s
ci / loongarch64-linux-debug (pull_request) Failing after 2h20m55s
ci / loongarch64-linux-release (pull_request) Failing after 1h41m10s
ci / s390x-linux-debug (pull_request) Successful in 2h40m35s
ci / s390x-linux-release (pull_request) Successful in 2h4m31s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 7h25m46s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
to ad71d52d54
Some checks failed
ci / x86_64-freebsd-release (pull_request) Failing after 40m31s
ci / x86_64-windows-debug (pull_request) Successful in 40m38s
ci / x86_64-freebsd-debug (pull_request) Failing after 52m1s
ci / x86_64-windows-release (pull_request) Successful in 49m37s
ci / aarch64-macos-release (pull_request) Successful in 1h15m45s
ci / aarch64-linux-release (pull_request) Successful in 1h25m26s
ci / aarch64-macos-debug (pull_request) Successful in 1h34m12s
ci / x86_64-linux-debug (pull_request) Failing after 1h47m10s
ci / aarch64-linux-debug (pull_request) Successful in 2h0m20s
ci / x86_64-linux-release (pull_request) Failing after 3h42m18s
ci / s390x-linux-release (pull_request) Successful in 1h46m36s
ci / loongarch64-linux-release (pull_request) Successful in 2h7m46s
ci / loongarch64-linux-debug (pull_request) Successful in 2h59m43s
ci / s390x-linux-debug (pull_request) Successful in 3h19m43s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 5h21m28s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
2025-12-01 02:16:34 +01:00
Compare
andrewrk force-pushed cancellation from 653dd118ed
Some checks failed
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-freebsd-release (pull_request) Successful in 35m5s
ci / x86_64-windows-debug (pull_request) Successful in 43m26s
ci / x86_64-windows-release (pull_request) Successful in 44m7s
ci / x86_64-freebsd-debug (pull_request) Successful in 45m31s
ci / aarch64-macos-release (pull_request) Successful in 53m3s
ci / aarch64-macos-debug (pull_request) Successful in 1h5m3s
ci / aarch64-linux-release (pull_request) Successful in 1h24m5s
ci / aarch64-linux-debug (pull_request) Successful in 2h3m38s
ci / loongarch64-linux-release (pull_request) Successful in 2h10m59s
ci / s390x-linux-release (pull_request) Successful in 2h27m48s
ci / loongarch64-linux-debug (pull_request) Successful in 3h1m38s
ci / s390x-linux-debug (pull_request) Successful in 3h16m2s
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
to bb3f56d5d5
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-windows-debug (pull_request) Successful in 32m22s
ci / x86_64-freebsd-release (pull_request) Successful in 33m16s
ci / x86_64-windows-release (pull_request) Successful in 37m33s
ci / x86_64-freebsd-debug (pull_request) Successful in 45m33s
ci / aarch64-macos-release (pull_request) Successful in 46m33s
ci / aarch64-macos-debug (pull_request) Successful in 55m32s
ci / aarch64-linux-release (pull_request) Successful in 1h24m51s
ci / x86_64-linux-debug (pull_request) Successful in 1h41m38s
ci / aarch64-linux-debug (pull_request) Successful in 1h55m38s
ci / loongarch64-linux-release (pull_request) Successful in 1h53m28s
ci / s390x-linux-release (pull_request) Successful in 1h42m43s
ci / loongarch64-linux-debug (pull_request) Successful in 2h53m50s
ci / s390x-linux-debug (pull_request) Successful in 3h46m13s
ci / x86_64-linux-release (pull_request) Successful in 4h30m5s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 5h58m17s
2025-12-02 04:18:10 +01:00
Compare
andrewrk merged commit 52ad126bb4 into master 2025-12-02 17:58:32 +01:00
andrewrk deleted branch cancellation 2025-12-02 17:58:34 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ziglang/zig!30033
No description provided.