<div dir="ltr"><div>So after spending more time testing all the options here is what I found.</div><div><br></div><div><font face="monospace,monospace">return "exit"</font> and <font face="monospace,monospace">return "die"</font> will exit the script when called from the hangup hook but not when called from within any other function.</div><div><div><font face="monospace,monospace">session:destroy()</font> will only work from the hangup hook but it crashes FreeSWITCH (master from 10 days ago) with error: "<font face="monospace,monospace">freeswitch: src/switch_channel.c:1421: switch_channel_set_variable_var_check: Assertion `channel != ((void *)0)' failed.</font><font face="monospace,monospace">"</font> I will open a Jira for that.</div>From the main body of a lua script (not within a function) any return will exit the script (e.g. <font face="monospace,monospace">return "blah"</font> would also exit);</div><div>Calling <font face="monospace,monospace">error()</font> would work from any place except for hangup hook.</div><div>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.</div><div><br></div><div>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?</div><div><br></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 7, 2016 at 8:17 PM, Michael Jerris <span dir="ltr"><<a href="mailto:mike@jerris.com" target="_blank">mike@jerris.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">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 <span style="color:rgb(191,191,191);font-family:Monaco;font-size:13px;background-color:rgb(0,0,0)">lua_error</span> 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.<div><div class="h5"><div><br><div><br></div><div><br><div><blockquote type="cite"><div>On Jul 7, 2016, at 7:20 PM, Abaci B <<a href="mailto:abaci64@gmail.com" target="_blank">abaci64@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>I just got around to test this again and here is what I found.</div><div>from within the hangup hook you can exit using <font face="monospace,monospace">return "exit"</font> and as noted by Andrew it will exit with an error unless you set <font face="monospace,monospace">debug.traceback=nil</font></div><div>from the main code (not within a function) you can exit by using <font face="monospace,monospace">return "exit"</font> (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 <font face="monospace,monospace">return "exit" end</font>).</div><div>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 <font face="monospace,monospace">session:hangup()</font> along with calling <font face="monospace,monospace">return "exit"</font> from the hangup hook, but on any other lua scripts (api, event hooks, dialplan hook, startup) there seems to be no way.</div><div>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.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 10, 2016 at 4:21 PM, Abaci B <span dir="ltr"><<a href="mailto:abaci64@gmail.com" target="_blank">abaci64@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>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?</div><div><div><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 9, 2016 at 1:20 AM, Chad Phillips <span dir="ltr"><<a href="mailto:chad@apartmentlines.com" target="_blank">chad@apartmentlines.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Andrew,<div><br></div><div>Curious if this structure will work at the bottom of either a a Lua script or Lua function:</div><div><br></div><div>do</div><div> return "exit"</div><div>end</div><div><br></div><div>I seem to recall that being a trick that helped for weird 'return' cases.</div><span><font color="#888888"><div><br></div><div>Chad</div></font></span></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Tue, Mar 8, 2016 at 12:54 PM, Andrew Keil <span dir="ltr"><<a href="mailto:andrew.keil@visytel.com" target="_blank">andrew.keil@visytel.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>
<div lang="EN-AU" link="blue" vlink="purple">
<div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Michael,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">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.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">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.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">ie. This works:<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">----------------------------------------------------------------<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">function CleanUp()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog("CLEANUP SECTION\n")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">end<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">function myHangupHook(s, status, arg)<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog(string.format("%s DETECTED\n",arg))<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> session:hangup()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> -- Run CleanUp function now since the caller has disconnected
<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> CleanUp()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> -- Abort Lua script here to avoid returning to MainService()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> return "exit"
<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">end<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">-- Setup Hangup event handler here<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">v_hangup = "HANGUP"<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">session:setHangupHook("myHangupHook", "v_hangup")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Whereas this does not work:<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">----------------------------------------------------------------<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">function CleanUp()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog("CLEANUP SECTION\n")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> return "exit"
<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">end
<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">function myHangupHook(s, status, arg)<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> freeswitch.consoleLog(string.format("%s DETECTED\n",arg))<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> session:hangup()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> -- Run CleanUp function now since the caller has disconnected
<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> CleanUp()<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">end<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">-- Setup Hangup event handler here<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">v_hangup = "HANGUP"<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">session:setHangupHook("myHangupHook", "v_hangup")<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><a name="m_4531360003437178456_m_1710721875543731147_-1920980919_-766515437_1060642169__MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Regards,<u></u><u></u></span></a></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Andrew<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p><p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> <a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a> [mailto:<a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a>]
<b>On Behalf Of </b>Michael Collins<br>
<b>Sent:</b> Saturday, 5 March 2016 8:11 AM<br>
<b>To:</b> FreeSWITCH Users Help <<a href="mailto:freeswitch-users@lists.freeswitch.org" target="_blank">freeswitch-users@lists.freeswitch.org</a>><br>
<b>Subject:</b> Re: [Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function<u></u><u></u></span></p><p class="MsoNormal"><u></u> <u></u></p>
<div><p class="MsoNormal"><u></u> <u></u></p>
<div><p class="MsoNormal"><u></u> <u></u></p>
<div><p class="MsoNormal">On Fri, Mar 4, 2016 at 9:19 AM, Abaci B <<a href="mailto:abaci64@gmail.com" target="_blank">abaci64@gmail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div><p class="MsoNormal">The question is not how to figure out when to exit the lua script, the question is <b>how to exit the lua script</b>, and that can sometimes be tricky or complicated as return "exit" only works from hangup hook.<u></u><u></u></p>
</div>
</blockquote>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal">Are you positive that it works only from a hangup hook? It seems to work at the end of any loop:<u></u><u></u></p>
</div>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">-- testing exit (no session, call with luarun)</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">freeswitch.consoleLog('INFO',"Starting infinite loop...\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">while(1) do</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New""> freeswitch.consoleLog('WARNING',"Before exit...\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New""> return "exit"</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">freeswitch.consoleLog('INFO',"All done!\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal">Or with a session:<u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">-- test exit with session, no hangup hook</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">session:answer()</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">freeswitch.consoleLog('INFO',"Entering main loop...\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">while ( session:ready() == true ) do</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New""> freeswitch.consoleLog('WARNING',"Inside loop...\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New""> return "exit"</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">end</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><span style="font-family:"Courier New"">freeswitch.consoleLog('INFO',"All done!\n")</span><u></u><u></u></p>
</div>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal">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?<u></u><u></u></p>
</div>
<div><p class="MsoNormal"><u></u> <u></u></p>
</div>
<div><p class="MsoNormal">-MC<u></u><u></u></p>
</div>
<div><p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<br></div></div><span>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com/" target="_blank" rel="noreferrer">http://www.freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org/" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org/" target="_blank" rel="noreferrer">http://confluence.freeswitch.org</a><br>
<a href="http://www.cluecon.com/" target="_blank" rel="noreferrer">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org/" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br></span></blockquote></div><br></div>
<br>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com/" target="_blank" rel="noreferrer">http://www.freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org/" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org/" target="_blank" rel="noreferrer">http://confluence.freeswitch.org</a><br>
<a href="http://www.cluecon.com/" target="_blank" rel="noreferrer">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org/" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br></blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>
_________________________________________________________________________<br>Professional FreeSWITCH Consulting Services: <br><a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a><br><a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br><br>Official FreeSWITCH Sites<br><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br><a href="http://confluence.freeswitch.org" target="_blank">http://confluence.freeswitch.org</a><br><a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br><br>FreeSWITCH-users mailing list<br><a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br><a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a></div></blockquote></div><br></div></div></div></div></div><br>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank" rel="noreferrer">http://www.freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org" target="_blank" rel="noreferrer">http://confluence.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank" rel="noreferrer">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br></blockquote></div><br></div></div>