Skip to content

nosuspend await not safety-checked? #8666

@euclidianAce

Description

@euclidianAce

This may be my own misunderstanding of how Zig's coroutines/async frames work, but the behavior of this snippet seems to contradict what nosuspend should do (at least in debug builds).

const std = @import("std");

pub fn main() !void {
    nosuspend asyncMain();
}

fn asyncMain() void {
    var f = async thing();
    // resume f;
    var x = nosuspend await f;
    std.log.info("x: {}", .{x});
}

fn thing() u32 {
    std.log.info("A, suspending", .{});
    suspend;
    std.log.info("B, returning", .{});
    return 16;
}
$ zig run example.zig
info: A, suspending
info: x: 0

By my understanding is that the await to f should suspend since the frame hasn't returned yet, but nosuspend should catch this?

And upon further inspection, the same happens if var x = nosuspend await f is replaced by

    // ...
    var x: u32 = 1;
    nosuspend {
        x = await f;
    }
    // ...

Specifically, x is zeroed out

$ zig version
0.8.0-dev.1479+aa1c78056

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions