zig icon indicating copy to clipboard operation
zig copied to clipboard

Overriding default log function in evented io can sometimes produce a program with random unrelated errors

Open bpeake13 opened this issue 3 years ago • 0 comments

Zig Version

0.9.1 and greater

Steps to Reproduce

Run this program

const std = @import("std");

pub const event_loop_mode = .single_threaded;
pub const io_mode = .evented;

pub fn defaultLog(
    comptime message_level: std.log.Level,
    comptime scope: @Type(.EnumLiteral),
    comptime format: []const u8,
    args: anytype,
) void {
    std.log.defaultLog(message_level, scope, format, args);
    
    const level_txt = comptime message_level.asText();
    const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
    _ = prefix2;
    _ = level_txt;

    var buf: [1024]u8 = undefined;
    _ = std.fmt.bufPrint(&buf, format, args) catch @panic("FAILED");
}

// Override the default logging function for our game.
pub const log = defaultLog;

var heap = std.heap.GeneralPurposeAllocator(.{}){};

pub fn main() anyerror!void {
    var allocator = std.heap.GeneralPurposeAllocator(.{}){};
    const mem = try allocator.allocator().alloc(u8, 127);
    allocator.allocator().free(mem);
}

Expected Behavior

The program should run fine to exit

Actual Behavior

The program crashes saying "resumed an async function which already returned", and stating that the point of error is

 fn freeImpl(ptr: *anyopaque, buf: []u8, buf_align: u29, ret_addr: usize) void {

The program works fine if io_mode is changed to blocking (and a custom event loop provided). It also works fine if the custom log function is removed, or if more specifically the bufPrint inside the log function is removed. It also works if the bufPrint takes no arguments. The stack does not seem to indicate that the log function is actually called, and breakpoints inside the log function do not get fired.

bpeake13 avatar Jul 03 '22 00:07 bpeake13