signal-safety of lua_sethook

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

signal-safety of lua_sethook

Reuben Thomas-5
What's the latest on this?

http://lua-users.org/lists/lua-l/2006-10/msg00427.html suggests that
lua_sethook is not, in fact, signal safe, contrary to the assertion in
the source (which also mentions "asynchronous use"). The manual makes
no guarantee at all.

I use lua_sethook in luaposix inside a signal handler, in order to
implement Lua signal handlers. Is there a safer way to achieve this?

--
http://rrt.sc3d.org

Reply | Threaded
Open this post in threaded view
|

Re: signal-safety of lua_sethook

Roberto Ierusalimschy
> What's the latest on this?
>
> http://lua-users.org/lists/lua-l/2006-10/msg00427.html suggests that
> lua_sethook is not, in fact, signal safe, contrary to the assertion in
> the source (which also mentions "asynchronous use"). The manual makes
> no guarantee at all.
>
> I use lua_sethook in luaposix inside a signal handler, in order to
> implement Lua signal handlers. Is there a safer way to achieve this?

As that message says, sethook is not "theoretically" safe. However, the
only problem seems to be the non-atomicity of pointer accesses. So,
it should be safe given the extra condition that read/write of pointer
addresses are atomic (which I believe is true on most machines).

(The problem is that, while reading the hook address, a signal
may change that adrress. Without  atomicity, the read may result
in a value that is neither the original nor the new value.)

I would say that, for practical purposes, the function is safe,
but no standard ensures that.

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: signal-safety of lua_sethook

Reuben Thomas-5
On 1 February 2012 21:56, Roberto Ierusalimschy <[hidden email]> wrote:
>
> I use lua_sethook in luaposix inside a signal handler, in order to
> implement Lua signal handlers. Is there a safer way to achieve this?

As that message says, sethook is not "theoretically" safe. However, the
only problem seems to be the non-atomicity of pointer accesses. So,
it should be safe given the extra condition that read/write of pointer
addresses are atomic (which I believe is true on most machines).

It would be good if this (and any other necessary assumptions) could be documented. Preferably in the manual, but at least in the code.

(The problem is that, while reading the hook address, a signal
may change that adrress. Without  atomicity, the read may result
in a value that is neither the original nor the new value.)

I would say that, for practical purposes, the function is safe,
but no standard ensures that.

And of course there's also the issue of what happens when lua_sethook is interrupted by another call of itself. Not something that you need tackle, but worth documenting, just for those of us who thought it might be easy!

--
http://rrt.sc3d.org