Skip to content

Commit 95ac92b

Browse files
server: fix cancel
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
1 parent 760c082 commit 95ac92b

2 files changed

Lines changed: 10 additions & 33 deletions

File tree

src/handler.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub const Stream = struct {
4545
}
4646

4747
fn closeCDP(self: *const Stream) void {
48+
const close_msg: []const u8 = .{ 5, 0 } ++ "close";
49+
self.recv(close_msg) catch |err| {
50+
log.err("stream close error: {any}", .{err});
51+
};
4852
std.posix.close(self.socket);
4953
}
5054

src/server.zig

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ pub const Ctx = struct {
120120
std.debug.assert(completion == self.conn_completion);
121121

122122
const size = result catch |err| {
123-
if (err == error.Canceled) {
124-
log.debug("read canceled", .{});
123+
if (self.isClosed() and err == error.FileDescriptorInvalid) {
124+
log.debug("read has been canceled", .{});
125125
return;
126126
}
127127
log.err("read error: {any}", .{err});
@@ -202,7 +202,7 @@ pub const Ctx = struct {
202202
if (now.since(self.last_active.?) > self.timeout) {
203203
// close current connection
204204
log.debug("conn timeout, closing...", .{});
205-
self.cancelAndClose();
205+
self.close();
206206
return;
207207
}
208208

@@ -216,19 +216,6 @@ pub const Ctx = struct {
216216
);
217217
}
218218

219-
fn cancelCbk(self: *Ctx, completion: *Completion, result: CancelError!void) void {
220-
std.debug.assert(completion == self.accept_completion);
221-
222-
_ = result catch |err| {
223-
log.err("cancel error: {any}", .{err});
224-
self.err = err;
225-
return;
226-
};
227-
log.debug("cancel done", .{});
228-
229-
self.close();
230-
}
231-
232219
// shortcuts
233220
// ---------
234221

@@ -265,7 +252,7 @@ pub const Ctx = struct {
265252
if (std.mem.eql(u8, cmd, "close")) {
266253
// close connection
267254
log.info("close cmd, closing conn...", .{});
268-
self.cancelAndClose();
255+
self.close();
269256
return error.Closed;
270257
}
271258

@@ -301,26 +288,12 @@ pub const Ctx = struct {
301288
}
302289
}
303290

304-
fn cancelAndClose(self: *Ctx) void {
305-
if (isLinux) { // cancel is only available on Linux
306-
self.loop.io.cancel(
307-
*Ctx,
308-
self,
309-
Ctx.cancelCbk,
310-
self.accept_completion,
311-
self.conn_completion,
312-
);
313-
} else {
314-
self.close();
315-
}
316-
}
317-
318291
fn close(self: *Ctx) void {
319-
std.posix.close(self.conn_socket);
320292

321293
// conn is closed
322-
log.debug("connection closed", .{});
323294
self.last_active = null;
295+
std.posix.close(self.conn_socket);
296+
log.debug("connection closed", .{});
324297

325298
// restart a new browser session in case of re-connect
326299
if (!self.sessionNew) {

0 commit comments

Comments
 (0)