# Buggy code
def __init__(self, controller_handle, sync: bool):
    self.router = Router(controller_handle)

    if sync:
        self.async_loop = create_or_get_async_loop_in_thread()
        asyncio.run_coroutine_threadsafe(
            self.router.setup_in_async_loop(),
            self.async_loop,
        )
    else:
        self.async_loop = asyncio.get_event_loop()
        self.async_loop.create_task(self.router.setup_in_async_loop())


# Traceback (TypeError)
2021-01-04 21:15:59,360	INFO services.py:1173 -- View the Ray dashboard at http://127.0.0.1:8265
(pid=6369) 2021-01-04 21:16:01,432	INFO controller.py:346 -- Starting router with name 'hRhwaS:SERVE_CONTROLLER_ACTOR:SERVE_PROXY_ACTOR-node:192.168.31.141-0' on node 'node:192.168.31.141-0' listening on '127.0.0.1:8000'
(pid=6366) INFO:     Started server process [6366]
Traceback (most recent call last):
File "a.py", line 11, in <module>
ray.put(handle)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/worker.py", line 1411, in put
object_ref = worker.put_object(value, pin_object=True)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/worker.py", line 262, in put_object
serialized_value = self.get_serialization_context().serialize(value)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/serialization.py", line 406, in serialize
return self._serialize_to_msgpack(value)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/serialization.py", line 386, in _serialize_to_msgpack
self._serialize_to_pickle5(metadata, python_objects)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/serialization.py", line 346, in _serialize_to_pickle5
raise e
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/serialization.py", line 343, in _serialize_to_pickle5
value, protocol=5, buffer_callback=writer.buffer_callback)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/cloudpickle/cloudpickle_fast.py", line 73, in dumps
cp.dump(obj)
File "/Users/simonmo/miniconda3/envs/py37/lib/python3.7/site-packages/ray/cloudpickle/cloudpickle_fast.py", line 563, in dump
return Pickler.dump(self, obj)
File "stringsource", line 2, in ray._raylet.Count.__reduce_cython__
TypeError: self.c_tag_keys,self.metric cannot be converted to a Python object for pickling
(pid=6369) 2021-01-04 21:16:03,924	INFO controller.py:753 -- Registering route 'f' to endpoint 'f' with methods '['GET']'.
2021-01-04 21:16:06,021	INFO api.py:65 -- Shutting down Ray Serve because client went out of scope. To prevent this, either keep a reference to the client object or use serve.start(detached=True).

# Fixed code
def __init__(self, controller_handle, sync: bool):
    self.controller_handle = controller_handle
    self.sync = sync
    self.router = Router(controller_handle)

    if sync:
        self.async_loop = create_or_get_async_loop_in_thread()
        asyncio.run_coroutine_threadsafe(
            self.router.setup_in_async_loop(),
            self.async_loop,
        )
    else:
        self.async_loop = asyncio.get_event_loop()
        self.async_loop.create_task(self.router.setup_in_async_loop())