diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-08-10 15:28:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-10 15:28:16 +0200 |
commit | aa8ec34ad52bb3b274ce91169e1bc4a598655049 (patch) | |
tree | 4c1a82ebac934277dcdec6ba16b62c858df1b539 /Lib/socketserver.py | |
parent | 6c8c2943d996b59a48d331f61f22cbe72933910e (diff) | |
download | cpython-git-aa8ec34ad52bb3b274ce91169e1bc4a598655049.tar.gz |
bpo-31151: Add socketserver.ForkingMixIn.server_close() (#3057)
* Add socketserver.ForkingMixIn.server_close()
bpo-31151: socketserver.ForkingMixIn.server_close() now waits until
all child processes completed to prevent leaking zombie processes.
* Fix test on Windows which doesn't have ForkingMixIn
Diffstat (limited to 'Lib/socketserver.py')
-rw-r--r-- | Lib/socketserver.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/socketserver.py b/Lib/socketserver.py index 6e1ae9fddd..df17114950 100644 --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -547,7 +547,7 @@ if hasattr(os, "fork"): active_children = None max_children = 40 - def collect_children(self): + def collect_children(self, *, blocking=False): """Internal routine to wait for children that have exited.""" if self.active_children is None: return @@ -571,7 +571,8 @@ if hasattr(os, "fork"): # Now reap all defunct children. for pid in self.active_children.copy(): try: - pid, _ = os.waitpid(pid, os.WNOHANG) + flags = 0 if blocking else os.WNOHANG + pid, _ = os.waitpid(pid, flags) # if the child hasn't exited yet, pid will be 0 and ignored by # discard() below self.active_children.discard(pid) @@ -620,6 +621,10 @@ if hasattr(os, "fork"): finally: os._exit(status) + def server_close(self): + super().server_close() + self.collect_children(blocking=True) + class ThreadingMixIn: """Mix-in class to handle each request in a new thread.""" |