Handling disrespectful outages when using plugs and sockets

I have a server that listens for socket connections and performs different actions depending on the request. One of them is long-term database queries for which there are server forks.

The server keeps a log of all active children and whenever it is asked to turn it off, it will kill all its children before exiting. A couple of times I came across a situation where the server crashed or was killed ruthlessly, which led to the fact that the child process became an orphan. If I try to return the server back, it will refuse to say that the listening socket cannot communicate, because this address / port is already connected.

I am looking for a way to improve this situation, so that the main server process can return immediately. I tried to control the parent creature from the child and leave as soon as it disappeared, but this only led to the fact that it had zombie processes, and the socket seems to be still connected.

The server is written in Python, but any explanation or suggestion in any language is welcome.

+3
source share
3 answers

Make your server the leader of a group of processes. In this case, the children stop when the group leader leaves.

Unix- , . , , , , , "" . ( , , .)

+2

listen():

int on = 1;
setsockopt (sockfd_wan, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));

, TCP- ( < 1024). !

Unrelated:

, : , filedescriptor. , , ( lsof netstat!)

, :

int close_on_exec_on(int fd)
{
  return fcntl(fd, F_SETFD, FD_CLOEXEC);
}

close_on_exec_on(sockfd);

, , , , exec.

!

+1

, fork, , , . ? , .

, -. SIGCHLD.

0

Source: https://habr.com/ru/post/1708376/


All Articles