Skip to content

Compiler segfaults with async spaghetti #4061

@pmwhite

Description

@pmwhite

Sorry for the unhelpful title - I'm not really sure what the cause of this segfault is, but here is a small-ish piece of code that will produce it. Feel free to edit the title if you know what the more root issue is. If you have a guess, I can also try to make a smaller program that reproduces the error. If this works in the latest master, then feel free to close; the segfault is in 0.5.0. EDIT: Just tested with master. The program still segfaults

const std = @import("std");

var global: ?u32 = null;

fn getResult() ?u32 {
    if (global == null) {
        suspend {
            frames.append(@frame()) catch unreachable;
        }
    }
    return global;
}

fn return_result(a: ?u32) void {
    global = a;
    for (frames.toSlice()) |frame| {
        resume frame;
    }
}

fn handleEvent(x: bool) void {
    if (x) {
        var result = await (async getResult());
        std.debug.warn("result is {}\n", result);
    } else {
        return_result(10);
    }
}

var frames: std.ArrayList(anyframe) = undefined;
fn amain() void {
    const events = [_]bool{ true, true, true, false };
    for (events) |event| {
        _ = async handleEvent(event);
    }
}

pub fn main() void {
    frames = std.ArrayList(anyframe).init(std.heap.direct_allocator);

    _ = async amain();
}

I wrote this while trying to produce a smaller version of some other code that was acting unexpectedly. The flow of this code matches the structure of my application - the eventHandler may suspend until the user does some action that resumes it.

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