Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: disarm watchdog before protocol teardown

Scott Taylorecc9cffa1284
diff --git a/src/browser/Browser.zig b/src/browser/Browser.zig
index 6dd8d0d87..ed92e610c 100644
--- a/src/browser/Browser.zig
+++ b/src/browser/Browser.zig
@@ -142,14 +142,13 @@ pub fn init(self: *Browser, app: *App, opts: InitOpts) !void {
pub fn deinit(self: *Browser) void {
const allocator = self.allocator;
+ self.prepareForTeardown();
+
self.closeSession();
lp.metrics.js_heap_physical_bytes.add(-@as(i64, @intCast(self.last_reported_js_bytes)));
self.last_reported_js_bytes = 0;
- // After this returns, the watchdog thread holds no reference to our env
- // or http_client — required before either is torn down.
- self.app.watchdog.unregister(&self.watchdog_entry);
self.env.deinit();
// After env.deinit() the Isolate is gone, so no further weak finalizer can
// fire — only now is it safe to free the pool backing their parameters.
@@ -162,6 +161,12 @@ pub fn deinit(self: *Browser) void {
self.selector_cache.deinit();
}
+// Wait out a watchdog scan before clearing its termination request.
+pub fn prepareForTeardown(self: *Browser) void {
+ self.app.watchdog.unregister(&self.watchdog_entry);
+ self.env.cancelTerminate();
+}
+
// Set (or overwrite) the stored state for a permission. The name is duped into
// `allocator`; the state is a plain enum tag. Used by CDP
// Browser.grantPermissions / setPermission.
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 4aa575eb5..0d6bed931 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -695,13 +695,11 @@ const Worker = struct {
Worker.notifyLoopOfChange(server, .{ .ws = ws, .op = .{ .attach = driver } });
driver.run();
// Release first: until the loop has let go of this websocket it can
- // still drop the link, and onLinkDisconnect requests a terminate. Doing
- // it the other way round left that request landing after the cancel,
- // so the teardown below ran with a pending terminate -- which is the
- // one thing the cancel is here to prevent (cdp.deinit() and
- // bidi.deinit() in our caller need V8 in a usable state).
+ // still drop the link, and onLinkDisconnect requests a terminate.
Worker.releaseConnection(server, ws);
- driver.browser.env.cancelTerminate();
+
+ // CDP/BiDi close the session before Browser.deinit, so disarm now.
+ driver.browser.prepareForTeardown();
}
// Worker -> loop: synchronous release. Blocks until the loop has dropped the