April 28, 2012
How can i catch kill signal to gracefully shutdown my app?

April 28, 2012
On Apr 28, 2012, at 1:04 PM, "Andrea Fontana" <nospam@example.com> wrote:

> How can i catch kill signal to gracefully shutdown my app?

Gracefully?  That's tough. I'd set a flag, then exit the signal handler. All threads should periodically check the flag and throw an Error if they see it. Or an Exception you're sure won't be caught.  You really can't do much in a signal handler, so it is t safe to do something fancier there.
April 28, 2012
On Sat, Apr 28, 2012 at 02:08:06PM -0700, Sean Kelly wrote:
> On Apr 28, 2012, at 1:04 PM, "Andrea Fontana" <nospam@example.com> wrote:
> 
> > How can i catch kill signal to gracefully shutdown my app?
> 
> Gracefully?  That's tough. I'd set a flag, then exit the signal handler. All threads should periodically check the flag and throw an Error if they see it. Or an Exception you're sure won't be caught. You really can't do much in a signal handler, so it is t safe to do something fancier there.

My favorite solution to this is to use the self-pipe trick:

	http://evbergen.home.xs4all.nl/unix-signals.html

MUCH safer than setting a flag, or trying to do too much in a signal handler, and can integrate seamlessly if your program is already using a select loop.

Of course, if you're using threads then things may be a bit more complicated. But still, moving the actual handling out of the signal handler is a good thing; it lets you do more complex stuff (like communicate with threads to stop, etc.).

Note, however, that SIGKILL is not catchable, ever.


T

-- 
MAS = Mana Ada Sistem?
April 28, 2012
Le 28/04/2012 22:04, Andrea Fontana a écrit :
> How can i catch kill signal to gracefully shutdown my app?
>

This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/

But still, you can't catch kill and it is linux specific.
April 28, 2012
On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
> Le 28/04/2012 22:04, Andrea Fontana a écrit :
> >How can i catch kill signal to gracefully shutdown my app?
> >
> 
> This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/
> 
> But still, you can't catch kill and it is linux specific.

AFAIK, SIGKILL cannot be caught on any posix system.


T

-- 
The easy way is the wrong way, and the hard way is the stupid way. Pick one.
April 28, 2012
Of course I mean sigterm.
I mixed sigterm and kill (not -9) utility and i wrote sigkill :)

On Saturday, 28 April 2012 at 22:22:00 UTC, H. S. Teoh wrote:
> On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
>> But still, you can't catch kill and it is linux specific.
>
> AFAIK, SIGKILL cannot be caught on any posix system.
>
>
> T


April 29, 2012
Le 29/04/2012 00:23, H. S. Teoh a écrit :
> On Sun, Apr 29, 2012 at 12:16:15AM +0200, deadalnix wrote:
>> Le 28/04/2012 22:04, Andrea Fontana a écrit :
>>> How can i catch kill signal to gracefully shutdown my app?
>>>
>>
>> This can be used for other signals : http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/
>>
>> But still, you can't catch kill and it is linux specific.
>
> AFAIK, SIGKILL cannot be caught on any posix system.
>
>
> T
>

You misunderstood me (the sentence was not clear). I mean, you cannot catch kill and the trick mentioned int he link is linux specific. Obviously, the fact that you cannot catch kill isn't linux specific ;)
April 30, 2012
On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote: [...]
> You misunderstood me (the sentence was not clear). I mean, you cannot catch kill and the trick mentioned int he link is linux specific. Obviously, the fact that you cannot catch kill isn't linux specific ;)

Ah, I see.

But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong.


T

-- 
They pretend to pay us, and we pretend to work. -- Russian saying
April 30, 2012
On Apr 29, 2012, at 9:28 PM, H. S. Teoh wrote:

> On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote: [...]
>> You misunderstood me (the sentence was not clear). I mean, you cannot catch kill and the trick mentioned int he link is linux specific. Obviously, the fact that you cannot catch kill isn't linux specific ;)
> 
> Ah, I see.
> 
> But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong.

It's legal to call write() in a signal handler, so if the socket is calling select() or poll() or whatever, something along those lines should work just fine.  As for catching kill, I thought the signal handler would fire but when it exited the app would still terminate.  No?
April 30, 2012
On Mon, Apr 30, 2012 at 06:50:50AM -0700, Sean Kelly wrote:
> On Apr 29, 2012, at 9:28 PM, H. S. Teoh wrote:
> 
> > On Sun, Apr 29, 2012 at 11:47:24PM +0200, deadalnix wrote: [...]
> >> You misunderstood me (the sentence was not clear). I mean, you cannot catch kill and the trick mentioned int he link is linux specific.  Obviously, the fact that you cannot catch kill isn't linux specific ;)
> > 
> > Ah, I see.
> > 
> > But really? I thought the self-pipe trick is portable across Posix platforms. But then again, my last contact with a non-Linux Posix system was, oh, at least 8 years ago with a Solaris workstation, so I could be totally wrong.
> 
> It's legal to call write() in a signal handler, so if the socket is
> calling select() or poll() or whatever, something along those lines
> should work just fine.  As for catching kill, I thought the signal
> handler would fire but when it exited the app would still terminate.
> No?

IIRC, catching SIGTERM allows you to not terminate (that's how SIG_IGN is implemented, I think). But SIGKILL isn't even catchable to begin with, you'll just terminate no matter what.

But again, my impression may be biased by exclusive use of Linux for the last 8 years or so.


T

-- 
GEEK = Gatherer of Extremely Enlightening Knowledge
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home