Discussion:
Breaking infinite loops / stopping code execution from C
r***@gmx.net
2002-08-19 22:10:46 UTC
Permalink
Hi there....

I was just wondering if there is any way to immediately stop Lua execution,
say, if some user scripts enter an infinite loop, for example, so that the
interpreter state is/can be restored to resume execution
after the script got edited. There should be some function call available
in the api that tells Lua to end execution on the fly, so that scripts can
be terminated from C.I'd really appreciate some help with this. Thanks in
advance.


Regards, Celvin
Luiz Henrique de Figueiredo
2002-08-19 23:38:28 UTC
Permalink
Post by r***@gmx.net
I was just wondering if there is any way to immediately stop Lua execution,
say, if some user scripts enter an infinite loop
You can set a hook and then a timer and a timer handler that calls lua_error
when the time has elapsed. In Lua 5.0 you'll be able to set "count" hooks
as well.
--lhf
j***@ucl.ac.uk
2002-08-20 11:50:06 UTC
Permalink
I've been trying Lua 5 alpha, and mostly it works fine, except that all my
attempts to use 'global' (legally or illegally) give the same error:

[string "Untitled 1"]:1: unexpected symbol near `global'

(a cursory examination of lparser.c (search for TK_GLOBAL) suggests
it doesn't attempt to handle global statements)

Is this a bug, or am I doing something silly?

jwb
d***@gmx.net
2002-08-20 17:39:08 UTC
Permalink
Post by r***@gmx.net
I was just wondering if there is any way to immediately stop Lua execution,
say, if some user scripts enter an infinite loop
You can set a hook and then a timer and a timer handler that calls lua_error
when the time has elapsed. In Lua 5.0 you'll be able to set "count" hooks
as well.
->-lhf
This can't be sufficient, as lua_error() will call exit() and the
application will immediately terminate.
Setting a hook may be a solution to break infinite loops, but is neither a
good solution, as neither a counting hook nor a timer-based approach are
very flexible. But the main problem is, the script
MUST provide a way to stop execution on demand from the user, i.e. if
he/she clicks a button, which
won't be solved by setting a timer. Terminating the whole application
doesn't seem to be good practice, either. So still, the problem persists...
Kelmar K. Firesun
2002-08-20 21:28:37 UTC
Permalink
----- Original Message -----
From: <***@gmx.net>; "Stratos" <***@gmx.net>
To: "Multiple recipients of list" <lua-***@tecgraf.puc-rio.br>
Sent: Tuesday, August 20, 2002 12:39 PM
Subject: Re: Breaking infinite loops / stopping code execution from C


] ... SNIP ... [
Post by d***@gmx.net
This can't be sufficient, as lua_error() will call exit() and the
application will immediately terminate.
Setting a hook may be a solution to break infinite loops, but is neither a
good solution, as neither a counting hook nor a timer-based approach are
very flexible. But the main problem is, the script
MUST provide a way to stop execution on demand from the user, i.e. if
he/she clicks a button, which
won't be solved by setting a timer. Terminating the whole application
doesn't seem to be good practice, either. So still, the problem persists...
lua_error() doesn't appear to call exit() to me; however, lua_error()
can be easily replaced using lua_register(). You can define your
own custom error handler this way. This allows you to redirect the
output to say, a window, instaead of stderr.

Here's how you can replace it using lua_register():
lua_register(L, LUA_ERRORMESSAGE, lua_myerror);

Hope this helps,
Kelmar K. Firesun (IRL: Bryce Simonds)
r***@gmx.net
2002-08-20 21:55:07 UTC
Permalink
Post by Kelmar K. Firesun
lua_error() doesn't appear to call exit() to me; however, lua_error()
can be easily replaced using lua_register(). You can define your
own custom error handler this way. This allows you to redirect the
output to say, a window, instaead of stderr.
Thanks Kelmar, but this is not my problem after all. I already replaced
error-msg output to a window in my gui application, but this has never been
the problem ;)
If you take a look at the lua_error() implementation in the LUA source:

LUA_API void lua_error (lua_State *L, const char *s) {
if (s) message(L, s);
luaD_breakrun(L, LUA_ERRRUN);
}

void luaD_breakrun (lua_State *L, int errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode;
longjmp(L->errorJmp->b, 1);
}
else {
if (errcode != LUA_ERRMEM)
message(L, "unable to recover; exiting\n");
exit(EXIT_FAILURE);
}
}

You'll see that it WILL call exit(EXIT_FAILURE), which is by the way also
in the docs:

"If lua_error is called from a C function that has been called from Lua,
then the corresponding Lua execution terminates, as if an error had
occurred inside Lua code. Otherwise, the whole host program terminates with
a call to exit(EXIT_FAILURE). "

Actually, my problem is, I'm using LUA to develop an application which is
completely driven by user scripts. It's targeted for an audience with
almost no knowledge in programming/scripting, so there definitely will be a
lot of errors. I'm now in dire need for a way to STOP EXECUTION immediately
if the user clicks a button on the gui. This should stop the whole script
and reset LUA back to a fresh state, breaking out from infinite loops etc.,
but neither terminate nor corrupt the application in another way except for
just stopping script execution. lua_error() WILL terminate the whole host
programm, which is not acceptable. Setting up a timer / counter may work
for infinite loops, but there are other situations when the script needs to
be stopped. Imagine the common Ctrl-Break, which is exactly what I'd need....
I'm almost desperate as I can't seem to find a way on my own, development
has stagnated since I stumpled upon this...hope anyone has an idea, I'd
REALLY appreciate it....
Thanks, Celvin
Kelmar K. Firesun
2002-08-21 04:00:49 UTC
Permalink
----- Original Message -----
From: <***@gmx.net>; "Celvin" <***@gmx.net>
To: "Multiple recipients of list" <lua-***@tecgraf.puc-rio.br>
Sent: Tuesday, August 20, 2002 4:55 PM
Subject: Re: Breaking infinite loops / stopping code execution from C
] ... SNIP ... [
Post by r***@gmx.net
LUA_API void lua_error (lua_State *L, const char *s) {
if (s) message(L, s);
luaD_breakrun(L, LUA_ERRRUN);
}
void luaD_breakrun (lua_State *L, int errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode;
longjmp(L->errorJmp->b, 1);
}
else {
if (errcode != LUA_ERRMEM)
message(L, "unable to recover; exiting\n");
exit(EXIT_FAILURE);
}
}
Hrm, must have confused it with a different part of the source, but you are
correct there.

] ... SNIP ... [
Post by r***@gmx.net
I'm almost desperate as I can't seem to find a way on my own, development
has stagnated since I stumpled upon this...hope anyone has an idea, I'd
REALLY appreciate it....
If I'm reading that correctly, if the Lua code is executing I would
susspect that L->errorJmp would have a valid jump point becuase the
only way for the script code to actually be executed is if it was
started by a previous C call to the Lua API, thus setting the long
jump pointer back to that point.

I.e. this looks to be more of a sanity check than an actual
realworld check.

This does however raise the problem of possible race conflits if
that is true. From the sounds of things your executing the Lua
inturpreture in a seprate thread.

I could also see the user hitting Ctrl+Break when the script >isn't<
running, thus causing luaD_breakrun() to call exit() as well.

What I might do in this situation is build a custom Lua library
with a replaced luaD_breakrun() function (no telling what else calls
it) that calls my own version of exit() or PostQuitMessage() or
whatever else you might need.

I might actually have it send a message to the other thread saying,
"hey there was an error, reset the lua_State variable after I'm done"

Kelmar K. Firesun (IRL: Bryce Simonds)
Lyndon Samson
2002-08-21 06:37:11 UTC
Permalink
You have a few options

1. Change the VM loop continue test so that it checks a global var that you could set on a user interupt.

2. Run LUA in a separate thread and kill that thread on a user interrupt.









---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs

Luiz Henrique de Figueiredo
2002-08-20 12:02:27 UTC
Permalink
Post by j***@ucl.ac.uk
(a cursory examination of lparser.c (search for TK_GLOBAL) suggests
it doesn't attempt to handle global statements)
"global" remains a reserved word in Lua 5.0 (alpha) until we can determine that
the new "globals" scheme is sufficient and "global" is no longer needed. In
other words, "global" is reserved but no semantics is implemented for it
and you get parse (not lex) errors when you use it.
--lhf
Continue reading on narkive:
Search results for 'Breaking infinite loops / stopping code execution from C' (Questions and Answers)
6
replies
Halo game can someone plz tell me the story of it i need a halo addict?
started 2007-10-15 16:19:07 UTC
video & online games
Loading...