# Buggy code
def __init__(self):
    self.qemu = backend_pool.libvirt.backend_service.LibvirtBackendService()
    self.guests = []
    self.guest_id = 0
    self.guest_lock = Lock()

    # time in seconds between each loop iteration
    self.loop_sleep_time = 5
    self.loop_next_call = None

    # default configs; custom values will come from the client when they connect to the pool
    self.max_vm = 2
    self.vm_unused_timeout = 600
    self.share_guests = True

    # file configs
    self.ssh_port = CowrieConfig().getint("backend_pool", "guest_ssh_port", fallback=-1)
    self.telnet_port = CowrieConfig().getint(
        "backend_pool", "guest_telnet_port", fallback=-1
    )

    # detect invalid config
    if not self.ssh_port > 0 and not self.telnet_port > 0:
        log.msg(
            eventid="cowrie.backend_pool.service",
            format="Invalid configuration: one of SSH or Telnet ports must be defined!",
        )
        os._exit(1)

    self.any_vm_up = False  # TODO fix for no VM available


# 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 __init__(self, nat_service):
    self.qemu = backend_pool.libvirt.backend_service.LibvirtBackendService()
    self.nat_service = nat_service

    self.guests = []
    self.guest_id = 0
    self.guest_lock = Lock()

    # time in seconds between each loop iteration
    self.loop_sleep_time = 5
    self.loop_next_call = None

    # default configs; custom values will come from the client when they connect to the pool
    self.max_vm = 2
    self.vm_unused_timeout = 600
    self.share_guests = True

    # file configs
    self.ssh_port = CowrieConfig().getint("backend_pool", "guest_ssh_port", fallback=-1)
    self.telnet_port = CowrieConfig().getint(
        "backend_pool", "guest_telnet_port", fallback=-1
    )

    self.local_pool = CowrieConfig().get("proxy", "pool", fallback="local") == "local"
    self.pool_only = CowrieConfig().getboolean(
        "backend_pool", "pool_only", fallback=False
    )
    self.use_nat = CowrieConfig().getboolean("backend_pool", "use_nat", fallback=True)

    # detect invalid config
    if not self.ssh_port > 0 and not self.telnet_port > 0:
        log.msg(
            eventid="cowrie.backend_pool.service",
            format="Invalid configuration: one of SSH or Telnet ports must be defined!",
        )
        os._exit(1)

    self.any_vm_up = False  # TODO fix for no VM available