Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS lookup before fork causes lookup failure after fork on OSX #75

Open
nickodell opened this issue Jul 31, 2020 · 1 comment
Open

DNS lookup before fork causes lookup failure after fork on OSX #75

nickodell opened this issue Jul 31, 2020 · 1 comment

Comments

@nickodell
Copy link

nickodell commented Jul 31, 2020

I'm running into a really specific and weird bug on OSX.

Reproduction instructions

Use OSX. I am using 10.15.4 right now; I don't know if the specific version matters.

Install daemonize 2.5.0.

Run this script:

#!/usr/bin/env python3
import socket
from daemonize import Daemonize

def run():
    print(socket.gethostbyname('google.com'))


if __name__ == '__main__':
    print(socket.gethostbyname('google.com'))
    daemon = Daemonize(app="monitor_script", pid="./test.pid", action=run, foreground=False)
    daemon.start()

Symptoms of bug

Before forking, the Python program can successfully do a DNS lookup.

The following message appears in Console.app under Log Reports > system.log:

Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script: Starting daemon.
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script: Traceback (most recent call last):
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script:   File "/Users/nick/.pyenv/versions/3.6.10/lib/python3.6/site-packages/daemonize.py", line 248, in start
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script:     self.action(*privileged_action_result)
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script:   File "test.py", line 7, in run
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script:     print(socket.gethostbyname('google.com'))
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script: socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Jul 31 14:22:24 Nicks-MacBook-Pro monitor_script: Stopping daemon.

Variations tried

I have run the reproduction script on Linux, and it can successfully resolve DNS before and after forking. (This is why I marked this as an OSX-specific bug.)

I have run the reproduction script without the first DNS lookup. This works.

I have run the reproduction script with foreground=True. This works.

I've tried putting a sleep between the two DNS lookups. This doesn't help.

Workaround

Don't do DNS lookups before forking.

@miigotu
Copy link

miigotu commented Aug 2, 2021

I don't consider this a bug. If foreground, the file descriptors wouldn't be closed, otherwise they are and socket (SocketIO) uses file descriptors which are kept in a reference list in stdlib socket. If you close them by daemonization, stdlib does not know that descriptors it opened are closed. You could get the fd from socket and pass it to the daemonize call to keep it alive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants