# Buggy code
def stop(self, error=False):
    """Stops this trial.

    Stops this trial, releasing all allocating resources. If stopping the
    trial fails, the run will be marked as terminated in error, but no
    exception will be thrown.

    Args:
        error (bool): Whether to mark this trial as terminated in error.
    """

    if error:
        self.status = Trial.ERROR
    else:
        self.status = Trial.TERMINATED

    try:
        if self.agent:
            self.agent.stop.remote()
            self.agent.__ray_terminate__.remote(self.agent._ray_actor_id.id())
    except Exception:
        print("Error stopping agent:", traceback.format_exc())
        self.status = Trial.ERROR
    finally:
        self.agent = None


# Traceback (Exception)
======================================================================
ERROR: testPauseThenResume (__main__.TrialRunnerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test/trial_runner_test.py", line 248, in testPauseThenResume
trials[0].resume()
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/tune/trial.py", line 120, in resume
self.start()
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/tune/trial.py", line 76, in start
self._setup_agent()
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/tune/trial.py", line 213, in _setup_agent
experiment_tag=self.experiment_tag)
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/actor.py", line 717, in remote
ray.worker.global_worker)
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/actor.py", line 381, in export_actor
num_gpus, worker.redis_client)
File "/home/travis/.local/lib/python3.6/site-packages/ray-0.2.1-py3.6-linux-x86_64.egg/ray/utils.py", line 229, in select_local_scheduler
"information is {}.".format(local_schedulers))
Exception: Could not find a node with enough GPUs or other resources to create this actor. The local scheduler information is [ {'ClientType': 'local_scheduler', 'Deleted': False, 'DBClientID': '9557d3251486fc299505385ce6ddeddef27ad895', 'AuxAddress': '127.0.0.1:38753', 'NumCPUs': 1.0, 'NumGPUs': 1.0, 'LocalSchedulerSocketName': '/tmp/scheduler14716168'}].

# Fixed code
def stop(self, error=False):
    """Stops this trial.

    Stops this trial, releasing all allocating resources. If stopping the
    trial fails, the run will be marked as terminated in error, but no
    exception will be thrown.

    Args:
        error (bool): Whether to mark this trial as terminated in error.
    """

    if error:
        self.status = Trial.ERROR
    else:
        self.status = Trial.TERMINATED

    try:
        if self.agent:
            stop_tasks = []
            stop_tasks.append(self.agent.stop.remote())
            stop_tasks.append(
                self.agent.__ray_terminate__.remote(self.agent._ray_actor_id.id())
            )
            _, unfinished = ray.wait(stop_tasks, num_returns=2, timeout=10000)
            if unfinished:
                print(("Stopping %s Actor was unsuccessful, but moving on...") % self)
    except Exception:
        print("Error stopping agent:", traceback.format_exc())
        self.status = Trial.ERROR
    finally:
        self.agent = None