Repository: pymedusa/Medusa

Issue:
[Medusa WebUI Crashes] Uncaught exception in APIv2: UnboundLocalError("local variable 'show_dir' referenced before assignment")
**Describe the bug**
Medusa tornado services crashes after refreshing files from any show, it medusa continues to run in background but all WebUI access is dead keeps saying unable to connect to Medusa

**To Reproduce**
Steps to reproduce the behavior:
Go To Any Show & Force a full refresh from disk

**Screenshots**
![Screen Shot 2019-06-13 at 1 57 58 am](https://user-images.githubusercontent.com/2242880/59367093-c7438f00-8d7e-11e9-9a40-73a71b479e79.jpg)


**Medusa (please complete the following information):**
 - OS: macOS 10.14.5 (Darwin-18.6.0-x86_64-i386-64bit)
 - Branch: develop
 - Commit: d34d957654d4d50ff2e60af7bc83aabc9f8a8dc1
 - Python version: 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)]
 - Database version: Database: 44.14

**Debug logs (at least 50 lines):**
<details>

```
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
```
</details>

**Additional context**
Full Log:
[Full Log.log](https://github.com/pymedusa/Medusa/files/3282219/Full.Log.log)

I just picked this up.. doesn't happen in master though... or didn't happen form the past 5-6+ days ago..
[Medusa WebUI Crashes] Uncaught exception in APIv2: UnboundLocalError("local variable 'show_dir' referenced before assignment")
**Describe the bug**
Medusa tornado services crashes after refreshing files from any show, it medusa continues to run in background but all WebUI access is dead keeps saying unable to connect to Medusa

**To Reproduce**
Steps to reproduce the behavior:
Go To Any Show & Force a full refresh from disk

**Screenshots**
![Screen Shot 2019-06-13 at 1 57 58 am](https://user-images.githubusercontent.com/2242880/59367093-c7438f00-8d7e-11e9-9a40-73a71b479e79.jpg)


**Medusa (please complete the following information):**
 - OS: macOS 10.14.5 (Darwin-18.6.0-x86_64-i386-64bit)
 - Branch: develop
 - Commit: d34d957654d4d50ff2e60af7bc83aabc9f8a8dc1
 - Python version: 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)]
 - Database version: Database: 44.14

**Debug logs (at least 50 lines):**
<details>

```
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
```
</details>

**Additional context**
Full Log:
[Full Log.log](https://github.com/pymedusa/Medusa/files/3282219/Full.Log.log)

I just picked this up.. doesn't happen in master though... or didn't happen form the past 5-6+ days ago..


Discussion:
Thanks for the bug report.
#6821 should fix the `UnboundLocalError` and the WebUI freezing when the show queue is populated.
I don't know what caused this:
```
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
```
Thanks for the bug report.
#6821 should fix the `UnboundLocalError` and the WebUI freezing when the show queue is populated.
I don't know what caused this:
```
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
```

Resolving patch:
diff --git a/medusa/system/schedulers.py b/medusa/system/schedulers.py
--- a/medusa/system/schedulers.py
+++ b/medusa/system/schedulers.py
@@ -131,7 +131,9 @@ def generate_show_queue():
     if not app.show_queue_scheduler:
         return []
 
-    queue = app.show_queue_scheduler.action.queue
+    # 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)
 


Test patch: