lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Mar 25, 2012 at 5:10 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
> On Sun, Mar 25, 2012 at 16:02, Petite Abeille <petite.abeille@gmail.com> wrote:
> This is common and painful problem for all code that deals with
> signals in Lua. Haven't seen lalarm implementation, but signal handler
> usually merely enables the Lua hook to fire on the next instruction
> (since you can't do much in a signal handler), the actual signal
> handler is run only after execution returns to the Lua VM (which
> wouldn't happen until io:read() finishes).

I don't think that's the problem here. What's probably happening is
the signal handler is being attached with signal(), which is legacy,
and has inconsistent behaviours. I have a very dim memory that lalarm
might be able to call either signal or sigaction, but defaults to
signal(), if so, try it with sigaction().

In particular, whether or not signal restarts system calls depends on
the system. The C handler should be registered with sigaction(), which
would allow RESTART to be set to false. That way, the system call will
return with EINTR, it will then hopefully return to the lua VM, and
the VM will cause the hook to fire.

I see this all the time with our lua code, which is usually blocked on
a socket. Symptom is it takes two Ctrl-C to kill from the command line
(first one fires the C sighandler, which clears the handler but just
restarts the blocking system call so no return to the VM occurs; the
second one happens, and since the handler has been cleared, default
action occurs, which is exit).

Its annoying enough I've thought about writing a small sig module
that's capable of getting lua's sig handler for SIGINT, and resetting
the action so it clears RESTART, but not so annoying I've actually
bothered. Two control-Cs in a row isn't so bad for me.  There are
various lua signal handling modules around, but none allow you to
reset how an existing signal handler is set. I'd want this so I can
change the flags of the standalone lua's C handler.

Anyhow, that's what I think is happening.

Cheers,
Sam