While Loop Inside Recursive Calls

Home » Asterisk Users » While Loop Inside Recursive Calls
Asterisk Users 1 Comment

Hi All

What happens if I, inside of a while/endwhile loop, call recursively a macro that itself has another while/endwhile loop and calls exitwhile() inside the recursive call?

[macro-recursive]
instance=$ARG1
if instance>3
macroexit X=1
_var1=””
; recursive loop while(Dcondition)
_var1=value_${X}
recursive(instance+1)
if var1==””
ExitWhile()
X=X+1
some_Dcondition_changing bla… bla… EndWhile macroexit

[some_running_context]
; Main loop while(Zcondition)
recursive(1)
if (var1==””)
ExitWhile some_Zcondition_changing bla… bla… endwhile more_intructions MacroExit

The problem I see is if I reentry “macro-recursive” a the second time, Macroexit returns to “more_instructions” at “some_running_context” (just after the Main Loop endWhile). It should return to ‘if (var1==””)’ at the previous
“macro-recursive” call.

It is like the second call to “macro-recursive” overwrites the first
“macro-recursive” while/endwhile loop return point, and the second ExitWhile exits from the while/endwhile at “some_running_context” and not from first
“macro-recursive” instance.

In short: second (and subsequent??) call to macro-recursive is ExitWhile()ing from “some_running_context” while loop. First call exits ok.

Any directions to share?

Is this explanation too complicated to understand?

Regards

Ethy

One thought on - While Loop Inside Recursive Calls

  • Well, I’ve never seen dialplan like that – is this a very old version of Asterisk, or psuedocode?

    Anyway..

    A subroutine MUST be balanced, and if a subroutine is aborted before it reaches return, you’ll be in all sorts of hell.

    An example:

    You make a subroutine of some sound files you want to loop through to make an IVR menu.

    You call it using Gosub. Someone presses two while the third of 5
    tracks is playing.

    The stack will then be unbalanced and you’ll then be in a potential world of stack overflow and unknown hell.

    So keep the gosubs for things where they cannot be interrupted. Playback is fine. Background and ControlPlayback is not. See?

    Here’s a solution to this problem which caused me HUGE amounts of headaches early on (and I mean days and nights on end!)

    Hope it helps… https://github.com/lardconcepts/asterisk-pbx-tips-and-snips/blob/master/gotchas-and-warnings.md