The following script is modified slightly from:

http://docs.python.org/library/socketserver.html

to make it run "forever". However, a memory leak occurs and eventually we see:

Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space
Exception in thread Thread:Traceback (most recent call last):
Exception in thread "MainThread" java.lang.OutOfMemoryError: Java heap space
  File "C:\jython2.5.2b2\Lib\threading.py", line 179, in _Thread__bootstrap

Can this be modified such that no memory leak occurs? Is this potentially a bug in Jython?

I am running 2.5.2b2, however this behaviour is observed on 2.5.1 also. Running Sun JDK on Win32 (XP).

Regards,

Gareth

---------------

import socket
import threading
import SocketServer
import time

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.currentThread()
        response = "%s: %s" % (cur_thread.getName(), data)
        self.request.send(response)

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    sock.send(message)
    response = sock.recv(1024)
    print "Received: %s" % response
    sock.close()

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost",  0

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.setDaemon(True)
    server_thread.start()
    print "Server loop running in thread:", server_thread.getName()

    i = 0
    while True:
        i += 1
        client(ip, port, "Hello World %i" % i)
        time.sleep(0.2)