Memory consumption of python ESL
Question: I have written a ESL server, which controls call flow on FreeSWITCH server.
The problem is that after the connection closes, the memory consumption does
not decrease to normal. After some hundred connections are made to the server,
its memory consumption goes to GBs and it has to be killed forcefully. I have
pinpointed the issue to be of ESLconnection object. I have tried deleting the
object and its instances using del, but to no avail. The code is as below:

ivrServer.py

    
    
    import SocketServer
    from ESL import *
    import importlib
    import sys
    import threading
    import traceback
    import signal
    import time
    
    class ESLRequestHandler(SocketServer.BaseRequestHandler):
        #svr_ivr_log = None
        def setup(self):
            fd = self.request.fileno()
            self.con = ESLconnection(fd)
            self.svr_ivr_log.info("Client connected: %s" % str(self.client_address))
    
        def handle(self):
            ivr_script = importlib.import_module('script')
            ivr_script = reload(ivr_script)
            ivr_script.process(self.con)
    
        def finish(self):
            self.con.disconnect()
    
    ESLRequestHandler.svr_ivr_log = logging('server')
    
    SocketServer.ThreadingTCPServer.allow_reuse_address = True
    server = SocketServer.ThreadingTCPServer(('', 9090), ESLRequestHandler)
    server.serve_forever()
    

script.py:

    
    
    def process(con):
        info = con.getInfo()
        uuid = info.getHeader('unique-id')
        con.execute('hangup', "NORMAL_CLEARING", uuid)
    

This would require ESL module of freeswitch compiled for your linux version.
Let me know if I need to explain the procedure to do that. Python2.7 is being
used here.


Answer: From [SocketServer
doc](https://docs.python.org/2/library/socketserver.html?highlight=tcpserver):

> ...A new instance is created for each request.

Each request creates a new `ESLRequestHandler` instance.

In your `setup()`, you make a connection to the FreeSWITCH server, but you
never close it after you finish handling the request. I suspect this is the
source of the memory leak.

To close the connection to the FreeSWITCH server you do something along the
lines of:

    
    
    class ESLRequestHandler(SocketServer.BaseRequestHandler):
        #svr_ivr_log = None
        def setup(self):
            fd = self.request.fileno()
            self.con = ESLconnection(fd)
            self.svr_ivr_log.info("Client connected: %s" % str(self.client_address))
    
        def handle(self):
            pass  # your code for interacting with the FreeSWITCH server here
    
        def finish(self):
            self.con.disconnect()  # close the connection
    

