New submission from Norihiro Maruyama <norihiro.maruy...@gmail.com>:
UDP/TCPServer with socketserver.ThreadingMixin class (also ThreadingTCPServer and ThreadingUDPServer class) seems to be memory leak while running the server. https://docs.python.org/3/library/socketserver.html#socketserver.ThreadingMixIn My code which wrote to check this is the following. ``` class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): def handle(self): data = str(self.request.recv(1024), 'ascii') cur_thread = threading.current_thread() response = bytes("{}: {}".format(cur_thread.name, data), 'ascii') self.request.sendall(response) if __name__ == "__main__": HOST, PORT = "localhost", 0 server = socketserver.ThreadingTCPServer((HOST, PORT), ThreadedTCPRequestHandler) server.daemon_threads = False server.block_on_close = True with server: ip, port = server.server_address server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() for i in range(1000): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(server.server_address) sock.sendall(bytes("hello", 'ascii')) response = str(sock.recv(1024), 'ascii') print("Received: {}".format(response)) time.sleep(0.01) server.server_close() server.shutdown() ``` ( I wrote this based on https://docs.python.org/3/library/socketserver.html#asynchronous-mixins) Then I checked memory usage with profiling tool. (I used memory-profiler module https://pypi.org/project/memory-profiler/) $ mprof run python mycode.py $ mprof plot I attached result plot. And also I checked this also more long time and I found memory usage was increased endlessly. My environment is Hardware: MacBook Pro (15-inch, 2018) OS: MacOS 10.14 Python 3.7.1 I guess it caused by a thread object is not released in spite of the thread finished to process request and thread object will be made infinitely until server_close() is called. ---------- components: Library (Lib) files: threadingmixin_memory_usage.png messages: 344926 nosy: maru-n priority: normal severity: normal status: open title: Memory leak while running TCP/UDPServer with socketserver.ThreadingMixIn type: resource usage versions: Python 3.7 Added file: https://bugs.python.org/file48399/threadingmixin_memory_usage.png _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37193> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com