# Buggy code
def stop_pool(self):
    # lazy import to avoid exception if not using the backend_pool and libvirt not installed (#1185)
    import libvirt

    log.msg(eventid="cowrie.backend_pool.service", format="Trying pool clean stop")

    # stop loop
    if self.loop_next_call:
        self.loop_next_call.cancel()

    # try destroying all guests
    for guest in self.guests:
        self.qemu.destroy_guest(guest["domain"], guest["snapshot"])

    # force destroy remaining stuff
    self.qemu.destroy_all_cowrie()

    try:
        self.qemu.stop_backend()
    except libvirt.libvirtError:
        print("Not connected to Qemu")


# 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 stop_pool(self):
    # lazy import to avoid exception if not using the backend_pool and libvirt not installed (#1185)
    import libvirt

    log.msg(eventid="cowrie.backend_pool.service", format="Trying pool clean stop")

    # stop loop
    if self.loop_next_call:
        self.loop_next_call.cancel()

    # try destroying all guests
    for guest in self.guests:
        self.qemu.destroy_guest(guest["domain"], guest["snapshot"])

    # force destroy remaining stuff
    self.qemu.destroy_all_cowrie()

    # close any NAT sockets
    if not self.local_pool and self.use_nat or self.pool_only:
        log.msg(eventid="cowrie.backend_pool.service", format="Free all NAT bindings")
        self.nat_service.free_all()

    try:
        self.qemu.stop_backend()
    except libvirt.libvirtError:
        print("Not connected to Qemu")