fix(cdp): don't kill WebSocket on unknown domain/method errors

When a CDP command with an unrecognized domain (e.g. `NonExistent.method`)
was sent, the error response was correctly returned but the connection
died immediately after. This happened because dispatch() re-returned the
error after sending the error response, which propagated up through
processMessage() → handleMessage() where `catch return false` closed
the WebSocket connection.

Now the error is only propagated if sendError itself fails (e.g. broken
pipe). Otherwise dispatch() returns normally and the read loop continues.

Fixes #1843

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Navid EMAD
2026-03-15 04:36:47 +01:00
parent a9b9cf14c3
commit a2e66f85a1

View File

@@ -168,13 +168,11 @@ pub fn CDPT(comptime TypeProvider: type) type {
if (is_startup) {
dispatchStartupCommand(&command, input.method) catch |err| {
command.sendError(-31999, @errorName(err), .{}) catch {};
return err;
command.sendError(-31999, @errorName(err), .{}) catch return err;
};
} else {
dispatchCommand(&command, input.method) catch |err| {
command.sendError(-31998, @errorName(err), .{}) catch {};
return err;
command.sendError(-31998, @errorName(err), .{}) catch return err;
};
}
}
@@ -924,18 +922,20 @@ test "cdp: invalid json" {
// method is required
try testing.expectError(error.InvalidJSON, ctx.processMessage(.{}));
try testing.expectError(error.InvalidMethod, ctx.processMessage(.{
try ctx.processMessage(.{
.method = "Target",
}));
});
try ctx.expectSentError(-31998, "InvalidMethod", .{});
try testing.expectError(error.UnknownDomain, ctx.processMessage(.{
try ctx.processMessage(.{
.method = "Unknown.domain",
}));
});
try ctx.expectSentError(-31998, "UnknownDomain", .{});
try testing.expectError(error.UnknownMethod, ctx.processMessage(.{
try ctx.processMessage(.{
.method = "Target.over9000",
}));
});
try ctx.expectSentError(-31998, "UnknownMethod", .{});
}
test "cdp: invalid sessionId" {