Calling back into Lua: Coroutines, ffi and the C api

  • From: Nils Nordman <nino@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 19 Nov 2013 10:19:19 +0100

Hi,

I have some questions regarding calling back into Lua.

In my application I use a third party library, which I currently
integrate with using both the C api and the ffi. The library needs a
callback; a pure C callback is used for that, which uses the C api to
call back into Lua. On the Lua side, ffi is used to access the
library's functions. Invoking a function in the library using ffi can
result in a callback which then uses the C api to call back into Lua.

A partial call stack can thus end up looking something like this:

C -> C api -> Lua -> ffi call -> C -> C api -> Lua

Which works well (perhaps surprisingly, given the convoluted call chain).

The other day I was playing with coroutines, and ended up with the
following corresponding call stack:

C -> C api -> Lua (coroutine.create/resume) -> ffi call -> C -> C api (SEGV)

Which ends with a SEGV in lua_getfield (e.g. index2adr or lj_str_new),
when the ffi call is made from a non-main coroutine. JIT on or off
didn't make any difference, and the app is single threaded for what
it's worth.

Now the questions that popped up are:

1) Is the SEGV in the coroutine case to be expected (due to
re-entering the Lua state which is in an undefined state, etc.)?

2) Is the first, working, setup actually supported? It has worked well
for a long time, but that might be me just lucking out.

3) In the coroutine case, if the SEGV is to be expected, would the
situation change if a ffi callback was used instead of using the C api
for call backs? Or if the C api was used instead of the ffi?

Apologies if this is already covered somewhere else, but I couldn't
find anything definitive regarding this when searching.

Cheers,

Nils

-- 
Nils Nordman <nino@xxxxxxxxxxx>

Other related posts: