Repository: Flexget/Flexget

Issue:
RuntimeError: threads can only be started once
### Expected behaviour:

While daemon running, I should still be able to run a manual execution.
### Actual behaviour:

It's seeing process is there but crashes because of Runtime error regards threads.
### Steps to reproduce:
- Step 1: start daemon `flexget daemon start -d`
- Step 2: run standard execution `flexget execute --verbose`
#### Log:

```
There is a FlexGet process already running for this config, sending execution there.
Traceback (most recent call last):
  File "/opt/bin/flexget", line 11, in <module>
    sys.exit(main())
  File "/opt/lib/python3.5/site-packages/flexget/__init__.py", line 42, in main
    manager.start()
  File "/opt/lib/python3.5/site-packages/flexget/manager.py", line 294, in start
    client.handle_cli(self.args)
  File "/opt/lib/python3.5/site-packages/rpyc/core/netref.py", line 196, in __call__
    return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
  File "/opt/lib/python3.5/site-packages/rpyc/core/netref.py", line 71, in syncreq
    return conn.sync_request(handler, oid, *args)
  File "/opt/lib/python3.5/site-packages/rpyc/core/protocol.py", line 441, in sync_request
    raise obj
RuntimeError: threads can only be started once

========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "/opt/lib/python3.5/site-packages/rpyc/core/protocol.py", line 305, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "/opt/lib/python3.5/site-packages/rpyc/core/protocol.py", line 535, in _handle_call
    return self._local_objects[oid](*args, **dict(kwargs))
  File "/opt/lib/python3.5/site-packages/flexget/ipc.py", line 80, in exposed_handle_cli
    self.manager.handle_cli(options)
  File "/opt/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/opt/lib/python3.5/site-packages/flexget/logger.py", line 88, in capture_output
    yield
  File "/opt/lib/python3.5/site-packages/flexget/ipc.py", line 80, in exposed_handle_cli
    self.manager.handle_cli(options)
  File "/opt/lib/python3.5/site-packages/flexget/manager.py", line 333, in handle_cli
    self.execute_command(command_options)
  File "/opt/lib/python3.5/site-packages/flexget/manager.py", line 365, in execute_command
    self.task_queue.start()
  File "/opt/lib/python3.5/site-packages/flexget/task_queue.py", line 35, in start
    self._thread.start()
  File "/opt/lib/python3.5/threading.py", line 840, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
```
### Additional information:
- Flexget Version: 2.1.1
- Python Version: 3.5
- Installation method: pip install
- OS and version: Android 4.4 AMLogic s805



Discussion:
I've got the same error message;

There is a FlexGet process already running for this config, sending execution there.
Traceback (most recent call last):
  File "./bin/flexget", line 11, in <module>
    sys.exit(main())
  File "/opt/local/lib/python3.5/site-packages/flexget/**init**.py", line 42, in main
    manager.start()
  File "/opt/local/lib/python3.5/site-packages/flexget/manager.py", line 295, in start
    client.handle_cli(self.args)
  File "/opt/local/lib/python3.5/site-packages/rpyc/core/netref.py", line 196, in **call**
    return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
  File "/opt/local/lib/python3.5/site-packages/rpyc/core/netref.py", line 71, in syncreq
    return conn.sync_request(handler, oid, *args)
  File "/opt/local/lib/python3.5/site-packages/rpyc/core/protocol.py", line 441, in sync_request
    raise obj
RuntimeError: threads can only be started once

========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "/opt/local/lib/python3.5/site-packages/rpyc/core/protocol.py", line 305, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "/opt/local/lib/python3.5/site-packages/rpyc/core/protocol.py", line 535, in _handle_call
    return self._local_objects[oid](*args, **dict%28kwargs%29)
  File "/opt/local/lib/python3.5/site-packages/flexget/ipc.py", line 81, in exposed_handle_cli
    self.manager.handle_cli(options)
  File "/opt/local/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/opt/local/lib/python3.5/site-packages/flexget/logger.py", line 83, in capture_output
    yield
  File "/opt/local/lib/python3.5/site-packages/flexget/ipc.py", line 81, in exposed_handle_cli
    self.manager.handle_cli(options)
  File "/opt/local/lib/python3.5/site-packages/flexget/manager.py", line 334, in handle_cli
    self.execute_command(command_options)
  File "/opt/local/lib/python3.5/site-packages/flexget/manager.py", line 366, in execute_command
    self.task_queue.start()
  File "/opt/local/lib/python3.5/site-packages/flexget/task_queue.py", line 36, in start
    self._thread.start()
  File "/opt/local/lib/python3.5/threading.py", line 840, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once

try running 

```
flexget --verbose execute
```

See if it still crashes.

With that I get:

```
$ flexget --verbose execute
There is a FlexGet process already running for this config, sending execution there.
usage: flexget [-h] [-V] [--test] [-c CONFIG] [--logfile LOGFILE]
               [--loglevel LEVEL] [--bugreport] [--profile [OUTFILE]] [--cron]
               [--debug-db-sessions] [--debug-warnings]
               <command> ...
flexget: error: unrecognized arguments: --verbose
```

Assuming you meant "flexget execute --verbose", it makes no difference. Still crashes with "RuntimeError: threads can only be started once" and no additional detail. Also same result with latest version 2.3.27.

I have this issue as well. Running 2.3.41 with Python3

@gazpachoking any clue?

It seems to be something related to Python3 - I reverted back to Python27 and i'm not having this issue anymore. 

Still occurring running 2.3.42 and Python 3.5.1, while in daemon. Running `flexget execute` with any flags or none. 

Sometimes I restart and the problem goes away...hmm


Resolving patch:
diff --git a/flexget/manager.py b/flexget/manager.py
--- a/flexget/manager.py
+++ b/flexget/manager.py
@@ -367,7 +367,12 @@ def execute_command(self, options):
         :param options: argparse options
         """
         fire_event('manager.execute.started', self, options)
-        if self.task_queue.is_alive():
+        if self.task_queue.is_alive() or self.is_daemon:
+            if not self.task_queue.is_alive():
+                log.error('Task queue has died unexpectedly. Restarting it. Please open an issue on Github and include'
+                          ' any previous error logs.')
+                self.task_queue = TaskQueue()
+                self.task_queue.start()
             if len(self.task_queue):
                 log.verbose('There is a task already running, execution queued.')
             finished_events = self.execute(options, output=logger.get_capture_stream(),


Test patch: