using pcall in chdk - error on press() commands - LUA Scripting - CHDK Forum

using pcall in chdk - error on press() commands

  • 6 Replies
  • 3682 Views
using pcall in chdk - error on press() commands
« on: 08 / August / 2009, 15:39:05 »
Advertisements
Code: [Select]
function cam.pressShutter()
  press("shoot_full")
  sleep(1)
  release("shoot_full")
  sleep(1)
  release("shoot_half")
  repeat
    sleep(1)
  until get_shooting() ~= true
end

pcall(cam.pressShutter)

fails because cam.pressShutter uses press() and release() functions. it gives the error -

attempt to yield across metamethod / c-call boundary

does anyone understand why this happens or how to get around it?

*

Offline reyalp

  • ******
  • 13058
Re: using pcall in chdk - error on press() commands
« Reply #1 on: 08 / August / 2009, 19:20:58 »
does anyone understand why this happens
Because press and release must yield, which cannot be done if there's lua stuff on the C stack.
Quote
or how to get around it?
1) Re-write the lua interface to run in it's own task and pass messages to the other tasks as needed ;)
or
2) Implement C co-routines. There are various versions of this floating around the lua community. Lua 5.2 is expected to have this in some form.
3) Re-write your code not to use pcall in this case.
Don't forget what the H stands for.

Re: using pcall in chdk - error on press() commands
« Reply #2 on: 08 / August / 2009, 19:31:32 »
thanks for the detailed description of the problem.

do you know of any other way of implementing a try/catch-ish block?

OR

a way of running lua code on script exit?

I really don't see a way around this particular bug. Its related to this uninformative thread -

http://chdk.setepontos.com/index.php/topic,3247.0.html

Randomly, 1% to 0.1% of the time, there will be an error on release of the shutter. Its possible that there is an underlying race condition but I don't have any more info than that.

grrr.....

*

Offline reyalp

  • ******
  • 13058
Re: using pcall in chdk - error on press() commands
« Reply #3 on: 08 / August / 2009, 20:53:13 »
do you know of any other way of implementing a try/catch-ish block?
I can't think of any that wouldn't suffer the same problem.
Quote
a way of running lua code on script exit?
Not any applicable to this problem (there's "restore()" but that won't help with error exits). It should be relatively easy to hack in C code that restarts a script if it exits with an error. Look in core/kbd.c
Quote
http://chdk.setepontos.com/index.php/topic,3247.0.html
I'd suggest tracking down the actual bug is a better idea than trying to catch the error. You should be able to use the C api debugging functions to get more info. Or failing that, hack some debug code into the various places the lua core can throw errors.

I'm sure this isn't what you want to hear :(
Don't forget what the H stands for.


Re: using pcall in chdk - error on press() commands
« Reply #4 on: 08 / August / 2009, 21:28:55 »
verified that dofile() produces the same problem. getting my build environment up and running...

*

Offline reyalp

  • ******
  • 13058
Re: using pcall in chdk - error on press() commands
« Reply #5 on: 08 / August / 2009, 22:17:18 »
verified that dofile() produces the same problem.
Right, every way of running lua code will go through something like pcall.

Anything that does C->lua->C->lua->C->yield(), because this would require somehow yielding the middle C call.
To put this in CHDK context,
kbd.c does the first C->lua, to run your lua script for a while in the KBD task.  If you call pcall, dofile etc this introduces the next C call, and press() etc make up the final C->yield().

If you google or search the lua list archives for "attempt to yield across metamethod/C-call boundary" you should find more details.

If you look at lib/lua/ldo.c you can see where this happens. You'll also see luaD_rawrunprotected, which is where the setjmp at the heart of lua error handling happens.
Don't forget what the H stands for.

Re: using pcall in chdk - error on press() commands
« Reply #6 on: 08 / August / 2009, 23:48:18 »
stuck trying to build before i can move forward on this - http://chdk.setepontos.com/index.php/topic,2752.msg37653.html#msg37653

i'm happy to dig into whatever i need to in order to fix this.

 

Related Topics