Manage disconnection (BrokenPipe) error while sending a message.

This commit is contained in:
Philippe Pittoli 2023-05-15 09:12:32 +02:00
parent 527049ed3d
commit bc4c54f624
3 changed files with 16 additions and 5 deletions

View File

@ -91,7 +91,9 @@ export fn ipc_read(ctx: *Context, index: usize, buffer: [*]u8, buflen: *usize) c
/// Wait for an event. /// Wait for an event.
/// Buffer length will be changed to the size of the received message. /// Buffer length will be changed to the size of the received message.
export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, buffer: [*]u8, buflen: *usize) callconv(.C) i32 { export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, buffer: [*]u8, buflen: *usize) callconv(.C) i32 {
var event = ctx.wait_event() catch return -1; var event = ctx.wait_event() catch |err| switch(err) {
else => { log.warn("error while waiting for an event: {}\n", .{err}); return -1; },
};
t.* = @enumToInt(event.t); t.* = @enumToInt(event.t);
index.* = event.index; index.* = event.index;
originfd.* = event.origin; originfd.* = event.origin;

View File

@ -478,6 +478,7 @@ pub const Context = struct {
} }
var m = self.tx.swapRemove(index); var m = self.tx.swapRemove(index);
defer m.deinit();
// SWITCHED = write message for its switch buddy (callbacks) // SWITCHED = write message for its switch buddy (callbacks)
if (self.connections.items[i].t == .SWITCHED) { if (self.connections.items[i].t == .SWITCHED) {
@ -500,8 +501,18 @@ pub const Context = struct {
return current_event; return current_event;
} else { } else {
// otherwise = write message for the msg.fd // otherwise = write message for the msg.fd
try self.write(m); self.write(m) catch |err| switch(err) {
m.deinit(); error.BrokenPipe => {
log.warn("cannot send message, dest probably closed the connection ({})", .{err});
try self.close(i);
return Event.init(Event.Type.ERROR, i, fd.fd, null);
},
else => {
log.warn("unmanaged error while sending a message ({})", .{err});
try self.close(i);
return Event.init(Event.Type.ERROR, i, fd.fd, null);
},
};
return Event.init(Event.Type.MESSAGE_TX, i, fd.fd, null); return Event.init(Event.Type.MESSAGE_TX, i, fd.fd, null);
} }
} }

View File

@ -152,9 +152,7 @@ pub const SwitchDB = struct {
return Event.init(Event.Type.SWITCH_RX, index, fd, message); return Event.init(Event.Type.SWITCH_RX, index, fd, message);
} }
/// Message is free'd in any case.
pub fn handle_event_write(self: *Self, index: usize, message: Message) Event { pub fn handle_event_write(self: *Self, index: usize, message: Message) Event {
defer message.deinit();
var fd = message.fd; var fd = message.fd;
self.write(message) catch |err| switch (err) { self.write(message) catch |err| switch (err) {
error.closeFD => { error.closeFD => {