From ff46886865e614e5c66e4bb4cf554dd8ee48a857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 17 May 2026 18:04:51 +0200 Subject: [PATCH] lua_func: nil return is not an error _handle_request was using the 1st pcall return as the error discriminator, so any Lua function returning nothing (e.g. long_wait in the example) was reported as failed. Discriminate on the 2nd return (err) instead, and encode nil result as cjson.null so the returned_value field stays present in the JSON-RPC response. --- src/testium/lua_func/json-rpc.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/testium/lua_func/json-rpc.lua b/src/testium/lua_func/json-rpc.lua index 1cfead2..52f10ae 100644 --- a/src/testium/lua_func/json-rpc.lua +++ b/src/testium/lua_func/json-rpc.lua @@ -41,8 +41,7 @@ end --- INTERNAL: Handle requests from the client function JSONRPC:_handle_request(req) local method = self.methods[req.method] - local ok, ret - local res, err + local ok, ret, err if not method then if req.id then self:_send_error(req.id, string.format("Method '%s' not registered in lua server")) end return @@ -52,15 +51,18 @@ function JSONRPC:_handle_request(req) -- Only send response if it's not a Notification (notifications have no ID) if req.id then - if ok then - res = ret - if res == nil then - self:_send_error(req.id, tostring(err)) - else - self:_send({ jsonrpc = "2.0", result = { returned_value = res }, id = req.id }) - end - else + if not ok then + -- pcall trapped a runtime error in the method itself. + self:_send_error(req.id, tostring(ret)) + elseif err ~= nil then + -- Method ran but signaled a logical error via its 2nd return. self:_send_error(req.id, tostring(err)) + else + -- Success. A user function returning nothing yields ret==nil; + -- encode it as JSON null so "returned_value" stays present. + local val = ret + if val == nil then val = json.null end + self:_send({ jsonrpc = "2.0", result = { returned_value = val }, id = req.id }) end end end