Skip to content

Lightpanda: the headless browser designed for AI and automation

make socket ownership more explicit and future-proof

Karl Seguin2ace9d154804
diff --git a/src/Inbox.zig b/src/Inbox.zig
index 699e721c6..0320bd916 100644
--- a/src/Inbox.zig
+++ b/src/Inbox.zig
@@ -125,7 +125,7 @@ pub const Message = struct {
payload: Payload,
node: DoublyLinkedList.Node = .{},
- const Payload = union(enum) {
+ pub const Payload = union(enum) {
// A CDP text/binary frame, parsed on the Network thread. `raw`
// is the original JSON bytes (owned). `arena` holds any
// auxiliary allocations from parseFromSliceLeaky (typically
diff --git a/src/server/bidi/BiDi.zig b/src/server/bidi/BiDi.zig
index e75ef157f..94d97fa92 100644
--- a/src/server/bidi/BiDi.zig
+++ b/src/server/bidi/BiDi.zig
@@ -21,12 +21,15 @@ const lp = @import("lightpanda");
const App = @import("../../App.zig");
const Inbox = @import("../../Inbox.zig");
+
+const sys_net = @import("../../sys/net.zig");
const uuidv4 = @import("../../id.zig").uuidv4;
-const Browser = @import("../../browser/Browser.zig");
-const Session = @import("../../browser/Session.zig");
const Notification = @import("../../Notification.zig");
const NodeRegistry = @import("../../NodeRegistry.zig");
+const Browser = @import("../../browser/Browser.zig");
+const Session = @import("../../browser/Session.zig");
+
const Link = @import("../Link.zig");
const Server = @import("../Server.zig");
@@ -113,27 +116,36 @@ const InputMessage = struct {
pub fn init(self: *BiDi, app: *App, inbox: *Inbox, origin: Origin) !void {
const allocator = app.allocator;
- self.* = .{
- .app = app,
- .link = null,
- .inbox = inbox,
- .mode = switch (origin) {
- .socket => .bidi_only,
- .session => |session| .{ .http = session.worker },
- },
- .browser = undefined,
- .user_context = undefined,
- .notification = undefined,
- .session_id = switch (origin) {
- .socket => null,
- .session => |session| session.id,
- },
- .node_registry = .init(allocator),
- .handles = .{ .allocator = allocator },
- .message_arena = std.heap.ArenaAllocator.init(allocator),
- .session_arena = std.heap.ArenaAllocator.init(allocator),
- };
+ {
+ // this is documentation, and future-proofing, to show exactly where
+ // the socket's ownership is
+ errdefer if (origin == .socket) {
+ sys_net.close(origin.socket);
+ };
+
+ self.* = .{
+ .app = app,
+ .link = null,
+ .inbox = inbox,
+ .mode = switch (origin) {
+ .socket => .bidi_only,
+ .session => |session| .{ .http = session.worker },
+ },
+ .browser = undefined,
+ .user_context = undefined,
+ .notification = undefined,
+ .session_id = switch (origin) {
+ .socket => null,
+ .session => |session| session.id,
+ },
+ .node_registry = .init(allocator),
+ .handles = .{ .allocator = allocator },
+ .message_arena = std.heap.ArenaAllocator.init(allocator),
+ .session_arena = std.heap.ArenaAllocator.init(allocator),
+ };
+ }
+ // Link.create takes ownership of the socket
switch (origin) {
.socket => |socket| self.link = try Link.create(app, socket, .bidi, inbox),
.session => {},
diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zig
index 560e923b6..228fd7aff 100644
--- a/src/server/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -23,6 +23,7 @@ const App = @import("../../App.zig");
const Inbox = @import("../../Inbox.zig");
const Notification = @import("../../Notification.zig");
+const sys_net = @import("../../sys/net.zig");
const http = @import("../../network/http.zig");
const HttpClient = @import("../../network/HttpClient.zig");
@@ -97,20 +98,26 @@ streams: @import("domains/io.zig").Streams,
pub fn init(self: *CDP, app: *App, socket: posix.socket_t, inbox: *Inbox) !void {
const allocator = app.allocator;
+ {
+ // this is documentation, and future-proofing, to show exactly where
+ // the socket's ownership is
+ errdefer sys_net.close(socket);
- self.* = .{
- .app = app,
- .link = undefined,
- .browser = undefined,
- .allocator = allocator,
- .browser_context = null,
- .frame_arena = std.heap.ArenaAllocator.init(allocator),
- .message_arena = std.heap.ArenaAllocator.init(allocator),
- .notification_arena = std.heap.ArenaAllocator.init(allocator),
- .browser_context_arena = std.heap.ArenaAllocator.init(allocator),
- .streams = .{ .allocator = allocator },
- };
+ self.* = .{
+ .app = app,
+ .link = undefined,
+ .browser = undefined,
+ .allocator = allocator,
+ .browser_context = null,
+ .frame_arena = std.heap.ArenaAllocator.init(allocator),
+ .message_arena = std.heap.ArenaAllocator.init(allocator),
+ .notification_arena = std.heap.ArenaAllocator.init(allocator),
+ .browser_context_arena = std.heap.ArenaAllocator.init(allocator),
+ .streams = .{ .allocator = allocator },
+ };
+ }
+ // takes ownership of the socket
try self.link.init(app, socket, .cdp, inbox);
errdefer self.link.deinit();