how can i stop a lua function block/call mid execution - lua

is there a way to stop a lua pcall to a lua function block from mid execution? Im using multiple threads and would be good to cancel a function mid-flight.

The simplest way is to call error('my message') as this will abort the execution and will return nil, 'my message' as the result of pcall(). If you want to abort a running coroutine (or a "main" chunk) from "outside", the only way to do it I know about without modifying Lua VM is to attach a debug hook to coroutine or the main chunk and call error() from that debug hook:
print(1)
local count = 0
debug.sethook(function(event, line)
if count > 1 then error('done', 2) end
count = count + 1
end, "l")
print(2)
print(3)
print("not getting here")
You should see something like this:
1
2
3
script.lua:4: done
In the case of pcall, you will see the parameter of the error() call to be passed as the error message by pcall.
The conditions to check can get quite elaborate; for example, you can check for the hook to be called from a particular function with a certain number of commands executed before aborting.

Related

Get complete stacktrace of a coroutine, including where a coroutine is resumed

A simple example:
coroutine.resume(coroutine.create(function()
print(debug.traceback())
end))
print(debug.traceback())
the output:
stack traceback:
./v.lua:2: in function <./v.lua:1>
stack traceback:
./v.lua:4: in main chunk
[C]: in ?
It shows that traceback inside a coroutine doesn't know about how it is resumed, so that xxx: in main chunk doesn't show.
How can I get the complete stacktrace inside a coroutine?
Well, I found a workaround here.
Since one Lua VM has only one execution point at one time (and that's why a full call stack is there), we can record resumption information manually.
In lua, manually build a resume-stack trace.
local xresume = coroutine.resume
local xtrace = debug.traceback
-- magic here! Take care of main thread.
local mainthr = coroutine.running() -- captureing is a must.
debug.traceback = function(athr)
if athr then return xtrace(athr) end -- no interest in specified thread.
return xtrace(mainthr)
end
coroutine.resume = function(thr, ...)
-- another magic.
local uptrace = debug.traceback
debug.traceback = function(athr)
if athr then return xtrace(athr) end -- no interest in specified thread.
return xtrace(thr) -- trace the stack of thr.
.. '\n' .. uptrace() -- trace from thr's resume point.
end
local result = { xresume(thr, ...) }
debug.traceback = uptrace
return table.unpack(result)
end
Other tips:
Using global table to store threads also works. But you still need to capture main thread so that it can be traced everywhere.
Writing codes in C function can prevent traceback into hooked coroutine.resume and debug.traceback itself, give you a clearer output.
You won't get much performance hit when debug.traceback is not called.

pause function and resume later without corutine

I am trying to implement multitasking to lua, so that I can use multiple Threads on the Node MCU.
My idea was to run the threads as functions, pause them and continue with the next and do that in a loop. The debug.sethook function seemed prommising but didn't work with the corutines, they executed the hook only after the corutine finished.
I only really need a way to pause a functon.
mt = {}
mt.threadList = {}
function mt.newThread(fnc)
table.insert(mt.threadList,fnc)
end
function mt.update()
for i=1,#mt.threadList do
print("EPOCH: "..i)
debug.sethook(print,"c",40)
coroutine.resume( mt.threadList[i] )
debug.sethook()
end
end
function tA()
for i=1,100 do
print("A",i)
end
end
function tB()
for i=1,100 do
print("B",i)
end
end
mt.newThread(tA)
mt.newThread(tB)
mt.update()
coroutine.resume continues a coroutine, not a regular function. Coroutines (from the Lua side) are generated by coroutine.create. coroutine.resume can only be called on the value returned by coroutine.create.
That being said, Lua coroutines are cooperative (hence the term "co-routine"). That means that you're not supposed to be able to arbitrarily interrupt their execution at any particular point. The coroutine itself should decide when to suspend, via a call to coroutine.yield or similar functions.
You can use debug.sethook on a coroutine to set its debug hook (if you don't pass a coroutine to sethook, then it assumes that you're setting the debug hook for the current thread, which is not what you want), and thereby coroutine.yield at arbitrary points in time. But you really shouldn't.
In any case, without coroutines, there is no way to "pause" a function's execution at all. Not even with a debug hook.

How can I stop a Lua coroutine from outside the function?

I'm trying to create a dispatcher which schedules multiple coroutines. The dispatcher needs to pause the coroutine, I can't figure out how to do this.
Update
Instead of kill, I meant to pause the coroutine from the outside.
You can kill a coroutine by setting a debug hook on it that calls error() from that hook. The next time the hook is called, it will trigger error() call, which will abort the coroutine:
local co = coroutine.create(function()
while true do print(coroutine.yield()) end
end)
coroutine.resume(co, 1)
coroutine.resume(co, 2)
debug.sethook(co, function()error("almost dead")end, "l")
print(coroutine.resume(co, 3))
print(coroutine.status(co))
This prints:
2
3
false coro-kill.lua:6: almost dead
dead
library that will yield when you return true in the hook that been set with debug.sethook(co, function() return true end, "y")
the library is enough to create multitask lua system just run require("yieldhook") at very first of your code further info at the git
https://github.com/evg-zhabotinsky/yieldhook
use coroutine.yield(coroutine-you-want-to-pause)

Ada - Task termination error - "Statement expected"

I have the following task
task body auto is
begin
Put_Line( licencepalte.all & " set off.");
delay traveltime.all;
Put_Line( licencepalte.all & " arrived.");
loop
select
indicator.passthrough;
terminate; -- error for this line: 'statement expected'
or
delay 0.2;
Put_Line( licencepalte.all & " is waiting.");
end select;
end loop;
end auto;
where we represent an indicator light and some cars (auto ~ automobiles) with tasks and protecteds. My main issue is, that I don't know, how to terminate, in case the indicator accepts the entry of the auto. You can see what I'm currently trying to do, and it pops up an error (see inline). How do I stop the task once the entry gets accepted? Why does this terminate not work? Thank you!
terminate is not an "action" that you perform. That is, you can't use a terminate statement anywhere you choose in the task body to terminate the task. The way to terminate a task is for the execution to reach the end that ends the body; in your case, exit to exit the loop works, as in Jacob's answer.
The purpose of or terminate is tell the program that a task is eligible for termination (I don't know if there's a better technical term for this). Suppose your task looks like:
task body Task_Type_1 is
begin
loop
select
accept Entry_1(...parameters...) do
-- something
end Entry_1;
or
accept Entry_2(...parameters...) do
-- something
end Entry_2;
end select;
end loop;
end Task_Type_1;
If the "something" code of the accept statements never exits the loop, the task will never terminate. This means that other constructs enclosing the task can never terminate. For example:
procedure Proc is
T1 : Task_Type_1;
begin
-- do some stuff
-- now we're at the end, and we have to wait for T1 to complete
end Proc;
The procedure creates a task of type Task_Type_1 and starts it. Then the body of the procedure is executed. When end Proc; is reached, the procedure doesn't terminate immediately, because it has to wait until the task finishes its job. But the way the task is written, the task will never complete. Therefore Proc will never return, and the program will probably deadlock.
or terminate is how to say that the task could terminate:
task body Task_Type_1 is
begin
loop
select
accept Entry_1(...parameters...) do
-- something
end Entry_1;
or
accept Entry_2(...parameters...) do
-- something
end Entry_2;
or
terminate;
end select;
end loop;
end Task_Type_1;
In this small example, where we have a procedure that just creates this one task, or terminate means: if this task reaches a point where it's blocked in the select because there aren't any entry calls waiting, and if Proc has reached the end of its code, then we terminate the task. The task body exits, any finalization that needs to be done is done, and then Proc can complete.
or terminate can be used only in a "selective accept". If you say select Some_Other_Task.Entry_2(...); so that it blocks until the other task's entry is available, you can't use or terminate in that kind of select.
In a more complex case, a procedure could create two or more tasks. When that procedure reaches its end statement, it won't return until (roughly speaking) all the tasks it creates are completed or all of the tasks that haven't completed are blocked on select statements that have or terminate clauses. If the latter happens, then all of those tasks complete and then the procedure can return.
The rule about "terminate alternatives" is in RM 9.3(6). It speaks in terms of depending on a master; in the example I showed above, Proc is the master.
If I understand your question correctly, an exit would do nicely instead of terminate.

How to exit a Lua script's execution?

I want to exit execution of Lua script on some condition .
Example :
content = get_content()
if not content then
-- ( Here i want some kind of exit function )
next_content = get_content()
--example there can lot of further checks
Here I want that if I am not getting content my script suppose to terminate is should not go to check to next.
Use os.exit() or just return from some "main" function if your script is embedded.
os.exit()
kill process by sending a signal
do return end
stop execution
The two methods are not equal if you want to write and execute some luacode in the interpreter after stopping the execution by launching your program using the -i flag.
th -i main.lua
extract from the lua api doc :
For syntactic reasons, a break or return can appear only as the last statement of a block (in other words, as the last statement in your chunk or just before an end, an else, or an until). For instance, in the next example, break is the last statement of the then block.
local i = 1
while a[i] do
if a[i] == v then break end
i = i + 1
end
Usually, these are the places where we use these statements, because any other statement following them is unreachable. Sometimes, however, it may be useful to write a return (or a break) in the middle of a block; for instance, if you are debugging a function and want to avoid its execution. In such cases, you can use an explicit do block around the statement:
function foo ()
return --<< SYNTAX ERROR
-- `return' is the last statement in the next block
do return end -- OK
... -- statements not reached
end
In lua 5.2.0-beta-rc1+, you can add a label at the end of your code called ::exit:: or something of the like, and then whenever you need to exit the program just call it like this:
goto exit

Resources