# Buggy code
def run(self) -> None:
    self._create_pid_file()
    module_logger.info(
        "=== Starting... (loop runs every %ds) Hit ^C to stop", self.interval
    )
    loop = True
    loops = self._max_loops
    heartbeat = True
    while loop:
        try:
            if loops > 0:
                loops -= 1
                if loops == 0:
                    module_logger.warning(
                        "Ran out of loop counter, will stop after this one"
                    )
                    loop = False
            if self._need_hup or self._check_hup_file():
                try:
                    module_logger.warning("Reloading configuration")
                    self._load_config()
                    self.hup_loggers()
                    self._need_hup = False
                except Exception:
                    module_logger.exception("Error while reloading configuration")
                    sys.exit(1)
            self.run_loop()

            if module_logger.level in ["error", "critical", "warn"] and self.heartbeat:
                heartbeat = not heartbeat
                if heartbeat:
                    sys.stdout.write(".")
                    sys.stdout.flush()
        except KeyboardInterrupt:
            module_logger.info("Received ^C")
            loop = False
        except Exception:
            module_logger.exception("Caught unhandled exception during main loop")
        if loop and self._network:
            if (
                self._remote_listening_thread
                and not self._remote_listening_thread.is_alive()
            ):
                module_logger.error("Listener thread died :(")
                self._start_network_thread()
        if self.one_shot:
            break

        try:
            if loop:
                time.sleep(self.interval)
        except Exception:
            module_logger.info("Quitting")
            loop = False

    self._remove_pid_file()


# Traceback (OSError)
% simplemonitor -t
[...]
2020-09-10 10:12:46     INFO (simplemonitor) Starting remote listener thread (allowing pickle data)
Traceback (most recent call last):
File "/root/.local/share/virtualenvs/sm-pip-9yYbf1A2/bin/simplemonitor", line 11, in <module>
load_entry_point('simplemonitor', 'console_scripts', 'simplemonitor')()
File "/home/james/src/simplemonitor/simplemonitor/monitor.py", line 203, in main
one_shot=options.one_shot,
File "/home/james/src/simplemonitor/simplemonitor/simplemonitor.py", line 73, in __init__
self._load_config()
File "/home/james/src/simplemonitor/simplemonitor/simplemonitor.py", line 120, in _load_config
self._start_network_thread()
File "/home/james/src/simplemonitor/simplemonitor/simplemonitor.py", line 139, in _start_network_thread
bind_host=self._network_bind_host,
File "/home/james/src/simplemonitor/simplemonitor/Loggers/network.py", line 133, in __init__
self.sock.bind((bind_host, port))
OSError: [Errno 98] Address already in use

# Fixed code
def run(self) -> None:
    self._create_pid_file()
    self._start_network_thread()
    module_logger.info(
        "=== Starting... (loop runs every %ds) Hit ^C to stop", self.interval
    )
    loop = True
    loops = self._max_loops
    heartbeat = True
    while loop:
        try:
            if loops > 0:
                loops -= 1
                if loops == 0:
                    module_logger.warning(
                        "Ran out of loop counter, will stop after this one"
                    )
                    loop = False
            if self._need_hup or self._check_hup_file():
                try:
                    module_logger.warning("Reloading configuration")
                    self._load_config()
                    self._start_network_thread()
                    self.hup_loggers()
                    self._need_hup = False
                except Exception:
                    module_logger.exception("Error while reloading configuration")
                    sys.exit(1)
            self.run_loop()

            if module_logger.level in ["error", "critical", "warn"] and self.heartbeat:
                heartbeat = not heartbeat
                if heartbeat:
                    sys.stdout.write(".")
                    sys.stdout.flush()
        except KeyboardInterrupt:
            module_logger.info("Received ^C")
            loop = False
        except Exception:
            module_logger.exception("Caught unhandled exception during main loop")
        if loop and self._network:
            if (
                self._remote_listening_thread
                and not self._remote_listening_thread.is_alive()
            ):
                module_logger.error("Listener thread died :(")
                self._start_network_thread()
        if self.one_shot:
            break

        try:
            if loop:
                time.sleep(self.interval)
        except Exception:
            module_logger.info("Quitting")
            loop = False

    self._remove_pid_file()