|
2025-03-20
| ||
| 13:47 | • Closed ticket [af0e43c96e]: some tailcall operations evade recursion limit check... artifact: 1ad422f4ab user: sebres | |
|
2024-02-07
| ||
| 17:23 | • Ticket [af0e43c96e]: 3 changes artifact: 5c772f2441 user: sebres | |
| 16:32 | • New ticket [af0e43c96e]. artifact: 71b6bfd95b user: dgp | |
some tailcall operations evade recursion limit checks
Declared a "separate issue" in [8e666d7c95], there seem to be some circumstances where [tailcall] operations never encounter testing of the interp recursionlimit and lock up Tcl in an infinite recursion. Should not lose sight of this.
Not "some circumstances" - basically it never tests interp recursionlimit:
% interp recursionlimit {}
1000
% proc test {{cnt 0}} { if {$cnt >= 1e5} {return STOPPED}; tailcall test [incr cnt] }; test
STOPPED
... and it is good so in my opinion:
tailcallis not a recursion directly, because it builds a function invocation stacklessly (replaces the current frame with invoked frame)moreover it is rather a replacement for (cyclic) uplevel call, and as its docu says:
The tailcall command replaces the currently executing procedure, lambda application, or method with another command
but also as its TIP says:
The effect of this command is very similar to:
return [uplevel 1 [list [namespace which cmd ] ?arg ...?]]
with the sole exception that the invocation of cmd happens after the currently executing body returns and is not visible in Tcl's call stack.it's one of the advantages of
tailcall;the important reason for
interp recursionlimitis not a prevention against infinite execution (for which one'd rather useinterp limit), but avoidance of stack overflow or OOM situation (mostly before NRE got introduced);many cyclic flows inclusive recursion unfolded to cycle with self-made stack for args as list (no matter NRE or not) would also not implicitly check
interp recursionlimit;
With other words, if it is not a recursion, why shall it check the interp recursionlimit?
Closed for no activity...
Please reopen, Don, if you have some objections.
However one could consider this rather as an RFE, and implement it either as:
interp tailcalllimit or interp limit {} tailcall ?value?
Just it will be hardly possible, IMHO, because in opposition to recursion, it doesn't necessarily have a nesting and/or start and end, or rather it is almost impossible to detect such.
Furthermore how one shall consider single tailcall used instead of return to uplevel-ed command, quasi to yield it to upper frame? Shall that be counted also (together with other tailcalls)?
And if we'd have some cyclic proceedings above?
Note that it is not always possible to detect recursion and a simple rule "tailcall to itself" doesn't always working, because it needs to consider nesting "loops" (e. g. cross tailcall to some parent proc).