Lightpanda: the headless browser designed for AI and automation
SingleFlight should own the key
Muki Kiboigof7e64643702d
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zigindex 36b9d98e7..f201469d6 100644--- a/src/network/HttpClient.zig+++ b/src/network/HttpClient.zig@@ -3602,7 +3602,7 @@ test "HttpClient: aborting a robots-parked transfer unlinks it from the gate" {const robots_url = "http://example.com/robots.txt";- var waiting: std.ArrayList(*Transfer) = .empty;+ var transfers: [2]*Transfer = undefined;for (0..2) |i| {const arena = try pool.acquire(.small, "test");const transfer = try arena.create(Transfer);@@ -3625,14 +3625,14 @@ test "HttpClient: aborting a robots-parked transfer unlinks it from the gate" {.start_time = 0,};try client.transfers.putNoClobber(testing.allocator, transfer.id, transfer);- try waiting.append(testing.allocator, transfer);- transfer.park(.robots);+ transfers[i] = transfer;}- try client.robots.single_flight.pending.putNoClobber(testing.allocator, robots_url, waiting);+ _ = try client.robots.single_flight.enter(robots_url, transfers[0], .robots);+ _ = try client.robots.single_flight.enter(robots_url, transfers[1], .robots);- const t1 = client.robots.single_flight.pending.get(robots_url).?.items[0];- const t2 = client.robots.single_flight.pending.get(robots_url).?.items[1];+ const t1 = transfers[0];+ const t2 = transfers[1];t1.abort(error.Abort);try testing.expectEqual(1, client.robots.single_flight.pending.get(robots_url).?.items.len);diff --git a/src/network/RobotsGate.zig b/src/network/RobotsGate.zigindex a1d723b04..bfffa742e 100644--- a/src/network/RobotsGate.zig+++ b/src/network/RobotsGate.zig@@ -80,40 +80,31 @@ pub fn remove(self: *RobotsGate, transfer: *Transfer) void {fn fetchThenResume(self: *RobotsGate, robots_url: [:0]const u8, transfer: *Transfer) !void {const client = transfer.client;- // The context, the response buffer and the pending-map key live on- // their own pooled arena, NOT on transfer.arena — any waiter (this one- // included) can be aborted while the fetch is still in flight, and the- // fetch's callbacks must survive that. The arena is released by- // whichever terminal callback fires (done / error / shutdown).+ const result = try self.single_flight.enter(robots_url, transfer, .robots);+ if (result == .queued) return;+ errdefer {+ self.single_flight.discard(robots_url);+ transfer.unpark();+ }+const arena = try client.arena_pool.acquire(.small, "RobotsGate.RobotsContext");errdefer arena.release();- const owned_url = try arena.dupeZ(u8, robots_url);- const result = try self.single_flight.enter(owned_url, transfer, .robots);- errdefer self.single_flight.discard(owned_url);- if (result == .queued) {- // Someone else already owns an in-flight fetch for this key- // (under its own, earlier-allocated owned_url). This arena was- // never handed to a fetch, so release it ourselves.- arena.release();- return;- }-const robots_ctx = try arena.create(RobotsContext);robots_ctx.* = .{.gate = self,.buffer = .empty,.arena = arena,.arena_pool = client.arena_pool,- .robots_url = owned_url,+ .robots_url = robots_url,};- log.debug(.browser, "fetching robots.txt", .{ .robots_url = owned_url });+ log.debug(.browser, "fetching robots.txt", .{ .robots_url = robots_url });// Only the parent's frame/loader ids (CDP correlation) and notification// carry over — no cookies, credentials, headers, or timeout.const fetch_transfer = try client.newRequest(.{- .url = owned_url,+ .url = robots_url,.method = .GET,.internal = true,.resource_type = .fetch,@@ -122,7 +113,7 @@ fn fetchThenResume(self: *RobotsGate, robots_url: [:0]const u8, transfer: *Trans.loader_id = transfer.req.loader_id,.notification = transfer.req.notification,.cookie_jar = null,- .cookie_origin = owned_url,+ .cookie_origin = robots_url,.ctx = robots_ctx,.header_callback = RobotsContext.headerCallback,.data_callback = RobotsContext.dataCallback,diff --git a/src/network/SingleFlight.zig b/src/network/SingleFlight.zigindex bf742f0de..d8e256ed5 100644--- a/src/network/SingleFlight.zig+++ b/src/network/SingleFlight.zig@@ -32,6 +32,7 @@ pub fn init(allocator: Allocator) SingleFlight {pub fn deinit(self: *SingleFlight) void {var it = self.pending.iterator();while (it.next()) |entry| {+ self.allocator.free(entry.key_ptr.*);entry.value_ptr.deinit(self.allocator);}self.pending.deinit(self.allocator);@@ -40,25 +41,27 @@ pub fn deinit(self: *SingleFlight) void {pub const EnterResult = enum { initial, queued };pub fn enter(self: *SingleFlight, key: []const u8, transfer: *Transfer, reason: Transfer.ParkedBy) !EnterResult {- const gop = try self.pending.getOrPut(self.allocator, key);- var waiting = gop.value_ptr;-- if (gop.found_existing) {+ if (self.pending.getPtr(key)) |waiting| {try waiting.append(self.allocator, transfer);transfer.park(reason);return .queued;}- waiting.* = .empty;- try waiting.append(self.allocator, transfer);+ const owned_key = try self.allocator.dupe(u8, key);+ errdefer self.allocator.free(owned_key);++ var waiting: std.ArrayList(*Transfer) = .empty;errdefer waiting.deinit(self.allocator);- transfer.park(reason);+ try waiting.append(self.allocator, transfer);+ try self.pending.put(self.allocator, owned_key, waiting);+ transfer.park(reason);return .initial;}pub fn abort(self: *SingleFlight, key: []const u8) void {var entry = self.pending.fetchRemove(key) orelse return;+ self.allocator.free(entry.key);entry.value.deinit(self.allocator);}@@ -76,11 +79,13 @@ pub fn remove(self: *SingleFlight, transfer: *Transfer) void {pub fn take(self: *SingleFlight, key: []const u8) ?std.ArrayList(*Transfer) {const entry = self.pending.fetchRemove(key) orelse return null;+ self.allocator.free(entry.key);return entry.value;}pub fn discard(self: *SingleFlight, key: []const u8) void {var entry = self.pending.fetchRemove(key) orelse return;+ self.allocator.free(entry.key);entry.value.deinit(self.allocator);}