server: fix cancel

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-11-29 14:59:14 +01:00
parent 760c082757
commit 95ac92b343
2 changed files with 10 additions and 33 deletions

View File

@@ -45,6 +45,10 @@ pub const Stream = struct {
} }
fn closeCDP(self: *const Stream) void { fn closeCDP(self: *const Stream) void {
const close_msg: []const u8 = .{ 5, 0 } ++ "close";
self.recv(close_msg) catch |err| {
log.err("stream close error: {any}", .{err});
};
std.posix.close(self.socket); std.posix.close(self.socket);
} }

View File

@@ -120,8 +120,8 @@ pub const Ctx = struct {
std.debug.assert(completion == self.conn_completion); std.debug.assert(completion == self.conn_completion);
const size = result catch |err| { const size = result catch |err| {
if (err == error.Canceled) { if (self.isClosed() and err == error.FileDescriptorInvalid) {
log.debug("read canceled", .{}); log.debug("read has been canceled", .{});
return; return;
} }
log.err("read error: {any}", .{err}); log.err("read error: {any}", .{err});
@@ -202,7 +202,7 @@ pub const Ctx = struct {
if (now.since(self.last_active.?) > self.timeout) { if (now.since(self.last_active.?) > self.timeout) {
// close current connection // close current connection
log.debug("conn timeout, closing...", .{}); log.debug("conn timeout, closing...", .{});
self.cancelAndClose(); self.close();
return; return;
} }
@@ -216,19 +216,6 @@ pub const Ctx = struct {
); );
} }
fn cancelCbk(self: *Ctx, completion: *Completion, result: CancelError!void) void {
std.debug.assert(completion == self.accept_completion);
_ = result catch |err| {
log.err("cancel error: {any}", .{err});
self.err = err;
return;
};
log.debug("cancel done", .{});
self.close();
}
// shortcuts // shortcuts
// --------- // ---------
@@ -265,7 +252,7 @@ pub const Ctx = struct {
if (std.mem.eql(u8, cmd, "close")) { if (std.mem.eql(u8, cmd, "close")) {
// close connection // close connection
log.info("close cmd, closing conn...", .{}); log.info("close cmd, closing conn...", .{});
self.cancelAndClose(); self.close();
return error.Closed; return error.Closed;
} }
@@ -301,26 +288,12 @@ pub const Ctx = struct {
} }
} }
fn cancelAndClose(self: *Ctx) void {
if (isLinux) { // cancel is only available on Linux
self.loop.io.cancel(
*Ctx,
self,
Ctx.cancelCbk,
self.accept_completion,
self.conn_completion,
);
} else {
self.close();
}
}
fn close(self: *Ctx) void { fn close(self: *Ctx) void {
std.posix.close(self.conn_socket);
// conn is closed // conn is closed
log.debug("connection closed", .{});
self.last_active = null; self.last_active = null;
std.posix.close(self.conn_socket);
log.debug("connection closed", .{});
// restart a new browser session in case of re-connect // restart a new browser session in case of re-connect
if (!self.sessionNew) { if (!self.sessionNew) {