Opened 3 weeks ago

Closed 3 weeks ago

#2467 closed enhancement (wontfix)

In daemon mode nginx completes detaching before the service is ready

Reported by: cryptonector@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version:
Keywords: Cc:
uname -a: all platforms and operating systems
nginx -V: all versions


This affects all versions, and is a very common problem in daemons of all kinds.

This makes tests that start nginx racy, and the workaround is to loop over polling a health end-point, sleeping between polling. The workaround makes tests slower. Similar considerations can affect actual production services.

The bug is here: where ngx_daemon() calls exit(0) immediately on the parent-side of fork(). Instead the parent should block waiting for the child to indicate that it has opened all the sockets it will listen on and called listen() on all of them.

Examples of how to implement this (prefix https to all of these -- I can't as otherwise my ticket gets rejected as spam!):


There are many many more to be found.

Change History (2)

comment:1 by cryptonector@…, 3 weeks ago

Notice that the daemonization code in Heimdal is in a library that supports Windows and POSIX, and has a friendly license.

This pattern is very common nowadays, and on some systems it is absolutely necessary. For example, in SMF (Solaris, Illumos) the service should daemonize in this way so that dependent services don't start until after the dependency is ready rather than merely starting.

comment:2 by Maxim Dounin, 3 weeks ago

Resolution: wontfix
Status: newclosed
Type: defectenhancement

This was discussed excessively in this thread (in Russian):

In particular, this message might be interesting, it contains some summary and a proof-of-concept patch:

In general, this is how most daemons are written in Unix for a long time, including how the daemon(3) library function is expected to be used. While trying to implement synchronization and only exit from the parent process when the daemon is fully up and running might be beneficial, this is not what can be expected from normal daemons.

For nginx, the only difference is the PID file, which might not be already created when the parent process exits. It is, however, not expected to be used right after the nginx start in practical cases.

Considering the above, implementing synchronization during demonization is believed to be an overkill, and the patch referenced above is therefore not committed.

If the particular use case requires using nginx PID file immediately after nginx startup, waiting for it to appear is believed to be trivial. The same approach can and should be used for all daemons unless they are specifically known to create PID files before exiting.

Note: See TracTickets for help on using tickets.