# Buggy code
def run(
    self,
    emitter: StdoutEmitter = None,
    hendrix: bool = True,
    learning: bool = True,
    availability: bool = True,
    worker: bool = True,
    pruning: bool = True,
    interactive: bool = False,
    prometheus: bool = False,
    start_reactor: bool = True,
) -> None:
    """Schedule and start select ursula services, then optionally start the reactor."""

    #
    # Async loops ordered by schedule priority
    #

    if pruning:
        self.__pruning_task = self._arrangement_pruning_task.start(
            interval=self._pruning_interval, now=True
        )

    if learning:
        if emitter:
            emitter.message(
                f"Connecting to {','.join(self.learning_domains)}",
                color="green",
                bold=True,
            )
        self.start_learning_loop(now=self._start_learning_now)

    if self._availability_check and availability:
        self._availability_sensor.start(now=False)  # wait...

    if worker and not self.federated_only:
        self.work_tracker.start(
            act_now=True, requirement_func=self._availability_sensor.status
        )

    #
    # Non-order dependant services
    #

    if prometheus:
        # TODO: Integrate with Hendrix TLS Deploy?
        # Local scoped to help prevent import without prometheus installed
        from nucypher.utilities.metrics import initialize_prometheus_exporter

        initialize_prometheus_exporter(ursula=self, port=self._metrics_port)

    if interactive and emitter:
        stdio.StandardIO(UrsulaCommandProtocol(ursula=self, emitter=emitter))

    if hendrix:
        if emitter:
            emitter.message(
                f"Starting Ursula on {self.rest_interface}", color="green", bold=True
            )

        deployer = self.get_deployer()
        deployer.addServices()
        deployer.catalogServers(deployer.hendrix)

        if not start_reactor:
            return

        if emitter:
            emitter.message("Working ~ Keep Ursula Online!", color="blue", bold=True)

        try:
            deployer.run()  # <--- Blocking Call (Reactor)
        except Exception as e:
            self.log.critical(str(e))
            if emitter:
                emitter.message(f"{e.__class__.__name__} {e}", color="red", bold=True)
            raise  # Crash :-(

    elif start_reactor:  # ... without hendrix
        reactor.run()  # <--- Blocking Call (Reactor)


# Traceback (ValueError)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: Unhandled error in Deferred:
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: Traceback (most recent call last):
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1283, in run
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.mainLoop()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1292, in mainLoop
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.runUntilCurrent()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 913, in runUntilCurrent
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     call.func(*call.args, **call.kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/task.py", line 239, in __call__
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     d = defer.maybeDeferred(self.f, *self.a, **self.kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: --- <exception caught here> ---
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     current.result = callback(current.result, *args, **kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 89, in handle_measurement_errors
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     failure.raiseException()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/python/failure.py", line 488, in raiseException
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     raise self.value.with_traceback(self.tb)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     result = f(*args, **kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 138, in maintain
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.measure()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 191, in measure
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     certificate_filepath=certificate_filepath)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/middleware.py", line 248, in check_rest_availability
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     certificate_filepath=certificate_filepath)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/middleware.py", line 114, in method_wrapper
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     raise ValueError("Don't try to pass a node with a certificate_filepath while also passing a"
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: builtins.ValueError: Don't try to pass a node with a certificate_filepath while also passing a different certificate_filepath.  What do you even expect?
Apr 03 00:02:29 ip-172-31-19-8 nucypher[29582]: Broadcasting CONFIRMACTIVITY Transaction (264369 gwei @ 3000000000)...

# Fixed code
def run(
    self,
    emitter: StdoutEmitter = None,
    hendrix: bool = True,
    learning: bool = True,
    availability: bool = True,
    worker: bool = True,
    pruning: bool = True,
    interactive: bool = False,
    prometheus: bool = False,
    start_reactor: bool = True,
) -> None:
    """Schedule and start select ursula services, then optionally start the reactor."""

    #
    # Async loops ordered by schedule priority
    #

    if emitter:
        emitter.message(f"Starting services...", color="yellow")

    if pruning:
        self.__pruning_task = self._arrangement_pruning_task.start(
            interval=self._pruning_interval, now=True
        )
        if emitter:
            emitter.message(f"✓ Database pruning", color="green")

    if learning:
        self.start_learning_loop(now=self._start_learning_now)
        if emitter:
            emitter.message(
                f"✓ Node Discovery ({','.join(self.learning_domains)})", color="green"
            )

    if self._availability_check and availability:
        self._availability_tracker.start(now=False)  # wait...
        if emitter:
            emitter.message(f"✓ Availability Checks", color="green")

    if worker and not self.federated_only:
        self.work_tracker.start(
            act_now=True, requirement_func=self._availability_tracker.status
        )
        if emitter:
            emitter.message(f"✓ Work Tracking", color="green")

    #
    # Non-order dependant services
    #

    if prometheus:
        # TODO: Integrate with Hendrix TLS Deploy?
        # Local scoped to help prevent import without prometheus installed
        from nucypher.utilities.metrics import initialize_prometheus_exporter

        initialize_prometheus_exporter(ursula=self, port=self._metrics_port)
        if emitter:
            emitter.message(f"✓ Prometheus Exporter", color="green")

    if interactive and emitter:
        stdio.StandardIO(UrsulaCommandProtocol(ursula=self, emitter=emitter))

    if hendrix:
        if emitter:
            emitter.message(
                f"Starting Ursula on {self.rest_interface}", color="green", bold=True
            )

        deployer = self.get_deployer()
        deployer.addServices()
        deployer.catalogServers(deployer.hendrix)

        if not start_reactor:
            return

        if emitter:
            emitter.message("Working ~ Keep Ursula Online!", color="blue", bold=True)

        try:
            deployer.run()  # <--- Blocking Call (Reactor)
        except Exception as e:
            self.log.critical(str(e))
            if emitter:
                emitter.message(f"{e.__class__.__name__} {e}", color="red", bold=True)
            raise  # Crash :-(

    elif start_reactor:  # ... without hendrix
        reactor.run()  # <--- Blocking Call (Reactor)