[ANY TFS] Catch scripts that freeze server

Discussion in 'Mods & Lua Functions' started by Stigma, Nov 11, 2018.

  1. Stigma

    Stigma Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,227
    Likes Received:
    1,974
    Best Answers:
    341
    put this code in a lib file (global.lua if tfs 1.x):
    Code (Lua):
    1.  
    2. local start = os.time()
    3. local linecount = 0
    4. debug.sethook(function(event, line)
    5.     linecount = linecount + 1
    6.     if os.mtime() - start >= 1 then
    7.         if linecount >= 30000 then
    8.             print(string.format("possible infinite loop in file %s near line %s", debug.getinfo(2).source, line))
    9.             debug.sethook()
    10.         end
    11.         linecount = 0
    12.         start = os.time()
    13.     end
    14. end, "l")
    example output:
    Code (Text):
    1. possible infinite loop in file @data/global.lua near line 83
    reason: i forced this infinite loop to hang server
    Code (Lua):
    1. local i = 1
    2. while true do
    3.     i = i + 1
    4. end
    if this script is producing false positives, change linecount >= 30000 to linecount >= 1000000, there should be no way other than an infinite loop to produce 1,000,000 line executions in 1 second
     
    Last edited by a moderator: Nov 11, 2018
    oshrigames, whiteblXK, cbrm and 2 others like this.
  2. Fresh

    Fresh Quack!

    Joined:
    Oct 21, 2009
    Messages:
    1,529
    Likes Received:
    130
    Best Answers:
    7
    Oh my god! That guy is supa dupa amazing!
    I wish to give him +rep and follow~
     

Share This Page

Loading...