I refactored this function that was using Child.init() before which was working with .init_single threaded:
pub fn dateChanger(
self: []const u8,
io: std.Io,
allocator: std.mem.Allocator,
stdout: *std.Io.Writer,
stderr: *std.Io.Writer,
file: [][]const u8,
baseDate: []u8,
increment_seconds: i64,
verbose: u8,
) !void {
for (file) |value| {
if (std.mem.eql(u8, self, value)) return;
// Compute new timestamp (convert to seconds, increment, convert back)
const newDate = try computeIncrementedDate(baseDate, increment_seconds);
cmd[2] = try std.fmt.allocPrint(allocator, "-DateTimeOriginal={s}", .{newDate});
defer allocator.free(cmd[2]);
cmd[3] = try std.fmt.allocPrint(allocator, "-CreateDate={s}", .{newDate});
defer allocator.free(cmd[3]);
cmd[4] = try std.fmt.allocPrint(allocator, "-ModifyDate={s}", .{newDate});
defer allocator.free(cmd[4]);
cmd[5] = value;
// exiftool command
const result = try std.process.run(allocator, io, .{
.argv = &cmd,
.stdout_limit = .limited(1024),
.stderr_limit = .limited(1024),
});
defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}
switch (verbose) {
't' => {
std.log.info("File: {s}", .{value});
if (result.term.exited == 0) {
//STDOUT
try stdout.writeAll(result.stdout);
try stdout.print("Exit: {d}\n", .{result.term.exited});
try stdout.flush();
} else {
//STDERR
try stderr.writeAll(result.stderr);
try stderr.flush();
std.log.err("Exit: {d}", .{result.term.exited});
}
},
'f' => {
if (result.term.exited == 0)
std.log.info("File: {s}", .{value})
else
std.log.err("File: {s}", .{value});
},
else => {},
}
}
}
I’m trying to understand where does allocation happens that bubbles OOM error but I’m having trouble with it. When I switched to Threaded.init() all worked:
// Io init
var io_init = std.Io.Threaded.init(
allocator,
.{ .environ = init.environ },
);
defer io_init.deinit();
const io = io_init.io();