prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Backport #1554
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/garage/tf/plotter/plotter.py`
  Content:
  ```
  1 import atexit
  2 from collections import namedtuple
  3 from enum import Enum
  4 import platform
  5 from queue import Queue
  6 from threading import Thread
  7 
  8 import numpy as np
  9 import tensorflow as tf
  10 
  11 from garage.sampler.utils import rollout as default_rollout
  12 
  13 __all__ = ['Plotter']
  14 
  15 
  16 class Op(Enum):
  17     STOP = 0
  18     UPDATE = 1
  19     DEMO = 2
  20 
  21 
  22 Message = namedtuple('Message', ['op', 'args', 'kwargs'])
  23 
  24 
  25 class Plotter:
  26 
  27     # Static variable used to disable the plotter
  28     enable = True
  29     # List containing all plotters instantiated in the process
  30     __plotters = []
  31 
  32     def __init__(self,
  33                  env,
  34                  policy,
  35                  sess=None,
  36                  graph=None,
  37                  rollout=default_rollout):
  38         Plotter.__plotters.append(self)
  39         self.env = env
  40         self.sess = tf.compat.v1.get_default_session(
  41         ) if sess is None else sess
  42         self.graph = tf.compat.v1.get_default_graph(
  43         ) if graph is None else graph
  44         with self.sess.as_default(), self.graph.as_default():
  45             self.policy = policy.clone('plotter_policy')
  46         self.rollout = rollout
  47         self.worker_thread = Thread(target=self._start_worker, daemon=True)
  48         self.queue = Queue()
  49 
  50         # Needed in order to draw glfw window on the main thread
  51         if ('Darwin' in platform.platform()):
  52             self.rollout(env,
  53                          policy,
  54                          max_path_length=np.inf,
  55                          animated=True,
  56                          speedup=5)
  57 
  58     def _start_worker(self):
  59         env = None
  60         policy = None
  61         max_length = None
  62         initial_rollout = True
  63         try:
  64             with self.sess.as_default(), self.sess.graph.as_default():
  65                 # Each iteration will process ALL messages currently in the
  66                 # queue
  67                 while True:
  68                     msgs = {}
  69                     # If true, block and yield processor
  70                     if initial_rollout:
  71                         msg = self.queue.get()
  72                         msgs[msg.op] = msg
  73                         # Only fetch the last message of each type
  74                         while not self.queue.empty():
  75                             msg = self.queue.get()
  76                             msgs[msg.op] = msg
  77                     else:
  78                         # Only fetch the last message of each type
  79                         while not self.queue.empty():
  80                             msg = self.queue.get_nowait()
  81                             msgs[msg.op] = msg
  82 
  83                     if Op.STOP in msgs:
  84                         self.queue.task_done()
  85                         break
  86                     if Op.UPDATE in msgs:
  87                         env, policy = msgs[Op.UPDATE].args
  88                         self.queue.task_done()
  89                     if Op.DEMO in msgs:
  90                         param_values, max_length = msgs[Op.DEMO].args
  91                         policy.set_param_values(param_values)
  92                         initial_rollout = False
  93                         self.rollout(env,
  94                                      policy,
  95                                      max_path_length=max_length,
  96                                      animated=True,
  97                                      speedup=5)
  98                         self.queue.task_done()
  99                     else:
  100                         if max_length:
  101                             self.rollout(env,
  102                                          policy,
  103                                          max_path_length=max_length,
  104                                          animated=True,
  105                                          speedup=5)
  106         except KeyboardInterrupt:
  107             pass
  108 
  109     def close(self):
  110         if self.worker_thread.is_alive():
  111             while not self.queue.empty():
  112                 self.queue.get()
  113                 self.queue.task_done()
  114             self.queue.put(Message(op=Op.STOP, args=None, kwargs=None))
  115             self.queue.join()
  116             self.worker_thread.join()
  117 
  118     @staticmethod
  119     def disable():
  120         """Disable all instances of the Plotter class."""
  121         Plotter.enable = False
  122 
  123     @staticmethod
  124     def get_plotters():
  125         return Plotter.__plotters
  126 
  127     def start(self):
  128         if not Plotter.enable:
  129             return
  130         if not self.worker_thread.is_alive():
  131             tf.compat.v1.get_variable_scope().reuse_variables()
  132             self.worker_thread.start()
  133             self.queue.put(
  134                 Message(op=Op.UPDATE,
  135                         args=(self.env, self.policy),
  136                         kwargs=None))
  137             atexit.register(self.close)
  138 
  139     def update_plot(self, policy, max_length=np.inf):
  140         if not Plotter.enable:
  141             return
  142         if self.worker_thread.is_alive():
  143             self.queue.put(
  144                 Message(op=Op.DEMO,
  145                         args=(policy.get_param_values(), max_length),
  146                         kwargs=None))
  147
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/garage/tf/plotter/plotter.py b/src/garage/tf/plotter/plotter.py
  --- a/src/garage/tf/plotter/plotter.py
  +++ b/src/garage/tf/plotter/plotter.py
  @@ -43,6 +43,7 @@
           ) if graph is None else graph
           with self.sess.as_default(), self.graph.as_default():
               self.policy = policy.clone('plotter_policy')
  +            self.policy.build(policy.model.input)
           self.rollout = rollout
           self.worker_thread = Thread(target=self._start_worker, daemon=True)
           self.queue = Queue()
  
verification_info: {"golden_diff": "diff --git a/src/garage/tf/plotter/plotter.py b/src/garage/tf/plotter/plotter.py\n--- a/src/garage/tf/plotter/plotter.py\n+++ b/src/garage/tf/plotter/plotter.py\n@@ -43,6 +43,7 @@\n         ) if graph is None else graph\n         with self.sess.as_default(), self.graph.as_default():\n             self.policy = policy.clone('plotter_policy')\n+            self.policy.build(policy.model.input)\n         self.rollout = rollout\n         self.worker_thread = Thread(target=self._start_worker, daemon=True)\n         self.queue = Queue()\n", "issue": "Backport #1554\n\n", "before_files": [{"content": "import atexit\nfrom collections import namedtuple\nfrom enum import Enum\nimport platform\nfrom queue import Queue\nfrom threading import Thread\n\nimport numpy as np\nimport tensorflow as tf\n\nfrom garage.sampler.utils import rollout as default_rollout\n\n__all__ = ['Plotter']\n\n\nclass Op(Enum):\n    STOP = 0\n    UPDATE = 1\n    DEMO = 2\n\n\nMessage = namedtuple('Message', ['op', 'args', 'kwargs'])\n\n\nclass Plotter:\n\n    # Static variable used to disable the plotter\n    enable = True\n    # List containing all plotters instantiated in the process\n    __plotters = []\n\n    def __init__(self,\n                 env,\n                 policy,\n                 sess=None,\n                 graph=None,\n                 rollout=default_rollout):\n        Plotter.__plotters.append(self)\n        self.env = env\n        self.sess = tf.compat.v1.get_default_session(\n        ) if sess is None else sess\n        self.graph = tf.compat.v1.get_default_graph(\n        ) if graph is None else graph\n        with self.sess.as_default(), self.graph.as_default():\n            self.policy = policy.clone('plotter_policy')\n        self.rollout = rollout\n        self.worker_thread = Thread(target=self._start_worker, daemon=True)\n        self.queue = Queue()\n\n        # Needed in order to draw glfw window on the main thread\n        if ('Darwin' in platform.platform()):\n            self.rollout(env,\n                         policy,\n                         max_path_length=np.inf,\n                         animated=True,\n                         speedup=5)\n\n    def _start_worker(self):\n        env = None\n        policy = None\n        max_length = None\n        initial_rollout = True\n        try:\n            with self.sess.as_default(), self.sess.graph.as_default():\n                # Each iteration will process ALL messages currently in the\n                # queue\n                while True:\n                    msgs = {}\n                    # If true, block and yield processor\n                    if initial_rollout:\n                        msg = self.queue.get()\n                        msgs[msg.op] = msg\n                        # Only fetch the last message of each type\n                        while not self.queue.empty():\n                            msg = self.queue.get()\n                            msgs[msg.op] = msg\n                    else:\n                        # Only fetch the last message of each type\n                        while not self.queue.empty():\n                            msg = self.queue.get_nowait()\n                            msgs[msg.op] = msg\n\n                    if Op.STOP in msgs:\n                        self.queue.task_done()\n                        break\n                    if Op.UPDATE in msgs:\n                        env, policy = msgs[Op.UPDATE].args\n                        self.queue.task_done()\n                    if Op.DEMO in msgs:\n                        param_values, max_length = msgs[Op.DEMO].args\n                        policy.set_param_values(param_values)\n                        initial_rollout = False\n                        self.rollout(env,\n                                     policy,\n                                     max_path_length=max_length,\n                                     animated=True,\n                                     speedup=5)\n                        self.queue.task_done()\n                    else:\n                        if max_length:\n                            self.rollout(env,\n                                         policy,\n                                         max_path_length=max_length,\n                                         animated=True,\n                                         speedup=5)\n        except KeyboardInterrupt:\n            pass\n\n    def close(self):\n        if self.worker_thread.is_alive():\n            while not self.queue.empty():\n                self.queue.get()\n                self.queue.task_done()\n            self.queue.put(Message(op=Op.STOP, args=None, kwargs=None))\n            self.queue.join()\n            self.worker_thread.join()\n\n    @staticmethod\n    def disable():\n        \"\"\"Disable all instances of the Plotter class.\"\"\"\n        Plotter.enable = False\n\n    @staticmethod\n    def get_plotters():\n        return Plotter.__plotters\n\n    def start(self):\n        if not Plotter.enable:\n            return\n        if not self.worker_thread.is_alive():\n            tf.compat.v1.get_variable_scope().reuse_variables()\n            self.worker_thread.start()\n            self.queue.put(\n                Message(op=Op.UPDATE,\n                        args=(self.env, self.policy),\n                        kwargs=None))\n            atexit.register(self.close)\n\n    def update_plot(self, policy, max_length=np.inf):\n        if not Plotter.enable:\n            return\n        if self.worker_thread.is_alive():\n            self.queue.put(\n                Message(op=Op.DEMO,\n                        args=(policy.get_param_values(), max_length),\n                        kwargs=None))\n", "path": "src/garage/tf/plotter/plotter.py"}], "after_files": [{"content": "import atexit\nfrom collections import namedtuple\nfrom enum import Enum\nimport platform\nfrom queue import Queue\nfrom threading import Thread\n\nimport numpy as np\nimport tensorflow as tf\n\nfrom garage.sampler.utils import rollout as default_rollout\n\n__all__ = ['Plotter']\n\n\nclass Op(Enum):\n    STOP = 0\n    UPDATE = 1\n    DEMO = 2\n\n\nMessage = namedtuple('Message', ['op', 'args', 'kwargs'])\n\n\nclass Plotter:\n\n    # Static variable used to disable the plotter\n    enable = True\n    # List containing all plotters instantiated in the process\n    __plotters = []\n\n    def __init__(self,\n                 env,\n                 policy,\n                 sess=None,\n                 graph=None,\n                 rollout=default_rollout):\n        Plotter.__plotters.append(self)\n        self.env = env\n        self.sess = tf.compat.v1.get_default_session(\n        ) if sess is None else sess\n        self.graph = tf.compat.v1.get_default_graph(\n        ) if graph is None else graph\n        with self.sess.as_default(), self.graph.as_default():\n            self.policy = policy.clone('plotter_policy')\n            self.policy.build(policy.model.input)\n        self.rollout = rollout\n        self.worker_thread = Thread(target=self._start_worker, daemon=True)\n        self.queue = Queue()\n\n        # Needed in order to draw glfw window on the main thread\n        if ('Darwin' in platform.platform()):\n            self.rollout(env,\n                         policy,\n                         max_path_length=np.inf,\n                         animated=True,\n                         speedup=5)\n\n    def _start_worker(self):\n        env = None\n        policy = None\n        max_length = None\n        initial_rollout = True\n        try:\n            with self.sess.as_default(), self.sess.graph.as_default():\n                # Each iteration will process ALL messages currently in the\n                # queue\n                while True:\n                    msgs = {}\n                    # If true, block and yield processor\n                    if initial_rollout:\n                        msg = self.queue.get()\n                        msgs[msg.op] = msg\n                        # Only fetch the last message of each type\n                        while not self.queue.empty():\n                            msg = self.queue.get()\n                            msgs[msg.op] = msg\n                    else:\n                        # Only fetch the last message of each type\n                        while not self.queue.empty():\n                            msg = self.queue.get_nowait()\n                            msgs[msg.op] = msg\n\n                    if Op.STOP in msgs:\n                        self.queue.task_done()\n                        break\n                    if Op.UPDATE in msgs:\n                        env, policy = msgs[Op.UPDATE].args\n                        self.queue.task_done()\n                    if Op.DEMO in msgs:\n                        param_values, max_length = msgs[Op.DEMO].args\n                        policy.set_param_values(param_values)\n                        initial_rollout = False\n                        self.rollout(env,\n                                     policy,\n                                     max_path_length=max_length,\n                                     animated=True,\n                                     speedup=5)\n                        self.queue.task_done()\n                    else:\n                        if max_length:\n                            self.rollout(env,\n                                         policy,\n                                         max_path_length=max_length,\n                                         animated=True,\n                                         speedup=5)\n        except KeyboardInterrupt:\n            pass\n\n    def close(self):\n        if self.worker_thread.is_alive():\n            while not self.queue.empty():\n                self.queue.get()\n                self.queue.task_done()\n            self.queue.put(Message(op=Op.STOP, args=None, kwargs=None))\n            self.queue.join()\n            self.worker_thread.join()\n\n    @staticmethod\n    def disable():\n        \"\"\"Disable all instances of the Plotter class.\"\"\"\n        Plotter.enable = False\n\n    @staticmethod\n    def get_plotters():\n        return Plotter.__plotters\n\n    def start(self):\n        if not Plotter.enable:\n            return\n        if not self.worker_thread.is_alive():\n            tf.compat.v1.get_variable_scope().reuse_variables()\n            self.worker_thread.start()\n            self.queue.put(\n                Message(op=Op.UPDATE,\n                        args=(self.env, self.policy),\n                        kwargs=None))\n            atexit.register(self.close)\n\n    def update_plot(self, policy, max_length=np.inf):\n        if not Plotter.enable:\n            return\n        if self.worker_thread.is_alive():\n            self.queue.put(\n                Message(op=Op.DEMO,\n                        args=(policy.get_param_values(), max_length),\n                        kwargs=None))\n", "path": "src/garage/tf/plotter/plotter.py"}]}