# Buggy code
def request_binding(self, guest_id, dst_ip, ssh_port, telnet_port):
    self.lock.acquire()
    try:
        # see if binding is already created
        if dst_ip in self.bindings:
            # increase connected
            self.bindings[guest_id][0] += 1

            return self.bindings[guest_id][1]._realPortNumber, self.bindings[guest_id][
                2
            ]._realPortNumber

        else:
            nat_ssh = reactor.listenTCP(
                0, ServerFactory(dst_ip, ssh_port), interface="0.0.0.0"
            )
            nat_telnet = reactor.listenTCP(
                0, ServerFactory(dst_ip, telnet_port), interface="0.0.0.0"
            )
            self.bindings[guest_id] = [0, nat_ssh, nat_telnet]

            return nat_ssh._realPortNumber, nat_telnet._realPortNumber
    finally:
        self.lock.release()


# Traceback (builtins.OSError)
2020-06-08T14:16:22.505410Z [backend_pool.nat.ServerFactory#info] Starting factory <backend_pool.nat.ServerFactory object at 0x7fc035e2ce80>
2020-06-08T14:16:22.507706Z [backend_pool.nat.ClientFactory#info] Starting factory <backend_pool.nat.ClientFactory object at 0x7fc035e2c3c8>
2020-06-08T14:16:22.508111Z [twisted.internet.tcp.Port#info] EMFILE encountered; releasing reserved file descriptor.
2020-06-08T14:16:22.508193Z [twisted.internet.tcp.Port#info] Re-reserving EMFILE recovery file descriptor.
2020-06-08T14:16:22.508593Z [backend_pool.nat.ClientFactory#info] Stopping factory <backend_pool.nat.ClientFactory object at 0x7fc035e2c3c8>
2020-06-08T14:16:23.235361Z [twisted.internet.tcp.Port#info] EMFILE encountered; releasing reserved file descriptor.
2020-06-08T14:16:23.235508Z [twisted.internet.tcp.Port#info] EMFILE recovery: Closed socket from ('172.16.1.6', 42942)
2020-06-08T14:16:23.235537Z [twisted.internet.tcp.Port#info] Re-reserving EMFILE recovery file descriptor.
2020-06-08T14:16:24.463433Z [-] Unhandled Error
Traceback (most recent call last):
File "/srv/cowrie/cowrie-venv/lib/python3.6/site-packages/twisted/application/app.py", line 399, in startReactor

File "/srv/cowrie/cowrie-venv/lib/python3.6/site-packages/twisted/application/app.py", line 312, in runReactorWithLogging

File "/srv/cowrie/cowrie-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1283, in run

File "/srv/cowrie/cowrie-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1292, in mainLoop

--- <exception caught here> ---
File "/srv/cowrie/cowrie-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 913, in runUntilCurrent

File "/srv/cowrie/cowrie-github/src/backend_pool/pool_service.py", line 280, in producer_loop

File "/srv/cowrie/cowrie-github/src/backend_pool/pool_service.py", line 190, in __producer_check_health

File "/srv/cowrie/cowrie-github/src/backend_pool/pool_service.py", line 151, in has_connectivity

File "/srv/cowrie/cowrie-github/src/backend_pool/util.py", line 18, in nmap_port

File "/usr/lib/python3.6/subprocess.py", line 423, in run

File "/usr/lib/python3.6/subprocess.py", line 687, in __init__

File "/usr/lib/python3.6/subprocess.py", line 1197, in _get_handles

builtins.OSError: [Errno 24] Too many open files

# Fixed code
def request_binding(self, guest_id, dst_ip, ssh_port, telnet_port):
    self.lock.acquire()
    try:
        # see if binding is already created
        if guest_id in self.bindings:
            # increase connected
            self.bindings[guest_id][0] += 1

            return self.bindings[guest_id][1]._realPortNumber, self.bindings[guest_id][
                2
            ]._realPortNumber
        else:
            nat_ssh = reactor.listenTCP(
                0, ServerFactory(dst_ip, ssh_port), interface="0.0.0.0"
            )
            nat_telnet = reactor.listenTCP(
                0, ServerFactory(dst_ip, telnet_port), interface="0.0.0.0"
            )
            self.bindings[guest_id] = [1, nat_ssh, nat_telnet]

            return nat_ssh._realPortNumber, nat_telnet._realPortNumber
    finally:
        self.lock.release()