fix: crash on shutdown from dangling logger references

This commit is contained in:
CJ van den Berg 2024-04-01 22:02:08 +02:00
parent 823d066a58
commit e2c565dfee
11 changed files with 38 additions and 16 deletions

View file

@ -59,6 +59,7 @@ const Process = struct {
fn deinit(self: *Process) void {
self.recv_buf.deinit();
self.logger.deinit();
self.a.free(self.cmd.buf);
self.close() catch {};
}

View file

@ -104,22 +104,24 @@ fn receive(self: *Self, from: tp.pid_ref, m: tp.message) tp.result {
}
pub const Logger = struct {
proc: tp.pid_ref,
proc: tp.pid,
tag: []const u8,
const Self_ = @This();
pub fn deinit(self: *const Logger) void {
self.proc.deinit();
}
pub fn write(self: Self_, value: anytype) void {
pub fn write(self: Logger, value: anytype) void {
self.proc.send(.{ "log", self.tag } ++ value) catch {};
}
pub fn print(self: Self_, comptime fmt: anytype, args: anytype) void {
pub fn print(self: Logger, comptime fmt: anytype, args: anytype) void {
var buf: [max_log_message]u8 = undefined;
const output = std.fmt.bufPrint(&buf, fmt, args) catch "MESSAGE TOO LARGE";
self.proc.send(.{ "log", self.tag, output }) catch {};
}
pub fn err(self: Self_, context: []const u8, e: anyerror) void {
pub fn err(self: Logger, context: []const u8, e: anyerror) void {
defer tp.reset_error();
var buf: [max_log_message]u8 = undefined;
var msg: []const u8 = "UNKNOWN";
@ -146,7 +148,19 @@ pub const Logger = struct {
};
pub fn logger(tag: []const u8) Logger {
return .{ .proc = tp.env.get().proc("log"), .tag = tag };
return .{ .proc = tp.env.get().proc("log").clone(), .tag = tag };
}
pub fn print(tag: []const u8, comptime fmt: anytype, args: anytype) void {
const l = logger(tag);
defer l.deinit();
return l.print(fmt, args);
}
pub fn err(tag: []const u8, context: []const u8, e: anyerror) void {
const l = logger(tag);
defer l.deinit();
return l.err(context, e);
}
pub fn subscribe() tp.result {

View file

@ -97,6 +97,7 @@ const Process = struct {
}
self.projects.deinit();
self.parent.deinit();
self.logger.deinit();
self.a.destroy(self);
}

View file

@ -100,6 +100,7 @@ const Process = struct {
fn deinit(self: *Process) void {
self.output.deinit();
self.logger.deinit();
self.a.free(self.query);
self.close() catch {};
}
@ -149,7 +150,7 @@ const Process = struct {
fn handle_output(self: *Process, bytes: []u8) !void {
try self.output.appendSlice(bytes);
// @import("log").logger(module_name).print("{s}", .{bytes}) catch {};
// self.logger.print("{s}", .{bytes}) catch {};
}
fn handle_terminated(self: *Process) !void {
@ -161,9 +162,9 @@ const Process = struct {
const msg: tp.message = .{ .buf = try cbor.fromJson(json, &msg_buf) };
try self.dispatch(msg);
// var buf: [tp.max_message_size]u8 = undefined;
// @import("log").logger(module_name).print("json: {s}", .{try msg.to_json(&buf)}) catch {};
// self.logger.print("json: {s}", .{try msg.to_json(&buf)}) catch {};
}
// @import("log").logger(module_name).print("done", .{}) catch {};
// self.logger.print("done", .{}) catch {};
try self.parent.send(.{ self.tag, "done" });
}

View file

@ -57,6 +57,7 @@ const Process = struct {
fn deinit(self: *Process) void {
self.parent.deinit();
self.logger.deinit();
self.a.destroy(self);
}

View file

@ -107,7 +107,7 @@ pub const List = struct {
pub fn add(self: *List, h: MessageFilter) !void {
(try self.list.addOne()).* = h;
// @import("log").logger("MessageFilter").print("add: {d} {s}", .{ self.list.items.len, self.list.items[self.list.items.len - 1].vtable.type_name });
// @import("log").print("MessageFilter", "add: {d} {s}", .{ self.list.items.len, self.list.items[self.list.items.len - 1].vtable.type_name });
}
pub fn remove(self: *List, h: MessageFilter) !void {

View file

@ -46,12 +46,12 @@ pub fn Closure(comptime T: type) type {
pub fn register(self: *Self) !void {
if (command_names.get(self.vtbl.name)) |id| {
self.vtbl.id = id;
reAddCommand(&self.vtbl) catch |e| return log.logger("cmd").err("reAddCommand", e);
// log.logger("cmd").print("reAddCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
reAddCommand(&self.vtbl) catch |e| return log.err("cmd", "reAddCommand", e);
// log.print("cmd", "reAddCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
} else {
self.vtbl.id = try addCommand(&self.vtbl);
command_names.put(self.vtbl.name, self.vtbl.id) catch |e| return log.logger("cmd").err("addCommand", e);
// log.logger("cmd").print("addCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
command_names.put(self.vtbl.name, self.vtbl.id) catch |e| return log.err("cmd", "addCommand", e);
// log.print("cmd", "addCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
}
}
@ -96,7 +96,7 @@ pub fn execute(id: ID, ctx: Context) tp.result {
const cmd = commands.items[id];
if (cmd) |p| {
// var buf: [tp.max_message_size]u8 = undefined;
// log.logger("cmd").print("execute({s}) {s}", .{ p.name, ctx.args.to_json(&buf) catch "" }) catch |e| return tp.exit_error(e);
// log.print("cmd", "execute({s}) {s}", .{ p.name, ctx.args.to_json(&buf) catch "" }) catch |e| return tp.exit_error(e);
return p.run(p, ctx);
} else {
return tp.exit_fmt("CommandNotAvailable: {d}", .{id});

View file

@ -297,6 +297,7 @@ pub const Editor = struct {
self.cursels.deinit();
self.matches.deinit();
self.handlers.deinit();
self.logger.deinit();
if (self.buffer) |p| p.deinit();
}

View file

@ -250,7 +250,7 @@ fn diff_update(self: *Self) !void {
}
fn diff_result(from: tp.pid_ref, edits: []diff.Edit) void {
diff_result_send(from, edits) catch |e| @import("log").logger(@typeName(Self)).err("diff", e);
diff_result_send(from, edits) catch |e| @import("log").err(@typeName(Self), "diff", e);
}
fn diff_result_send(from: tp.pid_ref, edits: []diff.Edit) !void {

View file

@ -58,6 +58,7 @@ pub fn deinit(self: *Self) void {
tui.current().message_filters.remove_ptr(self);
if (tui.current().mainview.dynamic_cast(mainview)) |mv|
mv.floating_views.remove(self.menu.menu_widget);
self.logger.deinit();
self.a.destroy(self);
}

View file

@ -192,6 +192,7 @@ fn deinit(self: *Self) void {
if (self.sigwinch_signal) |sig| sig.deinit();
self.frame_clock.deinit();
self.nc.stop();
self.logger.deinit();
self.a.destroy(self);
}
@ -718,6 +719,7 @@ const cmds = struct {
pub fn log_widgets(self: *Self, _: Ctx) tp.result {
const l = log.logger("z stack");
defer l.deinit();
var buf: [256]u8 = undefined;
var buf_parent: [256]u8 = undefined;
var z: i32 = 0;