Merge pull request #1872 from lightpanda-io/wp/mrdimidium/fix-cdp-close

Gracefull close ws socket
This commit is contained in:
Karl Seguin
2026-03-17 19:58:48 +08:00
committed by GitHub
3 changed files with 13 additions and 2 deletions

View File

@@ -242,7 +242,10 @@ pub const Client = struct {
fn stop(self: *Client) void {
switch (self.mode) {
.http => {},
.cdp => |*cdp| cdp.browser.env.terminate(),
.cdp => |*cdp| {
cdp.browser.env.terminate();
self.ws.sendClose();
},
}
self.ws.shutdown();
}

View File

@@ -308,10 +308,13 @@ pub fn run(self: *Runtime) void {
const socket = posix.accept(listener.socket, null, null, posix.SOCK.NONBLOCK) catch |err| {
switch (err) {
error.SocketNotListening, error.ConnectionAborted => {
error.SocketNotListening => {
self.pollfds[1] = .{ .fd = -1, .events = 0, .revents = 0 };
self.listener = null;
},
error.ConnectionAborted => {
lp.log.warn(.app, "accept connection aborted", .{});
},
error.WouldBlock => {},
else => {
lp.log.err(.app, "accept", .{ .err = err });

View File

@@ -308,6 +308,7 @@ pub fn Reader(comptime EXPECT_MASK: bool) type {
pub const WsConnection = struct {
// CLOSE, 2 length, code
const CLOSE_NORMAL = [_]u8{ 136, 2, 3, 232 }; // code: 1000
const CLOSE_GOING_AWAY = [_]u8{ 136, 2, 3, 233 }; // code: 1001
const CLOSE_TOO_BIG = [_]u8{ 136, 2, 3, 241 }; // 1009
const CLOSE_PROTOCOL_ERROR = [_]u8{ 136, 2, 3, 234 }; //code: 1002
// "private-use" close codes must be from 4000-49999
@@ -583,6 +584,10 @@ pub const WsConnection = struct {
return address;
}
pub fn sendClose(self: *WsConnection) void {
self.send(&CLOSE_GOING_AWAY) catch {};
}
pub fn shutdown(self: *WsConnection) void {
posix.shutdown(self.socket, .recv) catch {};
}