Is there a way to configure the Linux OOM killer to kill the parent instead of child process? When the child process is killed that parent keeps running in a bad state. I can detect if the child was killed and then exit but I wanted to know if there is a way to tell the OOM killer to kill the parent process instead of sacrificing a child process.
You can adjust the probability of any given process being selected as victim of OOM killer by changing the value in virtual file
/proc/<pid>/oom_score_adj. The closer the value is 1000 the more probable that process is getting selected by OOM killer.
The logic that OOM killer uses to select the victim has been changed in many different kernel versions. Modern kernel versions prefer to kill the child by default because the usual error case is e.g. web server starting worker processes and in case of error a single worker process gets out of control and starts to consume memory the best corrective action is to kill the child and assume that the parent process can handle the situation when it notices that the child got killed. In case of Apache, it would just log the failure of that given child and keep running. On the other hand, PostgreSQL (which looks very similar to kernel!) will notice the child getting killed and will automatically kill all the other children, too, and restart the parent prosess to make sure that the full system state is fully known. In neither case killing the just parent process would have resulted in better outcome. Google Chrome with multiple tabs (each tab running as a separate child process) works similarly and one tab running out of RAM should not bring the whole browser down.
The problem with
oom_score_adj is that it is NOT a multiplier for the OOM killer selection logic but an extra permille of total system RAM counted against the process. For example, if you have 32 GB of RAM and a Chrome tab has
oom_score_adj value of 300 (as automatically set by Google Chrome host process) and the tab actually consumes 105 MB RAM it will be pretty surely selected as OOM victim because when OOM killer is selecting the victim, kernel OOM killer implementation currently computes the "logical memory usage" of that Chrome child as 105 MB + 300/1000 * 32 GB = 9705 MB. Obviously the system is not going to free 9.7 GB of RAM by killing that process but unless you have even bigger process, it will be selected nonetheless.