Is it a Python bug that the main thread of a process created in a daemon thread is a daemon itself?

Hello everybody! When I call fork() inside a daemon thread, the main thread in the child process has the "daemon" property set to True. This is very confusing, since the program keeps running while the only thread is a daemon. According to the docs, if all the threads are daemons the program should exit. Here is an example: import os import threading def child(): assert not threading.current_thread().daemon # This shouldn't fail def parent(): new_pid = os.fork() if new_pid == 0: child() else: os.waitpid(new_pid, 0) t = threading.Thread(target=parent) t.setDaemon(True) t.start() t.join() Is it a bug in the CPython implementation? Also let's assume the second example. I have another non-daemon thread in the child process and want to detect this situation. Does anybody know a way to find such fake daemon threads that are really main threads? Best regards, Elizaveta Shashkova.

On Thu, Jun 25, 2015 at 12:23 PM, Elizabeth Shashkova < elizabeth.shashkova@gmail.com> wrote:
When I call fork() inside a daemon thread, the main thread in the child process has the "daemon" property set to True.
Didn't this (or a similar) topic come up here recently? For reference: http://bugs.python.org/issue24512 and https://docs.python.org/3.4/library/multiprocessing.html#contexts-and-start-... from which I quote (emphasis mine): The parent process uses os.fork() to fork the Python interpreter. The child process, when it begins, is *effectively identical to the parent process*. All resources of the parent are inherited by the child process. *Note that safely forking a multithreaded process is problematic*. So, even if you get past this particular problem, the conventional wisdom is, "don't do that." Skip

Hello Elizabeth, On Thu, 25 Jun 2015 20:23:44 +0300 Elizabeth Shashkova <elizabeth.shashkova@gmail.com> wrote:
Hello everybody!
When I call fork() inside a daemon thread, the main thread in the child process has the "daemon" property set to True. This is very confusing, since the program keeps running while the only thread is a daemon. According to the docs, if all the threads are daemons the program should exit. Here is an example:
[...]
Is it a bug in the CPython implementation?
Yes, it looks like a bug. You can report it at http://bugs.python.org
Also let's assume the second example. I have another non-daemon thread in the child process and want to detect this situation. Does anybody know a way to find such fake daemon threads that are really main threads?
There should really be only one fake daemon thread, since there's only one main thread. And after calling fork(), you know what the main thread is :-) Regards Antoine.
participants (3)
-
Antoine Pitrou
-
Elizabeth Shashkova
-
Skip Montanaro