Error compiling concurrency sample code

Hello all,

i am studying Zig and stumble on the following error:

sombriks@erebus 13 $ zig run 3-threads.zig 
/usr/lib/zig/std/Io/Uring.zig:2732:32: error: expected type 'error{AccessDenied,BadPathName,Canceled,FileNotFound,NameTooLong,NetworkNotFound,NoDevice,NotDir,PermissionDenied,ProcessFdQuotaExceeded,SymLinkLoop,SystemFdQuotaExceeded,SystemResources,Unexpected}!Io.Dir', found 'error{AccessDenied,BadPathName,Canceled,FileNotFound,NameTooLong,NoDevice,NotDir,PermissionDenied,ProcessFdQuotaExceeded,ReadOnlyFileSystem,SymLinkLoop,SystemFdQuotaExceeded,SystemResources,Unexpected}'
            else => |e| return e,
                               ^
/usr/lib/zig/std/Io/Uring.zig:2732:32: note: 'error.ReadOnlyFileSystem' not a member of destination error set
/usr/lib/zig/std/Io/Uring.zig:2708:16: note: function return type declared here
) Dir.OpenError!Dir {
  ~~~~~~~~~~~~~^~~~
referenced by:
    io: /usr/lib/zig/std/Io/Uring.zig:696:14
    main: 3-threads.zig:42:20
    5 reference(s) hidden; use '-freference-trace=7' to see all references
/usr/lib/zig/std/Io/Uring.zig:3157:28: error: expected type 'error{AccessDenied,AntivirusInterference,BadPathName,Canceled,DeviceBusy,FileBusy,FileNotFound,FileSystem,FileTooBig,InputOutput,IsDir,NameTooLong,NetworkNotFound,NoDevice,NoSpaceLeft,NotDir,OperationUnsupported,PathAlreadyExists,PermissionDenied,PipeBusy,ProcessFdQuotaExceeded,SymLinkLoop,SystemFdQuotaExceeded,SystemResources,Unexpected,UnrecognizedVolume}!usize', found 'error{AccessDenied,BadPathName,Canceled,DeviceBusy,FileBusy,FileNotFound,FileTooBig,IsDir,NameTooLong,NoDevice,NoSpaceLeft,NotDir,PathAlreadyExists,PermissionDenied,ProcessFdQuotaExceeded,ReadOnlyFileSystem,SymLinkLoop,SystemFdQuotaExceeded,SystemResources,Unexpected}'
        else => |e| return e,
                           ^
/usr/lib/zig/std/Io/Uring.zig:3157:28: note: 'error.ReadOnlyFileSystem' not a member of destination error set
/usr/lib/zig/std/Io/Uring.zig:3143:24: note: function return type declared here
) Dir.RealPathFileError!usize {
  ~~~~~~~~~~~~~~~~~~~~~^~~~~~
sombriks@erebus 13 $ 

This is the sample code:

// 3-threads.zig

const std = @import("std");

fn work(io: *std.Io, id: usize, n: i64) !void {
    std.log.info("worker {} started", .{id});
    defer std.log.info("worker {} finished", .{id});
    try io.*.sleep(.fromSeconds(n), .awake);
}

fn scheduler(io: *std.Io) anyerror!void {
    const ReturnType = @typeInfo(@TypeOf(work)).@"fn".return_type.?;
    var futures: [9]std.Io.Future(ReturnType) = undefined;
    for(0..9) |i| {
        const id = i + 1;
        const seconds = @as(i64, @intCast(1 + i % 3));
        futures[i] = io.*.async(work, .{io, id, seconds});
    }

    for(&futures) |*future| {
        _ = try future.await(io.*);
    }
}

pub fn main(init: std.process.Init) !void {
    var single = std.Io.Threaded.init_single_threaded;
    defer single.deinit();
    var io: std.Io = single.io();
    std.log.info("single threaded io:", .{});
    try scheduler(&io);

    var pool = std.Io.Threaded.init(init.gpa,.{});
    defer pool.deinit();
    io = pool.io();
    std.log.info("thread pool io:", .{});
    try scheduler(&io);

    // we need out-pointer this one.
    var evented: std.Io.Evented = undefined;
    _ = try std.Io.Evented.init(&evented, init.gpa,.{});
    defer evented.deinit();
    io = evented.io();
    std.log.info("evented io:", .{});
    try scheduler(&io);
}

The two first parts, single threaded and thread pool works as expected, but the event based async is failing, not suere why.

Any guidance is welcome.

Zig version is 0.16.0

I ran your code with the master branch compiler, and it ran without errors, so it seems it’s now fixed.

1 Like

By the way, you don’t need to pass Io as a pointer, because it is a vtable interface.

You can use my project if you want real async I/O. Even though the std.Io.Uring in Zig master compiles, it’s still missing much of the functionality.

2 Likes