[Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function

Abaci B abaci64 at gmail.com
Fri Jul 8 20:11:28 MSD 2016


So after spending more time testing all the options here is what I found.

return "exit" and return "die" will exit the script when called from the
hangup hook but not when called from within any other function.
session:destroy() will only work from the hangup hook but it crashes
FreeSWITCH (master from 10 days ago) with error: "freeswitch:
src/switch_channel.c:1421: switch_channel_set_variable_var_check: Assertion
`channel != ((void *)0)' failed." I will open a Jira for that.
>From the main body of a lua script (not within a function) any return will
exit the script (e.g. return "blah" would also exit);
Calling error() would work from any place except for hangup hook.
So while it would have been nice to have one method that works everywhere,
at least there is a way to do it wherever you are in the code.

Can someone please document this on the lua wiki so that others can save
the time figuring it out, or should I open a Jira for that also?



On Thu, Jul 7, 2016 at 8:17 PM, Michael Jerris <mike at jerris.com> wrote:

> Just returning a value from any random function won't have any effect.
> The return from hangup hook is special as thats a function we call from the
> c code.  Its probably possible to add a method that you could run that
> would terminate the script, but I don't think there is any way to do that
> currently.  On quick review, it looks like the way "exit" return from
> hangup hook is implemented is by way of calling lua_error from the c
> code, which would cause the script to terminate.  Other than that, as was
> said above, the way to end a script is by way of returning from the body of
> it.
>
>
>
> On Jul 7, 2016, at 7:20 PM, Abaci B <abaci64 at gmail.com> wrote:
>
> I just got around to test this again and here is what I found.
> from within the hangup hook you can exit using return "exit" and as noted
> by Andrew it will exit with an error unless you set debug.traceback=nil
> from the main code (not within a function) you can exit by using return
> "exit" (or return anything else) which is just lua exiting but it has to
> be at the end of a block (wither a loop or you can just return "exit" end
> ).
> from within a function there seems to be no way to exit the lua script,
> for scripts that are session based you can probably use session:hangup()
> along with calling return "exit" from the hangup hook, but on any other
> lua scripts (api, event hooks, dialplan hook, startup) there seems to be no
> way.
> So my question is if anyone know another way to exit a lua script from
> within a function or if I should open a Jira.
>
> On Thu, Mar 10, 2016 at 4:21 PM, Abaci B <abaci64 at gmail.com> wrote:
>
>> I would expect to be able to exit from a lua script without a while loop,
>> should I open a feature request on Jira for that?
>>
>> On Wed, Mar 9, 2016 at 1:20 AM, Chad Phillips <chad at apartmentlines.com>
>> wrote:
>>
>>> Andrew,
>>>
>>> Curious if this structure will work at the bottom of either a a Lua
>>> script or Lua function:
>>>
>>> do
>>>   return "exit"
>>> end
>>>
>>> I seem to recall that being a trick that helped for weird 'return' cases.
>>>
>>> Chad
>>>
>>> On Tue, Mar 8, 2016 at 12:54 PM, Andrew Keil <andrew.keil at visytel.com>
>>> wrote:
>>>
>>>> Michael,
>>>>
>>>>
>>>>
>>>> I have just run some more tests and you are correct that calling it
>>>> inside a while loop like what you explained below does work outside of the
>>>> hangup hook handler.  Thanks for providing this extra information.
>>>>
>>>>
>>>>
>>>> It should be noted that it does not work on its own simply at the
>>>> bottom of a Lua function (ie. as the last statement without a while loop)
>>>> -  except as the last statement inside the hangup hook function.
>>>>
>>>>
>>>>
>>>> ie. This works:
>>>>
>>>> ----------------------------------------------------------------
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> function CleanUp()
>>>>
>>>>                 freeswitch.consoleLog("CLEANUP SECTION\n")
>>>>
>>>>                 freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")
>>>>
>>>> end
>>>>
>>>>
>>>>
>>>> function myHangupHook(s, status, arg)
>>>>
>>>>                 freeswitch.consoleLog(string.format("%s
>>>> DETECTED\n",arg))
>>>>
>>>>                 session:hangup()
>>>>
>>>>                 -- Run CleanUp function now since the caller has
>>>> disconnected
>>>>
>>>>                 CleanUp()
>>>>
>>>>                 -- Abort Lua script here to avoid returning to
>>>> MainService()
>>>>
>>>>                 return "exit"
>>>>
>>>> end
>>>>
>>>> -- Setup Hangup event handler here
>>>>
>>>> v_hangup = "HANGUP"
>>>>
>>>> session:setHangupHook("myHangupHook", "v_hangup")
>>>>
>>>>
>>>>
>>>> Whereas this does not work:
>>>>
>>>> ----------------------------------------------------------------
>>>>
>>>>
>>>>
>>>> function CleanUp()
>>>>
>>>>                 freeswitch.consoleLog("CLEANUP SECTION\n")
>>>>
>>>>                 freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")
>>>>
>>>>                 return "exit"
>>>>
>>>> end
>>>>
>>>>
>>>>
>>>> function myHangupHook(s, status, arg)
>>>>
>>>>                 freeswitch.consoleLog(string.format("%s
>>>> DETECTED\n",arg))
>>>>
>>>>                 session:hangup()
>>>>
>>>>                 -- Run CleanUp function now since the caller has
>>>> disconnected
>>>>
>>>>                 CleanUp()
>>>>
>>>> end
>>>>
>>>> -- Setup Hangup event handler here
>>>>
>>>> v_hangup = "HANGUP"
>>>>
>>>> session:setHangupHook("myHangupHook", "v_hangup")
>>>>
>>>>
>>>>
>>>> Regards,
>>>>
>>>>
>>>>
>>>> Andrew
>>>>
>>>>
>>>>
>>>> *From:* freeswitch-users-bounces at lists.freeswitch.org [mailto:
>>>> freeswitch-users-bounces at lists.freeswitch.org] *On Behalf Of *Michael
>>>> Collins
>>>> *Sent:* Saturday, 5 March 2016 8:11 AM
>>>> *To:* FreeSWITCH Users Help <freeswitch-users at lists.freeswitch.org>
>>>> *Subject:* Re: [Freeswitch-users] Re- End Lua script after HangupHook
>>>> handled without all the extra code to handle the return to the function
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Fri, Mar 4, 2016 at 9:19 AM, Abaci B <abaci64 at gmail.com> wrote:
>>>>
>>>> The question is not how to figure out when to exit the lua script, the
>>>> question is *how to exit the lua script*, and that can sometimes be
>>>> tricky or complicated as return "exit" only works from hangup hook.
>>>>
>>>>
>>>>
>>>> Are you positive that it works only from a hangup hook? It seems to
>>>> work at the end of any loop:
>>>>
>>>>
>>>>
>>>> -- testing exit (no session, call with luarun)
>>>>
>>>> freeswitch.consoleLog('INFO',"Starting infinite loop...\n")
>>>>
>>>> while(1) do
>>>>
>>>>   freeswitch.consoleLog('WARNING',"Before exit...\n")
>>>>
>>>>   return "exit"
>>>>
>>>> end
>>>>
>>>> freeswitch.consoleLog('INFO',"All done!\n")
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Or with a session:
>>>>
>>>> -- test exit with session, no hangup hook
>>>>
>>>> session:answer()
>>>>
>>>> freeswitch.consoleLog('INFO',"Entering main loop...\n")
>>>>
>>>> while ( session:ready() == true ) do
>>>>
>>>>   freeswitch.consoleLog('WARNING',"Inside loop...\n")
>>>>
>>>>   return "exit"
>>>>
>>>> end
>>>>
>>>> freeswitch.consoleLog('INFO',"All done!\n")
>>>>
>>>>
>>>>
>>>> In both cases I never see "All done!" at the CLI. Can you try it and
>>>> see if there's a scenario where it does not exit as expected?
>>>>
>>>>
>>>>
>>>> -MC
>>>>
>>>>
>>>>
>>>>
>>>> _________________________________________________________________________
>>>> Professional FreeSWITCH Consulting Services:
>>>> consulting at freeswitch.org
>>>> http://www.freeswitchsolutions.com
>>>>
>>>> Official FreeSWITCH Sites
>>>> http://www.freeswitch.org
>>>> http://confluence.freeswitch.org
>>>> http://www.cluecon.com
>>>>
>>>> FreeSWITCH-users mailing list
>>>> FreeSWITCH-users at lists.freeswitch.org
>>>> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
>>>> UNSUBSCRIBE:
>>>> http://lists.freeswitch.org/mailman/options/freeswitch-users
>>>> http://www.freeswitch.org
>>>>
>>>
>>>
>>> _________________________________________________________________________
>>> Professional FreeSWITCH Consulting Services:
>>> consulting at freeswitch.org
>>> http://www.freeswitchsolutions.com
>>>
>>> Official FreeSWITCH Sites
>>> http://www.freeswitch.org
>>> http://confluence.freeswitch.org
>>> http://www.cluecon.com
>>>
>>> FreeSWITCH-users mailing list
>>> FreeSWITCH-users at lists.freeswitch.org
>>> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
>>> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
>>> http://www.freeswitch.org
>>>
>>
>>
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org
> http://www.freeswitchsolutions.com
>
> Official FreeSWITCH Sites
> http://www.freeswitch.org
> http://confluence.freeswitch.org
> http://www.cluecon.com
>
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
>
>
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org
> http://www.freeswitchsolutions.com
>
> Official FreeSWITCH Sites
> http://www.freeswitch.org
> http://confluence.freeswitch.org
> http://www.cluecon.com
>
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
> http://www.freeswitch.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20160708/d3350d9e/attachment-0001.html 


Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users mailing list