Skip to content

Lightpanda: the headless browser designed for AI and automation

centralize teardown protection (covers every case this way)

Karl Seguin2a977351d7f0
diff --git a/src/browser/Browser.zig b/src/browser/Browser.zig
index bf5fcd0d5..79cad999b 100644
--- a/src/browser/Browser.zig
+++ b/src/browser/Browser.zig
@@ -203,13 +203,6 @@ pub fn newSession(self: *Browser, notification: *Notification) !*Session {
}
pub fn closeSession(self: *Browser) void {
- const env = &self.env;
-
- // Teardown GC must not arm a new termination.
- const was_tearing_down = env.tearing_down;
- env.tearing_down = true;
- defer env.tearing_down = was_tearing_down;
-
if (self.session) |*session| {
session.deinit();
self.session = null;
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 8c312289a..dd0516aa2 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -180,7 +180,14 @@ pub fn deinit(self: *Session) void {
self.cookie_jar.deinit();
- self.browser.env.memoryPressureNotification(.critical);
+ {
+ // Every context is disposed; this GC must not arm a termination.
+ const env = &self.browser.env;
+ const was_tearing_down = env.tearing_down;
+ env.tearing_down = true;
+ defer env.tearing_down = was_tearing_down;
+ env.memoryPressureNotification(.critical);
+ }
self.storage_shed.deinit(self.browser.app.allocator);
self.idb.deinit();
@@ -243,12 +250,6 @@ pub fn processDestroyQueues(self: *Session) void {
{
const queue = self._page_destruction_queue.items;
if (queue.len > 0) {
- // Context disposal GC must not arm a new termination.
- const env = &self.browser.env;
- const was_tearing_down = env.tearing_down;
- env.tearing_down = true;
- defer env.tearing_down = was_tearing_down;
-
for (queue) |page| {
page.deinit();
self.browser.page_pool.destroy(page);
diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zig
index 9475804e6..340537b54 100644
--- a/src/browser/js/Context.zig
+++ b/src/browser/js/Context.zig
@@ -206,6 +206,12 @@ pub fn deinit(self: *Context) void {
const env = self.env;
defer self.arena.release();
+ // Disposal GCs below can trip the near-heap-limit callback. There's no JS
+ // left in this context to stop, so it must not arm a termination.
+ const was_tearing_down = env.tearing_down;
+ env.tearing_down = true;
+ defer env.tearing_down = was_tearing_down;
+
// Unlink any IndexedDB gate participants first: the session-scoped engine
// must never wake a waiter into this scheduler once it's torn down.
self.page.session.idb.detachContext(self);
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index 1bf52dfb1..fae6096f2 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -104,7 +104,7 @@ terminate_mutex: std.Io.Mutex = .init,
// thread making sure terminate hasn't been canceled.
terminate_requested: std.atomic.Value(bool) = .init(false),
-// Set while V8 contexts are being destroyed.
+// Set while a V8 context (or the isolate) is being disposed.
tearing_down: bool = false,
heap_limit_protected: bool = false,