clean shutdown on signal (was Re: shutdown() SEGVs (was Re: [omniORB] atexit, exit, _exit, and ORB::destroy()))

Huw Rogers count0@building2.co.jp
Sat, 27 Jan 2001 03:18:13 +0900


Summary: don't call shutdown() from
within a signal handler under Linux.

Well this cost me several days, but
I figured it out. It's a wierd problem
with LinuxThreads and signals. Hope
this helps someone else out there.
It might be a good idea to note this
somewhere in the documentation
even though it's totally platform specific.

All pthread_* functions are unsafe to be called
from within signal handlers under Linux, and
can apparently cause memory corruption within the
LinuxThreads library if you do so. The only
permissible thing to do in a signal handler
is sem_post(), and have a
thread waiting on the same semaphore
(from the LinuxThreads FAQ, doh).

So you can't call shutdown() directly from
a signal handler under Linux, and neither can
you create a thread to call shutdown()
in the handler. You have to do that before-
hand and have it wait on a semaphore
until the time comes.

So in main():

sem_init(...)
pthread_create(...) // the dedicated finalization thread
sigaction(...) // the signal handler
orb->run() // woken up by the finalization thread calling shutdown()
orb->destroy()
_exit(code)

in the signal handler for SIGINT/SIGTERM:

sem_post(...) // wake up the finalization thread

... and in the finalization thread:

sem_wait(...) // wait to die
orb->shutdown(!0)
return(0)

Cue weary sigh...

    -Huw

>