daemonize icon indicating copy to clipboard operation
daemonize copied to clipboard

When closing descriptors, set some fixed (configurable?) cap

Open devgs opened this issue 5 years ago • 2 comments

Currently, at daemonize.py#L137:

for fd in range(3, resource.getrlimit(resource.RLIMIT_NOFILE)[0]):

It's not very wise to use RLIMIT_NOFILE for this purpose. In real-world production servers this value may be as high as few tens of millions. In which case, daemonization will spam the system with useless syscalls and be delayed up to tens of seconds because of that.

It's not reasonable to attempt to close more that few thousands of descriptors. After all, daemonization, almost exclusively takes place immediately after the start of application. There's no way that a normal app is going to have this many open descriptors during that time.

devgs avatar Oct 30 '20 07:10 devgs

Is disabling closing of file descriptors altogether and managing your pre-daemonization descriptors yourself a proper way to work around this?

miigotu avatar Aug 02 '21 08:08 miigotu

@miigotu As an option - definitely. But i would suggest adding an optional function parameter (callback) that should handle the closing, to override the built-in logic.

Moreover, the closing should definitely be handled by the child side of the post-fork pair. Imagine a scenario, when the pipe is created. One side (descriptor) belongs to a parent and the other side to the child (now daemon). Parent process can't close any of them before doing the fork. It's the child's responsibility to close the parent's side and vice versa. But both must do it post-fork.

devgs avatar Aug 02 '21 08:08 devgs