Lightpanda: the headless browser designed for AI and automation
mcp: bracket every isolate use, drop park_isolates
Adrià Arrufatd8893f95d75a
diff --git a/src/mcp/HttpServer.zig b/src/mcp/HttpServer.zigindex e1394cd4a..517dd7d19 100644--- a/src/mcp/HttpServer.zig+++ b/src/mcp/HttpServer.zig@@ -282,7 +282,7 @@ fn worker(self: *HttpServer) void {};defer server.deinit();- server.enableIsolateParking();+ server.multi_session = true;self.worker_ok = true;self.worker_ready.set(lp.io);diff --git a/src/mcp/Server.zig b/src/mcp/Server.zigindex bc101aa2b..76084450d 100644--- a/src/mcp/Server.zig+++ b/src/mcp/Server.zig@@ -23,11 +23,10 @@ app: *App,sessions: std.StringHashMapUnmanaged(*lp.ToolSession) = .empty,/// Monotonic counter backing auto-generated session ids (`s1`, `s2`, …).session_seq: u32 = 0,-/// Whether sessions park their isolates between uses (see-/// `ToolSession.enterIsolate`). The HTTP transport sets this; stdio (one-/// isolate, permanently entered by `Env`) leaves it false and keeps its-/// historical behavior.-park_isolates: bool = false,+/// Whether the transport can route a request to a named session. HTTP does+/// (`Mcp-Session-Id`); over stdio the session tools are refused, since a+/// session created there could never be addressed.+multi_session: bool = false,/// The session the request currently being handled targets. Safe as a single/// field because every request is dispatched on one thread, one at a time;/// the transport sets it (via `useSession`) before each dispatch. Tools and@@ -83,7 +82,7 @@ pub fn createSession(self: *Self, id: []const u8) !*lp.ToolSession {}try self.sessions.put(self.allocator, owned_id, entry);- self.exitIsolate(entry);+ entry.exitIsolate();return entry;}@@ -91,25 +90,6 @@ fn isDefault(id: []const u8) bool {return std.mem.eql(u8, id, default_session_id);}-/// Switch to the multi-isolate discipline: park the default (which `Server.init`-/// left entered) and require every use to bracket with `enterIsolate`. The HTTP-/// transport calls this on its worker thread before serving anyone.-pub fn enableIsolateParking(self: *Self) void {- self.park_isolates = true;- self.exitIsolate(self.defaultSession());-}--/// `ToolSession.enterIsolate`, unless stdio's single isolate is permanently-/// current. Must bracket any use of the session (dispatch, idle pumping,-/// teardown).-pub fn enterIsolate(self: *Self, entry: *lp.ToolSession) void {- if (self.park_isolates) entry.enterIsolate();-}--pub fn exitIsolate(self: *Self, entry: *lp.ToolSession) void {- if (self.park_isolates) entry.exitIsolate();-}-/// Tear down the session named `id`. Returns false if no such session, or if/// it is the default (which lives for the whole process).pub fn closeSession(self: *Self, id: []const u8) bool {@@ -127,7 +107,7 @@ fn destroySession(self: *Self, id: []const u8, entry: *lp.ToolSession) void {}}- self.enterIsolate(entry);+ entry.enterIsolate();entry.deinit();self.allocator.free(id);self.allocator.destroy(entry);@@ -163,9 +143,9 @@ pub fn idle(self: *Self) u31 {while (it.next()) |entry| {// Pumping may resume JS (e.g. a completed script fetch), so it needs// the session's isolate current.- self.enterIsolate(entry.*);+ entry.*.enterIsolate();wait = @min(wait, entry.*.session.idleSlice());- self.exitIsolate(entry.*);+ entry.*.exitIsolate();}return wait;}@@ -198,8 +178,8 @@ pub fn handleToolList(self: *Self, arena: std.mem.Allocator, req: protocol.Requepub fn handleToolCall(self: *Self, arena: std.mem.Allocator, req: protocol.Request) !void {// Dispatch runs page JS, so enter the target isolate around it.const entry = self.active_session;- self.enterIsolate(entry);- defer self.exitIsolate(entry);+ entry.enterIsolate();+ defer entry.exitIsolate();return tools.handleCall(self, arena, req);}@@ -209,8 +189,8 @@ pub fn handleResourceList(self: *Self, req: protocol.Request) !void {pub fn handleResourceRead(self: *Self, arena: std.mem.Allocator, req: protocol.Request) !void {const entry = self.active_session;- self.enterIsolate(entry);- defer self.exitIsolate(entry);+ entry.enterIsolate();+ defer entry.exitIsolate();return resources.handleRead(self, arena, req);}diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex f2f001e81..31444d7f7 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -207,12 +207,11 @@ fn handleSave(server: *Server, arena: std.mem.Allocator, id: std.json.Value, argtry sendToolResultText(server, id, msg, false);}-/// The session tools require the HTTP transport's parked-isolate discipline:-/// a second session means a second V8 isolate, only safe when isolates are-/// entered around use. Over stdio (one permanently-entered isolate) they are-/// all unsupported, kept uniform so clients see one consistent rule.+/// The session tools need a transport that routes by session id (HTTP's+/// `Mcp-Session-Id`). Over stdio they are all unsupported, kept uniform so+/// clients see one consistent rule.fn requireMultiSession(server: *Server, id: std.json.Value) !bool {- if (server.park_isolates) return true;+ if (server.multi_session) return true;try sendToolResultText(server, id, "multiple sessions require the HTTP transport (start with --port)", true);return false;}@@ -1036,6 +1035,10 @@ test "MCP - Actions: click, fill, scroll, hover, press, selectOption, setCheckedvar out: std.Io.Writer.Allocating = .init(aa);const server = try testLoadPage("http://localhost:9582/src/browser/tests/mcp_actions.html", &out.writer);defer server.deinit();+ // Poking the page directly (node registration, JS) needs its isolate+ // entered, as a tool dispatch would.+ server.active_session.enterIsolate();+ defer server.active_session.exitIsolate();const frame = server.active_session.session.currentFrame().?;@@ -1173,6 +1176,10 @@ test "MCP - click that navigates clears node registry" {var out: std.Io.Writer.Allocating = .init(aa);const server = try testLoadPage("http://localhost:9582/src/browser/tests/mcp_nav.html", &out.writer);defer server.deinit();+ // Poking the page directly (node registration, JS) needs its isolate+ // entered, as a tool dispatch would.+ server.active_session.enterIsolate();+ defer server.active_session.exitIsolate();const before_frame = server.active_session.session.currentFrame().?;const link = before_frame.document.getElementById("navlink", before_frame).?.asNode();@@ -1198,6 +1205,10 @@ test "MCP - Actions by selector: hover, selectOption, setChecked" {var out: std.Io.Writer.Allocating = .init(aa);const server = try testLoadPage("http://localhost:9582/src/browser/tests/mcp_actions.html", &out.writer);defer server.deinit();+ // Poking the page directly (node registration, JS) needs its isolate+ // entered, as a tool dispatch would.+ server.active_session.enterIsolate();+ defer server.active_session.exitIsolate();// Single-page test: reach straight into the live page.const page = server.active_session.session.pages.items[0];@@ -1580,8 +1591,7 @@ test "MCP - sessions: new, list, attach isolation, close" {var out: std.Io.Writer.Allocating = .init(aa);var server = try Server.init(testing.allocator, testing.test_app, &out.writer);defer server.deinit();- // Session tools require the HTTP transport's parked-isolate discipline.- server.enableIsolateParking();+ server.multi_session = true;try router.handleMessage(server, aa,\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"session_new","arguments":{"name":"a"}}}@@ -1628,10 +1638,14 @@ fn testLoadPage(url: [:0]const u8, writer: *std.Io.Writer) !*Server {var server = try Server.init(testing.allocator, testing.test_app, writer);errdefer server.deinit();- const page = try server.active_session.session.createPage();+ const session = server.active_session;+ session.enterIsolate();+ defer session.exitIsolate();++ const page = try session.session.createPage();try page.navigate(url, .{});- var runner = server.active_session.session.runner(.{});+ var runner = session.session.runner(.{});try runner.waitForFrame(page.frame_id, 2000, .{ .until = .done });return server;}