Copas 4.2.0
My script should perform HTTP requests to API 24/7 (about 1000 requests per minute), but after some time (sometimes after an hour, sometimes after several days) in the global error handler appears error {"timeout"}
. Not as a string, but as a table.
Timeout error location
The code from the file launcher.lua, through which my scripts (Telegram bots) are launched:
local res,err = pcall(copas.loop, function()
print("Started")
copas.setErrorHandler(function(msg, co)
-- msg = {"timeout"}
-- tostring(co) = "(thread) thread: 0x55ada1cb5e0"
logger(msg, co)
end, true)
end)
...
Executing HTTP request
Simplified view of the function
local copas_request = require("copas.http").request
function http_request(tParams, fSuccess, fError)
copas.addthread(function()
copas.setErrorHandler(function(err, thread)
-- handles errors like TLS/SSL handshake failed: timeout
-- Everything works fine here
timer_Simple(60, function() http_request(tParams, fSuccess, fError) end)
end)
local res, code, headers = copas_request(tParams)
if res then fSuccess(code, res, headers) else fError(code) end
end)
end
In the http_request()
function, the errors are handled normally. Nowhere else is copas.addthread
used, but an error {"timeout"}
appears in the global handler. After this error appears, the execution of http requests starts to fall apart.
Examples of tracebacks:
From the global error handler.
2022-09-12 12:25:46 /home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:79: attempt to call upvalue 'f' (a nil value)
stack traceback:
/home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:79: in function </home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:79>
stack traceback:
launcher.lua:109: in function <launcher.lua:108>
(tail call): ?
/home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:47: in function </home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:45>
(tail call): ?
(tail call): ?
...e/ubuntu/.luarocks/share/lua/5.1/timerwheel/init.lua:136: in function 'step'
/home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1211: in function </home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1208>
(tail call): ?
2022-09-12 12:27:10 ["timeout"]
stack traceback:
launcher.lua:109: in function <launcher.lua:108>
[C]: in function 'oldpcall'
/home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:68: in function </home/ubuntu/.luarocks/share/lua/5.1/coxpcall.lua:64>
(tail call): ?
/home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1041: in function '_doTick'
/home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1332: in function 'step'
/home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1424: in function 'step'
/home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1460: in function </home/ubuntu/.luarocks/share/lua/5.1/copas.lua:1452>
[C]: in function 'pcall'
launcher.lua:102: in main chunk
[C]: ?
The question is, how to make the script fail-safe to such problems? 🤔
P.S. Thank you very much for your work on copas. I'm a bit of a cheapskate, but if there was a "Donate" button here, I'd use it ❤️