Std.process.spawn new 0.16 help

            cmd = std.fmt.allocPrint(allocChild, "./{s}  {s}", .{ module, value }) catch unreachable;
            arg3 = .{ "/bin/sh", "-c", cmd };
            Print("\r\n arg3 {s}  {s} {s} \n", .{ arg3[0], arg3[1], arg3[2] });
//  arg3   /bin/sh  -c ./Pecho  1778892611755 
// programme Pecho =  print 1778892611755
            // Nouveau code (0.16) avec le wrapper :
            // const childTerm = spawnAndWait(&arg3) catch unreachable;
            var vChild = try std.process.spawn(io, .{
                .argv = &arg3,
                .cwd = .{ .inherit = {} },
                .stdin = .ignore,
                .stdout = .ignore,
                .stderr = .ignore,
            });

            const childTerm = vChild.wait(io) catch unreachable;
            switch (childTerm) {
                .exited => |code| {
                    if (code != 0) {
                        Perror(ErrCallpgm.Module_error_internal_Occurs);
                        return ErrCallpgm.Module_Error;
                    }
                },
                .signal => |signal| {
                    std.debug.print("Processus tué par le signal: {d}\n", .{signal});
                    return ErrCallpgm.Module_Error;
                },
                .stopped => |signal| {
                    std.debug.print("Processus stoppé par le signal: {d}\n", .{signal});
                    return ErrCallpgm.Module_Error;
                },
                // .unknown => |signal| {
                //     std.debug.print("Processus repris par le signal: {d}\n", .{signal});
                //     // Gérer si nécessaire
                // },
                else => unreachable,
            }


error: OutOfMemory
/home/soleil/.zig/lib/std/mem/Allocator.zig:300:82: 0x10429a3 in allocBytesWithAlignment__anon_8785 (std.zig)
const byte_ptr = self.rawAlloc(byte_count, alignment, return_address) orelse return error.OutOfMemory;
^
/home/soleil/.zig/lib/std/mem/Allocator.zig:286:5: 0x10975ee in allocWithSizeAndAlignment__anon_11505 (std.zig)
return self.allocBytesWithAlignment(alignment, byte_count, return_address);
^
/home/soleil/.zig/lib/std/mem/Allocator.zig:274:55: 0x1166470 in allocAdvancedWithRetAddr (std.zig)
const ptr: []align(a.toByteUnits()) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
^
/home/soleil/.zig/lib/std/mem/Allocator.zig:222:21: 0x116656a in allocWithOptionsRetAddr__anon_25117 (std.zig)
const ptr = try self.allocAdvancedWithRetAddr(Elem, optional_alignment, n + 1, return_address);
^
/home/soleil/.zig/lib/std/mem/Allocator.zig:252:5: 0x116626c in allocSentinel__anon_24322 (std.zig)
return self.allocWithOptionsRetAddr(Elem, n, null, sentinel, @returnAddress());
^
/home/soleil/.zig/lib/std/Io/Threaded.zig:14926:22: 0x115398b in spawnPosix (std.zig)
const argv_buf = try arena.allocSentinel(?[
:0]const u8, options.argv.len, null);
^
/home/soleil/.zig/lib/std/Io/Threaded.zig:15098:21: 0x1151cb6 in processSpawnPosix (std.zig)
const spawned = try spawnPosix(t, options);
^
/home/soleil/.zig/lib/std/process.zig:443:5: 0x11f8ebd in spawn (std.zig)
return io.vtable.processSpawn(io.userdata, options);
^
/home/soleil/Zmmap/libtui/calling/callpgm.zig:101:26: 0x11d6bfd in callPgmPid (callpgm.zig)
var vChild = try std.process.spawn(io, .{

The program crashes at the std.process.spawn statement.

I’ve tried every possible option.
Everything works fine in 0.15, but not in 0.16.

I can’t get it to work. Can someone please help me?

my guess is the problem lies with creating/passing Io

What happens if .stdout = .inherit? It seems like .ignore might be causing an issue? The error.OutOfMemory is strange, so that is my idea.

The error is in allocating the args for the child process, has nothing to do with stdio.

It is odd to get OutOfMemory on what I assume is a normal personal/dev environment. That is why I assume the issue is in faulty state from initialisation, passing ptrs around (Io and Allocator are fat ptrs), or something similar.

It is also entirely possible the error and trace are wrong :3 it wouldn’t be the first time.

2 Likes

First of all, thank you for your responses.

var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();

            var vChild = try std.process.spawn(io, .{
                .argv = &arg3,
                .stdin = .ignore,
                .stdout = .ignore,
                .stderr = .ignore,
            });

I’ve tried several scenarios It’s always the same answer

pub const init_single_threaded: Threaded = init: {
    const env_block: process.Environ.Block = if (is_windows) .global else .empty;
    break :init .{
        .allocator = .failing,
        ...

That initializes the io with a failing allocator see std.Io.Threaded.init_single_threaded, so you can’t use it if you want to allocate anything, instead use std.Io.Threaded.init or juicy main like @skdishansachin suggested.

2 Likes

I’ll give it a try and let you know. Thanks

What kind of allocator should I use, since “var allocChild = std.heap.page_allocator;” isn’t accepted?

Should I use an arena?

I suggest you use something like this updated for recent versions:

So choose debug allocator in safe modes to detect memory leaks and std.heap.smp_allocator in unsafe release modes.

You need a thread safe allocator, debug allocator and smp_allocator are both thread safe.

1 Like

juicy main does an updated version of that logic, ofc that may have too much overhead from other things, but op could copy that logic from std.start.callMain

1 Like

idem error 0.16.0 and 0.17.dev