2025-07-21 09:10 CEST

View Issue Details Jump to Notes ]
IDProjectCategoryView StatusLast Update
0002499Spring engineLuapublic2011-07-08 17:22
ReporterKingRaptor 
Assigned Tozerver 
PrioritynormalSeveritymajorReproducibilityalways
StatusresolvedResolutionfixed 
Product Version0.82.7+git 
Target VersionFixed in Version0.82.7+git 
Summary0002499: (master) LUS: WaitForTurn in a loop without sleeps causes stack overflow
DescriptionSpawning a chickenflyerqueen in ZK and giving it a move order results in:

[f=0000663] [string "LuaGadgets/Gadgets/unit_script.lua"]:312: stack overflow
[f=0000663] stack traceback:
    [string "LuaGadgets/Gadgets/unit_script.lua"]:312: in function 'WaitForTurn'
    [string "scripts/chickenflyerqueen.lua"]:63: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    ...
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function 'Fly'
    [string "scripts/chickenflyerqueen.lua"]:72: in function <[string "scripts/chickenflyerqueen.lua"]:55>

Adding Sleep(0) before or after each WaitForTurn cures the problem.
Adding Sleep(0) at the start of the loop results in a broken animation, but no stack overflow.
Additional InformationTested with engine version 0.82.3-2330-gab1fc5d

https://code.google.com/p/zero-k/source/browse/trunk/mods/zk/scripts/chickenflyerqueen.lua

A possibly related problem occurs in 0.82.7.1 where using WaitForTurn without any sleeps causes the animation to rapidly speed up until stack overflow. To see this, comment the sleeps out from the Dig() function of https://code.google.com/p/zero-k/source/browse/trunk/mods/zk/scripts/chickenwurm.lua
TagsNo tags attached.
Checked infolog.txt for Errors
Attached Files

-Relationships
+Relationships

-Notes

~0006821

jK (developer)

local function Fly()
        if (isMoving) then
                ...
                Fly()
        end
end

This might not be tail call recursion optimized.
Please write KISS code, a loop is a loop and not recursion ...

~0006822

KingRaptor (reporter)

Last edited: 2011-06-28 05:42

Using a real loop (while true do) instead of recursion merely causes it to hang instead of stack overflow.

Also fixed by Sleep(0).

(In fact, the loop "while true do Sleep(0) end" works fine, which comes across as rather illogical.)

~0006823

Kloot (developer)

"Using a real loop (while true do) instead of recursion merely causes it to hang instead of stack overflow."


Erhm, duh?

Lua threads are not system threads! There is always only one executing code at any point in time and it MUST yield to others or return by itself ==> an infinite loop without any Sleep or WaitFor* call WILL monopolize the CPU.

We cannot fix stack overflows in bad recursive Lua code either.

~0006824

Kloot (developer)

Also, Sleep(0) by design is equal to Sleep(1), Sleep(2), ..., and Sleep(33), the argument is assumed to be in MILLIseconds (because there are 33ms per frame).

~0006832

jK (developer)

WaitFor... still had a bug which got fixed in afc991eac3

~0006961

zerver (reporter)

FYI, the good old CUnitScript::Tick crashbug came back after this commit, imma fix it

~0006973

zerver (reporter)

https://github.com/spring/spring/commit/0d59eb60854e843d24404ed33789ab25a61eb368
+Notes

-Issue History
Date Modified Username Field Change
2011-06-27 08:18 KingRaptor New Issue
2011-06-27 08:18 KingRaptor Graphics Card => unknown
2011-06-28 01:42 jK Note Added: 0006821
2011-06-28 01:46 jK Status new => resolved
2011-06-28 01:46 jK Resolution open => no change required
2011-06-28 01:46 jK Assigned To => jK
2011-06-28 05:36 KingRaptor Note Added: 0006822
2011-06-28 05:36 KingRaptor Status resolved => assigned
2011-06-28 05:36 KingRaptor Resolution no change required => reopened
2011-06-28 05:42 KingRaptor Note Edited: 0006822
2011-06-28 12:02 Kloot Note Added: 0006823
2011-06-28 12:08 Kloot Note Added: 0006824
2011-06-28 12:08 Kloot Status assigned => closed
2011-06-28 12:08 Kloot Resolution reopened => won't fix
2011-06-30 15:24 jK Status closed => assigned
2011-06-30 15:24 jK Resolution won't fix => reopened
2011-06-30 15:25 jK Note Added: 0006832
2011-06-30 15:25 jK Status assigned => resolved
2011-06-30 15:25 jK Resolution reopened => fixed
2011-07-08 03:05 zerver Assigned To jK => zerver
2011-07-08 03:05 zerver Note Added: 0006961
2011-07-08 03:05 zerver Status resolved => feedback
2011-07-08 03:05 zerver Resolution fixed => reopened
2011-07-08 17:22 zerver Note Added: 0006973
2011-07-08 17:22 zerver Status feedback => resolved
2011-07-08 17:22 zerver Fixed in Version => 0.82.7+git
2011-07-08 17:22 zerver Resolution reopened => fixed
+Issue History