Interrupting a lua script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Interrupting a lua script

Levente Kovacs
Dear List,


I have a C application that runs a LUA script. That script might have an
infinite loop.

Is there any way in my C application to interrupt the script, do something,
and continue with the script?

I can use alarm() to interrupt, but I have no idea how to continue.


Can you help?

Thanks,
Lev

--
Levente Kovacs

Senior Electronic Engineer

W: http://levente.logonex.eu

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Archie Cobbs
On Sun, Jan 26, 2020 at 2:28 PM Levente Kovacs <[hidden email]> wrote:
I have a C application that runs a LUA script. That script might have an
infinite loop.

Is there any way in my C application to interrupt the script, do something,
and continue with the script?

You can use the debug library to insert the appropriate hook callbacks.

This is described in section 25.2 of "Programming in Lua" (4th ed).

See also section 25.4 "Sandboxing".

-Archie

--
Archie L. Cobbs
Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Eretnek Hippi Messiás
In reply to this post by Levente Kovacs
hi there, my hungarian brother! :)

(to the others, who read my walls of texts recently: nothing new here)

i dont use the c api, but i know nothing that would really change the game there. so i know about 2 approaches to break a loop at will. the 1st one is like an event loop, where u can continuously check a dedicated loop condition just for this purpose, so u can safely exit via setting that from within the loop, or maybe with an interrupt handler. the 2nd is to put your loop into pcall() or xpcall() and call error() (or trigger any kinda error) and then u will continue right after the pcall().

at 1st, to not shoot my leg during development (live-coding), i wrote things like:
local hp=100
while whateverCondition and 0<hp do
  hp=hp-1
  doTheBusiness()
end
so simply thats the basic idea, and then, in a function thats executed inside xpcall(), i could figure out a way to leave behind that hp boilerplate with 3 components, and now i write simply brk() right after the beginning of the loop, that gets the current line via the debug library, and uses that as an identifier (or whatever i give it), and then it makes the countdown on its own, and if its over, then it will calls error(), otherwise if the function reaches its end, then i will delete all the counters after the xpcall(). the rest is upon ur imagination, and u can use a counter, a signal, a timer, a variable, a file, an environment variable or whatever to trigger it...

btw, as i mentioned it, i wrote about this a few days ago, and u can search lua-l for "brk", nobody else wrote it after me if im right :D

have fun, bests! :)


Sent with ProtonMail Secure Email.

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Eretnek Hippi Messiás
In reply to this post by Levente Kovacs
ahh, sorry, i just realized that u only wanna suspend it... then i think its a matter of a callback that u change at will, but if its based on a signal, then i think others gonna bring up better ideas than whatever i could... :D


Sent with ProtonMail Secure Email.

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Viacheslav Usov
In reply to this post by Archie Cobbs
On Sun, Jan 26, 2020 at 10:01 PM Archie Cobbs <[hidden email]> wrote:

> You can use the debug library to insert the appropriate hook callbacks.

In certain cases, yes.

In the case when your Lua VM is already running, you would generally need to do something else, and things get involved pretty quickly depending on how general you want your approach to be.

And then there is the case when the Lua code calls into a native function that does not feel like returning any time soon :)

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Levente Kovacs
Thanks for the answers. I think the method is not straight forward, so I
won't implement it.


--
Levente Kovacs

Senior Electronic Engineer

W: http://levente.logonex.eu

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

Oliver Schmidt
In reply to this post by Levente Kovacs
Hi,

On 26.01.20 21:28, Levente Kovacs wrote:
> Is there any way in my C application to interrupt the script, do something,
> and continue with the script?

you might want to have a look at my "mtint" module, see
https://github.com/osch/lua-mtint#mtint. This might not fit 100% your use case,
since it is meant to be a solution used from pure Lua and not from C
applications. But perhaps it might be useful somehow.

Your email inspired me to extend the "mtint" module to also allow installation
of an interrupt handler function that makes it possible to continue the
interrupted thread.

With "mtint" it is possible to interrupt another thread or coroutine from a
concurrently running thread. This can be useful for applications with
interactive user interface when a user wants to abort a long running background
task or a user supplied script that is stuck in an infinite loop.

See Example 3 (https://github.com/osch/lua-mtint#example-3): here a
"computation" background thread is started. The background thread registers an
interrupt handler function and gives its interruptible id back to the main
thread. Then the computation is started and runs in an infinite loop. The main
thread then interrupts the computation thread several times by using the
interuptible id and communicates with the interrupt handler function to give
commands to continue or to quit the computation.

For this example other Lua modules are needed for 1.) starting a concurrently
running thread and for 2.) inter-thread communication. For the example I used
"lua-llthreads2" (see https://luarocks.org/modules/moteus/lua-llthreads2) and
"mtmsg" (see https://luarocks.org/modules/osch/mtmsg) but "mtint" itself does
not depend on these modules and you can use other modules for this if you want.

Best regards,
Oliver

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting a lua script

aryajur
In reply to this post by Levente Kovacs
Just throwing it out there. I had written a module LuaStepper ( https://github.com/aryajur/LuaStepper )  with which you can load Lua Scripts, execute them concurrently, pause them and resume them. It works by setting hooks in the scripts so doesn't depend on any operating system threading. As mentioned before in this thread that the hook approach won't work if the script calls a blocking C function. To avoid that there are some mechanisms in the module to wrap any external code required by the script and then provide it to the executing script to control blocking behavior.

On Mon, Jan 27, 2020 at 11:58 AM Levente Kovacs <[hidden email]> wrote:
Thanks for the answers. I think the method is not straight forward, so I
won't implement it.


--
Levente Kovacs

Senior Electronic Engineer

W: http://levente.logonex.eu