diff options
author | MARUYAMA Norihiro <norihiro.maruyama@gmail.com> | 2020-11-01 23:51:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-01 23:51:04 (GMT) |
commit | c41559021213cfc9dc62a83fc63306b3bdc3e64b (patch) | |
tree | 2a8974f08a5fa31aa57d336b29f01dddd5df164c /Lib/test/test_socketserver.py | |
parent | e662c398d87f136497f8ec672e83657ae3a599e0 (diff) | |
download | cpython-c41559021213cfc9dc62a83fc63306b3bdc3e64b.zip cpython-c41559021213cfc9dc62a83fc63306b3bdc3e64b.tar.gz cpython-c41559021213cfc9dc62a83fc63306b3bdc3e64b.tar.bz2 |
bpo-37193: remove thread objects which finished process its request (GH-13893)
* bpo-37193: remove the thread which finished process request from threads list
* rename variable t to thread.
* don't remove thread from list if it is daemon.
* use lock to protect self._threads.
* use finally block in case of exception from shutdown_request().
* check "not thread.daemon" before lock to avoid holding the lock if it's unnecessary.
* fix the place of _threads_lock.
* separate code to remove a current thread into a function.
* check ValueError when removing thread.
* fix wrong code which all instance shared same lock.
* Extract thread management into a _Threads class to encapsulate atomic operations and separate concerns.
* Replace multiple references of 'block_on_close' with one, avoiding the possibility that 'block_on_close' could change during the course of processing requests. Now, there's exactly one _threads object with behavior fixed for the duration.
* Add docstrings to private classes.
* Add test to ensure that a ThreadingTCPServer can be closed without serving any requests.
* Use _NoThreads as the default value. Fixes AttributeError when server is closed without serving any requests.
* Add blurb
* Add test capturing failure.
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Diffstat (limited to 'Lib/test/test_socketserver.py')
-rw-r--r-- | Lib/test/test_socketserver.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py index 7cdd115..1944795f 100644 --- a/Lib/test/test_socketserver.py +++ b/Lib/test/test_socketserver.py @@ -277,6 +277,13 @@ class SocketServerTest(unittest.TestCase): t.join() s.server_close() + def test_close_immediately(self): + class MyServer(socketserver.ThreadingMixIn, socketserver.TCPServer): + pass + + server = MyServer((HOST, 0), lambda: None) + server.server_close() + def test_tcpserver_bind_leak(self): # Issue #22435: the server socket wouldn't be closed if bind()/listen() # failed. @@ -491,6 +498,23 @@ class MiscTestCase(unittest.TestCase): self.assertEqual(server.shutdown_called, 1) server.server_close() + def test_threads_reaped(self): + """ + In #37193, users reported a memory leak + due to the saving of every request thread. Ensure that the + threads are cleaned up after the requests complete. + """ + class MyServer(socketserver.ThreadingMixIn, socketserver.TCPServer): + pass + + server = MyServer((HOST, 0), socketserver.StreamRequestHandler) + for n in range(10): + with socket.create_connection(server.server_address): + server.handle_request() + [thread.join() for thread in server._threads] + self.assertEqual(len(server._threads), 0) + server.server_close() + if __name__ == "__main__": unittest.main() |