This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ThreadingMixIn.daemon_threads is not honored when parent is daemon
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: flox Nosy List: flox, neologix, python-dev, vstinner
Priority: normal Keywords: needs review, patch

Created on 2011-10-09 20:18 by flox, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_socketserver.py flox, 2011-10-09 20:18
socketserver_daemon.diff neologix, 2011-10-21 20:15 review
issue13140.diff flox, 2011-10-22 23:14 review
Messages (6)
msg145273 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-10-09 20:18
I use the socketserver.ThreadingMixIn to create a TCPServer.

I set the server thread as daemon (t.daemon=True).
But I want the client threads to run as non-daemon.
According to the documentation, the "daemon_threads" class attribute should do the trick.

But it fails: if server is daemon, the clients are daemon too, even if daemon_threads=False.

Demo attached.
msg146127 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2011-10-21 20:15
"""
        """Start a new thread to process the request."""
        t = threading.Thread(target = self.process_request_thread,
                             args = (request, client_address))
        if self.daemon_threads:
            t.daemon = True
"""

If daemon_threads is false, t.daemon is not set, and the daemonic property is inherited from the creating thread, i.e. the server thread.
Patch attached (I don't think a test is necessary for such a trivial change).
msg146201 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-10-22 23:14
I would prefer to preserve the inheritance by default, and to change the daemonic attribute only if it is explicitly set to True or False.
This way it will be backward compatible.

Patch attached.
msg146227 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2011-10-23 11:28
> I would prefer to preserve the inheritance by default, and to change the daemonic attribute only if it is explicitly set to True or False.
> This way it will be backward compatible.

It may be backward compatible, but IMHO, the current behavior is
broken: while it can certainly make sense to set the server thread
daemonic, you most certainly don't want to have client threads
daemonic implicitely (since you usually don't want to terminate the
client's connections abruptly when the main thread exits).
But I must admit I don't have a strong opinion, so both solutions are OK to me.
The only thing that bothers me is this:
"""
+.. versionchanged:: 3.3
+   previously, the *daemon_threads = False* flag was ignored.
"""

You usually document new features or behavior changes: this really
looks like a bug fix (and is one actually). Something like "the
semantics of *daemon_threads* changed slighty" might be better (but
I'm no native speaker).
msg146988 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-11-03 22:26
I agree it is a bug. It does not make sense to preserve this behaviour.
I plan to apply the initial patch by Charles-François to 2.7 and 3.x.
msg147001 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-04 09:23
New changeset f09e3b1603ee by Florent Xicluna in branch '2.7':
Issue #13140: Fix the daemon_threads attribute of ThreadingMixIn.
http://hg.python.org/cpython/rev/f09e3b1603ee

New changeset 94017ce9304d by Florent Xicluna in branch '3.2':
Closes #13140: Fix the daemon_threads attribute of ThreadingMixIn.
http://hg.python.org/cpython/rev/94017ce9304d

New changeset 6fe6769e54a5 by Florent Xicluna in branch 'default':
Merge 3.2: issue #13140
http://hg.python.org/cpython/rev/6fe6769e54a5
History
Date User Action Args
2022-04-11 14:57:22adminsetgithub: 57349
2011-11-04 09:23:37python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg147001

resolution: fixed
stage: patch review -> resolved
2011-11-03 22:26:46floxsetassignee: flox
2011-11-03 22:26:30floxsetmessages: + msg146988
2011-10-23 11:28:54neologixsetmessages: + msg146227
2011-10-22 23:14:29floxsetfiles: + issue13140.diff

messages: + msg146201
versions: - Python 2.6
2011-10-21 20:15:12neologixsetfiles: + socketserver_daemon.diff

nosy: + vstinner, neologix
messages: + msg146127

keywords: + patch, needs review
stage: needs patch -> patch review
2011-10-09 20:18:44floxcreate