bpo-33540: Add block_on_close attr to socketserver (GH-6911) · python/cpython@fa286ed (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -116,10 +116,13 @@ server classes.
116 116 only available on POSIX platforms that support :func:`~os.fork`.
117 117
118 118 :meth:`socketserver.ForkingMixIn.server_close` waits until all child
119 - processes complete.
119 + processes complete, except if
120 +:attr:`socketserver.ForkingMixIn.block_on_close` attribute is false.
120 121
121 122 :meth:`socketserver.ThreadingMixIn.server_close` waits until all non-daemon
122 - threads complete. Use daemonic threads by setting
123 + threads complete, except if
124 +:attr:`socketserver.ThreadingMixIn.block_on_close` attribute is false. Use
125 + daemonic threads by setting
123 126 :data:`ThreadingMixIn.daemon_threads` to ``True`` to not wait until threads
124 127 complete.
125 128
@@ -128,6 +131,8 @@ server classes.
128 131 :meth:`socketserver.ForkingMixIn.server_close` and
129 132 :meth:`socketserver.ThreadingMixIn.server_close` now waits until all
130 133 child processes and non-daemonic threads complete.
134 + Add a new :attr:`socketserver.ForkingMixIn.block_on_close` class
135 + attribute to opt-in for the pre-3.7 behaviour.
131 136
132 137
133 138 .. class:: ForkingTCPServer
Original file line number Diff line number Diff line change
@@ -1216,6 +1216,18 @@ by default.
1216 1216 (Contributed by Christian Heimes in :issue:`28134`.)
1217 1217
1218 1218
1219 +socketserver
1220 +------------
1221 +
1222 +:meth:`socketserver.ThreadingMixIn.server_close` now waits until all non-daemon
1223 +threads complete. :meth:`socketserver.ForkingMixIn.server_close` now waits
1224 +until all child processes complete.
1225 +
1226 +Add a new :attr:`socketserver.ForkingMixIn.block_on_close` class attribute to
1227 +:class:`socketserver.ForkingMixIn` and :class:`socketserver.ThreadingMixIn`
1228 +classes. Set the class attribute to ``False`` to get the pre-3.7 behaviour.
1229 +
1230 +
1219 1231 sqlite3
1220 1232 -------
1221 1233
@@ -2156,10 +2168,17 @@ Changes in the Python API
2156 2168 and module are affected by this change. (Contributed by INADA Naoki and
2157 2169 Eugene Toder in :issue:`29463`.)
2158 2170
2159 -* :meth:`~socketserver.BaseServer.server_close` in
2160 -:class:`socketserver.ThreadingMixIn` and :class:`socketserver.ForkingMixIn`
2161 - now waits until all non-daemon threads complete.
2162 - (Contributed by Victor Stinner in :issue:`31233` and :issue:`31151`.)
2171 +* :meth:`socketserver.ThreadingMixIn.server_close` now waits until all
2172 + non-daemon threads complete. Set the new
2173 +:attr:`socketserver.ThreadingMixIn.block_on_close` class attribute to
2174 + ``False`` to get the pre-3.7 behaviour.
2175 + (Contributed by Victor Stinner in :issue:`31233` and :issue:`33540`.)
2176 +
2177 +* :meth:`socketserver.ForkingMixIn.server_close` now waits until all
2178 + child processes complete. Set the new
2179 +:attr:`socketserver.ForkingMixIn.block_on_close` class attribute to ``False``
2180 + to get the pre-3.7 behaviour.
2181 + (Contributed by Victor Stinner in :issue:`31151` and :issue:`33540`.)
2163 2182
2164 2183 * The :func:`locale.localeconv` function now temporarily sets the ``LC_CTYPE``
2165 2184 locale to the value of ``LC_NUMERIC`` in some cases.
Original file line number Diff line number Diff line change
@@ -543,6 +543,8 @@ class ForkingMixIn:
543 543 timeout = 300
544 544 active_children = None
545 545 max_children = 40
546 +# If true, server_close() waits until all child processes complete.
547 +block_on_close = True
546 548
547 549 def collect_children(self, *, blocking=False):
548 550 """Internal routine to wait for children that have exited."""
@@ -620,7 +622,7 @@ def process_request(self, request, client_address):
620 622
621 623 def server_close(self):
622 624 super().server_close()
623 -self.collect_children(blocking=True)
625 +self.collect_children(blocking=self.block_on_close)
624 626
625 627
626 628 class ThreadingMixIn:
@@ -629,6 +631,8 @@ class ThreadingMixIn:
629 631 # Decides how threads will act upon termination of the
630 632 # main process
631 633 daemon_threads = False
634 +# If true, server_close() waits until all non-daemonic threads terminate.
635 +block_on_close = True
632 636 # For non-daemonic threads, list of threading.Threading objects
633 637 # used by server_close() to wait for all threads completion.
634 638 _threads = None
@@ -659,11 +663,12 @@ def process_request(self, request, client_address):
659 663
660 664 def server_close(self):
661 665 super().server_close()
662 -threads = self._threads
663 -self._threads = None
664 -if threads:
665 -for thread in threads:
666 -thread.join()
666 +if self.block_on_close:
667 +threads = self._threads
668 +self._threads = None
669 +if threads:
670 +for thread in threads:
671 +thread.join()
667 672
668 673
669 674 if hasattr(os, "fork"):
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Add a new ``block_on_close`` class attribute to ``ForkingMixIn`` and
2 +``ThreadingMixIn`` classes of :mod:`socketserver`.