# Buggy code
def generate_show_queue():
    """
    Generate a JSON-pickable list of items in the show queue.

    :returns: list of show queue items
    """
    if not app.show_queue_scheduler:
        return []

    queue = app.show_queue_scheduler.action.queue
    if app.show_queue_scheduler.action.currentItem is not None:
        queue.insert(0, app.show_queue_scheduler.action.currentItem)

    return [_queued_show_to_json(item) for item in queue]


# Traceback (RuntimeError)
2019-06-13 01:57:56 DEBUG    SHOWQUEUE-REFRESH :: [d34d957] Unable to retrieve image to save in /Users/ovidijus/Apps/Medusa/cache/images/tvdb/thumbnails/360579.banner.jpg, skipping
2019-06-13 01:57:56 INFO     SHOWQUEUE-REFRESH :: [d34d957] Cache check completed
2019-06-13 01:57:58 ERROR    SHOWQUEUE-REFRESH :: [d34d957] Exception generated: threads can only be started once
Traceback (most recent call last):
File "/Users/ovidijus/Apps/Medusa/medusa/scheduler.py", line 93, in run
self.action.run(self.force)
File "/Users/ovidijus/Apps/Medusa/medusa/generic_queue.py", line 99, in run
self.currentItem.start()
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 843, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
2019-06-13 01:58:01 ERROR    TORNADO :: [d34d957] Uncaught exception in APIv2: UnboundLocalError("local variable 'show_dir' referenced before assignment")
Request: GET /api/v2/config/ (127.0.0.1)
Traceback (most recent call last):
File "/Users/ovidijus/Apps/Medusa/ext/tornado/web.py", line 1592, in _execute
result = yield result
File "/Users/ovidijus/Apps/Medusa/ext/tornado/gen.py", line 1133, in run
value = future.result()
File "/Users/ovidijus/Apps/Medusa/ext/tornado/gen.py", line 1141, in run
yielded = self.gen.throw(*exc_info)
File "/Users/ovidijus/Apps/Medusa/medusa/server/api/v2/base.py", line 64, in async_call
content = yield IOLoop.current().run_in_executor(executor, prepared)
File "/Users/ovidijus/Apps/Medusa/ext/tornado/gen.py", line 1133, in run
value = future.result()
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/ovidijus/Apps/Medusa/medusa/server/api/v2/config.py", line 389, in get
config_data[section] = DataGenerator.get_data(section)
File "/Users/ovidijus/Apps/Medusa/medusa/server/api/v2/config.py", line 466, in get_data
return getattr(cls, 'data_' + section)()
File "/Users/ovidijus/Apps/Medusa/medusa/server/api/v2/config.py", line 929, in data_system
section_data['showQueue'] = generate_show_queue()
File "/Users/ovidijus/Apps/Medusa/medusa/system/schedulers.py", line 139, in generate_show_queue
return [_queued_show_to_json(item) for item in queue]
File "/Users/ovidijus/Apps/Medusa/medusa/system/schedulers.py", line 139, in <listcomp>
return [_queued_show_to_json(item) for item in queue]
File "/Users/ovidijus/Apps/Medusa/medusa/system/schedulers.py", line 93, in _queued_show_to_json
'showDir': show_dir,
UnboundLocalError: local variable 'show_dir' referenced before assignment

# Fixed code
def generate_show_queue():
    """
    Generate a JSON-pickable list of items in the show queue.

    :returns: list of show queue items
    """
    if not app.show_queue_scheduler:
        return []

    # Make a shallow copy of the current queue.
    queue = []
    queue.extend(app.show_queue_scheduler.action.queue)
    if app.show_queue_scheduler.action.currentItem is not None:
        queue.insert(0, app.show_queue_scheduler.action.currentItem)

    return [_queued_show_to_json(item) for item in queue]