Lightpanda: the headless browser designed for AI and automation
http: fix possible double-free on submit error
Karl Seguinb13736539415
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex f01195b84..8e9469dbd 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -1094,7 +1094,7 @@ pub fn makeRequest(self: *Frame, req: HttpClient.Request) !void {errdefer transfer.deinit();try self.headersForRequest(transfer);}- return transfer.submit();+ transfer.submit() catch {};}// Two-phase variant; see HttpClient.newRequest for the ownership contract.diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zigindex dc673a34a..f491c471b 100644--- a/src/browser/ScriptManager.zig+++ b/src/browser/ScriptManager.zig@@ -671,3 +671,23 @@ test "ScriptManager: async script whose submit fails synchronously releases its\\document.head.appendChild(s);, null);}++test "ScriptManager: preload whose submit fails synchronously releases its arena once" {+ const page = try testing.pageTest("mcp_nav.html", .{});+ defer page.close();++ const frame = page.frame().?;+ const sm = &frame._script_manager;+ const client = sm.base.client;+ client.test_fail_submit = error.TestSubmitFailure;+ defer client.test_fail_submit = null;++ // PreloadedScript.errorCallback logs the fetch error.+ testing.expectLog(&.{.http});++ const url = "http://127.0.0.1:9582/fails-at-submit.js";+ // A fetch was started (and failed), so the hint's error event fires.+ try testing.expectEqual(true, try sm.preloadScript(null, url));+ // errorCallback consumed the entry; nothing dangles in the map.+ try testing.expectEqual(false, sm.preloaded_scripts.contains(url));+}diff --git a/src/browser/ScriptManagerBase.zig b/src/browser/ScriptManagerBase.zigindex 524313e5a..37de84f2c 100644--- a/src/browser/ScriptManagerBase.zig+++ b/src/browser/ScriptManagerBase.zig@@ -1120,6 +1120,58 @@ test "ScriptManagerBase: shutdownCallback fails a .loading module" {try testing.expectError(error.Failed, sm.waitForImport(url));}+test "ScriptManagerBase: import whose submit fails synchronously releases its arena once" {+ const page = try testing.pageTest("mcp_nav.html", .{});+ defer page.close();+ const frame = page.frame().?;++ const sm = &frame._script_manager.base;+ const client = sm.client;+ client.test_fail_submit = error.TestSubmitFailure;+ defer client.test_fail_submit = null;++ // Script.errorCallback logs the fetch error.+ testing.expectLog(&.{.http});++ const url: [:0]const u8 = "http://127.0.0.1:9582/fails-at-submit.js";+ try sm.preloadImport(url, frame.url, .{});++ // The failure is delivered through the entry, same as an async one.+ try testing.expect(sm.async_scripts.first == null);+ try testing.expect(sm.imported_modules.getPtr(url).?.state == .err);+ try testing.expectError(error.Failed, sm.waitForImport(url));+}++test "ScriptManagerBase: dynamic import whose submit fails synchronously rejects once" {+ const page = try testing.pageTest("mcp_nav.html", .{});+ defer page.close();+ const frame = page.frame().?;++ const sm = &frame._script_manager.base;+ const client = sm.client;+ client.test_fail_submit = error.TestSubmitFailure;+ defer client.test_fail_submit = null;++ // Script.errorCallback logs the fetch error.+ testing.expectLog(&.{.http});++ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ try ls.local.eval(+ \\globalThis.__dyn = 'pending';+ \\import('http://127.0.0.1:9582/fails-at-submit.js').then(+ \\ () => { globalThis.__dyn = 'resolved'; },+ \\ (e) => { globalThis.__dyn = String(e); },+ \\);+ , frame.url); // the resource name is the import's base url+ ls.local.runMicrotasks();++ try testing.expect(sm.async_scripts.first == null);+ try testing.expectEqual(true, (try ls.local.exec("globalThis.__dyn === 'TestSubmitFailure'", null)).toBool());+}+test "ScriptManagerBase: waitForImport stops when teardown is pending" {const page = try testing.pageTest("mcp_nav.html", .{});defer page.close();diff --git a/src/browser/webapi/WorkerGlobalScope.zig b/src/browser/webapi/WorkerGlobalScope.zigindex 5ec3f8fbb..6326cda6c 100644--- a/src/browser/webapi/WorkerGlobalScope.zig+++ b/src/browser/webapi/WorkerGlobalScope.zig@@ -270,7 +270,7 @@ pub fn makeRequest(self: *WorkerGlobalScope, req: HttpClient.Request) !void {errdefer transfer.deinit();try self.headersForRequest(transfer);}- return transfer.submit();+ transfer.submit() catch {};}// Two-phase variant; see HttpClient.newRequest for the ownership contract.