From fbd554a15f60713dac083b451603afdeff808992 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 4 Feb 2026 16:24:48 +0800 Subject: [PATCH] Call Inpsector::ContextDestroyed This seems to solve some potential use-after-free issues. By informing the Inspector that the context is gone, it seems to effectively ensure that no more messages are sent from the inspector for things related to the context. --- src/browser/js/Env.zig | 11 ++++++++++- src/browser/js/Inspector.zig | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig index 7cc5e3e7..9001d343 100644 --- a/src/browser/js/Env.zig +++ b/src/browser/js/Env.zig @@ -264,8 +264,17 @@ pub fn destroyContext(self: *Env, context: *Context) void { @panic("Tried to remove unknown context"); } } + + const isolate = self.isolate; + if (self.inspector) |inspector| { + var hs: js.HandleScope = undefined; + hs.init(isolate); + defer hs.deinit(); + inspector.contextDestroyed(@ptrCast(v8.v8__Global__Get(&context.handle, isolate.handle))); + } + context.deinit(); - self.isolate.notifyContextDisposed(); + isolate.notifyContextDisposed(); } pub fn runMicrotasks(self: *const Env) void { diff --git a/src/browser/js/Inspector.zig b/src/browser/js/Inspector.zig index 76e8dc9b..7ba97257 100644 --- a/src/browser/js/Inspector.zig +++ b/src/browser/js/Inspector.zig @@ -128,8 +128,8 @@ pub fn contextCreated( } } -pub fn contextDestroyed(self: *Inspector, local: *const js.Local) void { - v8.v8_inspector__Inspector__ContextDestroyed(self.handle, local.handle); +pub fn contextDestroyed(self: *Inspector, context: *const v8.Context) void { + v8.v8_inspector__Inspector__ContextDestroyed(self.handle, context); } pub fn resetContextGroup(self: *const Inspector) void {