Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What is the default handling of SIGTERM?

What does Python do under the covers by default if it receives a SIGTERM but there is no signal handler registered for it?

like image 614
meteoritepanama Avatar asked Mar 29 '12 17:03

meteoritepanama


People also ask

Can SIGTERM be handled?

The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL , this signal can be blocked, handled, and ignored.

What happens when a process receives SIGTERM?

If a process receives SIGTERM, some other process sent that signal. SIGTERM is the signal that is typically used to administratively terminate a process. That's not a signal that the kernel would send, but that's the signal a process would typically send to terminate (gracefully) another process.

How does signal alarm work Python?

If time is non-zero, this function requests that a SIGALRM signal be sent to the process in time seconds. Any previously scheduled alarm is canceled (only one alarm can be scheduled at any time). The returned value is then the number of seconds before any previously set alarm was to have been delivered.


1 Answers

Building on the answer of Thomas Wouters, python does not register a handler for the SIGTERM signal. We can see this by doing:

In[23]: signal.SIG_DFL == signal.signal(signal.SIGTERM,signal.SIG_DFL) Out[23]: True 

That means that the system will take the default action. On linux, the default action (according to the signal man page) for a SIGTERM is to terminate the process.

Terminating a process means that:

  • the process will simply not be allocated any more time slices during which it can execute code.

    • This means that it will not raise an exception, or call the code in try: finally: blocks, or the __exit__ method of context managers. It will not do those things because that particular python interpreter will never get the chance to execute another instruction.
  • The process's memory and other resources (open files, network sockets, etc...) will be released back to the rest of the system.

like image 188
stochastic Avatar answered Sep 19 '22 16:09

stochastic