Skip to content

Commit 453bd0b

Browse files
authored
bpo-33540: Add block_on_close attr to socketserver (GH-6911)
Add a new block_on_close class attribute to ForkingMixIn and ThreadingMixIn classes of socketserver to opt-in for pre-3.7 behaviour.
1 parent b97de3d commit 453bd0b

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

Doc/library/socketserver.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ server classes.
116116
only available on POSIX platforms that support :func:`~os.fork`.
117117

118118
: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.
120121

121122
: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
123126
:data:`ThreadingMixIn.daemon_threads` to ``True`` to not wait until threads
124127
complete.
125128

@@ -128,6 +131,8 @@ server classes.
128131
:meth:`socketserver.ForkingMixIn.server_close` and
129132
:meth:`socketserver.ThreadingMixIn.server_close` now waits until all
130133
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.
131136

132137

133138
.. class:: ForkingTCPServer

Doc/whatsnew/3.7.rst

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,18 @@ by default.
12161216
(Contributed by Christian Heimes in :issue:`28134`.)
12171217

12181218

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+
12191231
sqlite3
12201232
-------
12211233

@@ -2156,10 +2168,17 @@ Changes in the Python API
21562168
and module are affected by this change. (Contributed by INADA Naoki and
21572169
Eugene Toder in :issue:`29463`.)
21582170

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`.)
21632182

21642183
* The :func:`locale.localeconv` function now temporarily sets the ``LC_CTYPE``
21652184
locale to the value of ``LC_NUMERIC`` in some cases.

Lib/socketserver.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ class ForkingMixIn:
543543
timeout = 300
544544
active_children = None
545545
max_children = 40
546+
# If true, server_close() waits until all child processes complete.
547+
block_on_close = True
546548

547549
def collect_children(self, *, blocking=False):
548550
"""Internal routine to wait for children that have exited."""
@@ -620,7 +622,7 @@ def process_request(self, request, client_address):
620622

621623
def server_close(self):
622624
super().server_close()
623-
self.collect_children(blocking=True)
625+
self.collect_children(blocking=self.block_on_close)
624626

625627

626628
class ThreadingMixIn:
@@ -629,6 +631,8 @@ class ThreadingMixIn:
629631
# Decides how threads will act upon termination of the
630632
# main process
631633
daemon_threads = False
634+
# If true, server_close() waits until all non-daemonic threads terminate.
635+
block_on_close = True
632636
# For non-daemonic threads, list of threading.Threading objects
633637
# used by server_close() to wait for all threads completion.
634638
_threads = None
@@ -659,11 +663,12 @@ def process_request(self, request, client_address):
659663

660664
def server_close(self):
661665
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()
667672

668673

669674
if hasattr(os, "fork"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new ``block_on_close`` class attribute to ``ForkingMixIn`` and
2+
``ThreadingMixIn`` classes of :mod:`socketserver`.

0 commit comments

Comments
 (0)