• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[ANY TFS] Catch scripts that freeze server

Lua:
-- CATCH SCRIPTS THAT COPNSUMES CPU
local log_slow_exec_startExecProg = os.clock()
local log_slow_exec_currExecProg = ""

local log_slow_exec_ReportTime = 0.005 -- where 1 is 1 second and 0.005 is 5 ms

-- enable disable this logger
local ExtendedDebug = true

debug.sethook(function(event, line)
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      if log_slow_exec_currExecProg ~= "" then
        local execTime = os.clock() - log_slow_exec_startExecProg
        if execTime > log_slow_exec_ReportTime then
          print(string.format(">>> long file %s executed in %.3f ms", log_slow_exec_currExecProg, execTime*1000))
        end
      end
    end
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      -- init file changed
      log_slow_exec_startExecProg = os.clock()
      log_slow_exec_currExecProg = debug.getinfo(2).source
      -- print ("source changed: " .. log_slow_exec_currExecProg) -- display source file changed for debugging only
    end
end, "l")

local start = os.time()
local linecount = 0
debug.sethook(function(event, line)
    linecount = linecount + 1
    if os.time() - start >= 1 then
        if linecount >= 1000000 then
            print(string.format("possible infinite loop in file %s near line %s", debug.getinfo(2).source, line))
            debug.sethook()
        end
        linecount = 0
        start = os.time()
    end
end, "l")

ThIs should catch the scripts instantly after server opens or the script must be used in order to check if its looping too long or infinite times?
 
Lua:
-- CATCH SCRIPTS THAT COPNSUMES CPU
local log_slow_exec_startExecProg = os.clock()
local log_slow_exec_currExecProg = ""

local log_slow_exec_ReportTime = 0.005 -- where 1 is 1 second and 0.005 is 5 ms

-- enable disable this logger
local ExtendedDebug = true

debug.sethook(function(event, line)
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      if log_slow_exec_currExecProg ~= "" then
        local execTime = os.clock() - log_slow_exec_startExecProg
        if execTime > log_slow_exec_ReportTime then
          print(string.format(">>> long file %s executed in %.3f ms", log_slow_exec_currExecProg, execTime*1000))
        end
      end
    end
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      -- init file changed
      log_slow_exec_startExecProg = os.clock()
      log_slow_exec_currExecProg = debug.getinfo(2).source
      -- print ("source changed: " .. log_slow_exec_currExecProg) -- display source file changed for debugging only
    end
end, "l")

local start = os.time()
local linecount = 0
debug.sethook(function(event, line)
    linecount = linecount + 1
    if os.time() - start >= 1 then
        if linecount >= 1000000 then
            print(string.format("possible infinite loop in file %s near line %s", debug.getinfo(2).source, line))
            debug.sethook()
        end
        linecount = 0
        start = os.time()
    end
end, "l")

ThIs should catch the scripts instantly after server opens or the script must be used in order to check if its looping too long or infinite times?
The script must be in use, some scripts execute things when they are loaded and others when they are used, in any case it will help you to detect.
It's worth mentioning that this is a debugging trick, I hope you're not considering leaving this code forever on your production server.
 
Back
Top