Repository: Flexget/Flexget

Issue:
Database clean up resulting in crash
### The problem
The database clean up results in a crash. It seems this is related to the VACUUM operations on the database. I already reinstalled Python, Flexget + all dependencies and trashed my database. But the problem persists.

### Steps to reproduce:
During its normal run flexget performs a database clean up. his can also be triggered manually by the command below. Both give similar output.
`$ flexget database clean`

flexget.log:
```
2016-12-30 14:44 INFO     manager       SD        Running database cleanup.
2016-12-30 14:44 INFO     db_vacuum     SD        Running VACUUM on database to improve performance and decrease db size.
2016-12-30 14:44 CRITICAL task_queue                    Database error while running a task. Attempting to recover.
2016-12-30 14:44 CRITICAL manager                       An unexpected crash has occurred. Writing crash report to /Users/me/.flexget/crash_report.2016.12.30.144427043731.log. Please verify you are running the latest version of flexget by using "flexget -V" from CLI or by using version_checker plugin at http://flexget.com/wiki/Plugins/version_checker. You are currently using version 2.8.14
```
crash_report.2016.12.30.144427043731.log:
```
2016-12-30 14:44 INFO     manager                       Running database cleanup.
2016-12-30 14:44 INFO     db_vacuum                     Running VACUUM on database to improve performance and decrease db size.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: cannot VACUUM from within a transaction

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/flexget", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/flexget/__init__.py", line 42, in main
    manager.start()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 327, in start
    self.handle_cli()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 354, in handle_cli
    options.cli_command_callback(self, command_options)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 16, in do_cli
    cleanup(manager)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 26, in cleanup
    manager.db_cleanup(force=True)
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 896, in db_cleanup
    fire_event('manager.db_cleanup', self, session)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 106, in fire_event
    result = event(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/generic/db_vacuum.py", line 22, in on_cleanup
    session.execute('VACUUM')
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 185, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) cannot VACUUM from within a transaction [SQL: 'VACUUM']
````


### Additional information:

- Flexget Version: 2.8.14
- Python Version: Python 3.6.0
- Installation method: pip3
- OS and version: macOS 10.12.2
#### Flexget dependencies:
- pathlib 1.0.1
- jsonschema 2.5.1
- pyparsing 2.1.10
- SQLAlchemy 1.1.4
- path.py 9.0.1
- guessit 2.0.4
- flask 0.12
- flask-cors 3.0.2
- apscheduler 3.3.1
- cherrypy 8.6.0
- flask-compress 1.3.2
- terminaltables 3.1.0
- flask-login 0.4.0
- PyRSS2Gen 1.1
- html5lib 0.999999999
- requests 2.12.4
- flask-restful 0.3.5
- pynzb 0.1.0
- colorclass 2.2.0
- beautifulsoup4 4.5.1
- jinja2 2.8.1
- PyYAML 3.12
- future 0.16.0
- python-dateutil 2.6.0
- rpyc 3.3.0
- FeedParser 5.2.1
- flask-restplus 0.8.6
- Safe 0.4


<!---
Please verify that the following data is present before submitting your issue:

- Paste or link to a paste service (http://pastebin.com/ for example) of relevant config (preferably full config including templates if present. Remember to redact any personal information! Please make sure the paste does not expire, if possible.
- Paste or link to a paste service of debug level logs of relevant task/s. Use `flexget -L debug execute --tasks <Task_name>`
- Flexget version (Use `flexget -V` to get it).
- Full Python version (`2.7.11` for example). Run `python -V` to get it.
- Installation method (pip, git install, etc.)
- OS and version
- Attach crash log if available
--->



Discussion:
hmm, does this occur when daemon is running? if so, did you try to restart daemon?
Both with daemon and manually by either running `flexget execute` or `flexget database clean`.
please run with the `--debug` and `--debug-db-sessions` flags and paste full output
Could this be python 3.6 related issue?
I installed and couldn't reproduce. Seems unlikely 
This also happens to me, running flexget 2.8.17 in a FreeNAS 9 jail (FreeBSD 10.2):

```
2017-01-04 06:13 CRITICAL task_queue                    Database error while running a task. Attempting to recover.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flexget/task_queue.py", line 48, in run
    self.current_task.execute()
  File "/usr/local/lib/python2.7/site-packages/flexget/task.py", line 72, in wrapper
    return func(self, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/flexget/task.py", line 643, in execute
    self.manager.db_cleanup()
  File "/usr/local/lib/python2.7/site-packages/flexget/manager.py", line 896, in db_cleanup
    fire_event('manager.db_cleanup', self, session)
  File "/usr/local/lib/python2.7/site-packages/flexget/event.py", line 106, in fire_event
    result = event(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/flexget/plugins/generic/db_vacuum.py", line 22, in on_cleanup
    session.execute('VACUUM')
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
OperationalError: (pysqlite2.dbapi2.OperationalError) cannot VACUUM from within a transaction [SQL: u'VACUUM']
```
Please run with the aforementioned flags
Are these the exact commands you want me to run?

**--debug**
```
$ flexget --debug database cleanup
2017-01-04 23:17 DEBUG    manager                       Figuring out config load paths
2017-01-04 23:17 DEBUG    manager                       Found config: /Users/koen/.flexget/config.yml
2017-01-04 23:17 DEBUG    manager                       Config file /Users/koen/.flexget/config.yml selected
2017-01-04 23:17 DEBUG    manager                       sys.defaultencoding: utf-8
2017-01-04 23:17 DEBUG    manager                       sys.getfilesystemencoding: utf-8
2017-01-04 23:17 DEBUG    manager                       flexget detected io encoding: utf-8
2017-01-04 23:17 DEBUG    manager                       os.path.supports_unicode_filenames: True
2017-01-04 23:17 DEBUG    plugin                        Trying to load plugins from: ['/Users/koen/.flexget/plugins', '/usr/local/lib/python3.6/site-packages/flexget/plugins']
2017-01-04 23:17 DEBUG    safe                          Reading from cache file /var/folders/2r/tx6q38317wz7d1rdqp8cjj3c0000gn/T/safe-0.4.words.cache
2017-01-04 23:17 DEBUG    plugin                        ('Plugin `%s` requires `%s` to load.', 'memusage', 'ext lib `guppy`')
2017-01-04 23:17 DEBUG    plugin                        Plugins took 2.28 seconds to load. 268 plugins in registry.
2017-01-04 23:17 DEBUG    manager                       Connecting to: sqlite:////Users/koen/.flexget/db-config.sqlite
2017-01-04 23:17 DEBUG    manager                       config_name: config
2017-01-04 23:17 DEBUG    manager                       config_base: /Users/koen/.flexget
2017-01-04 23:17 DEBUG    manager                       New config data loaded.
2017-01-04 23:17 DEBUG    schema                        current flexget version already exist in db 2.8.17
2017-01-04 23:17 DEBUG    parsing                       setting default movie parser to internal. (options: {'guessit': <flexget.plugins.parsers.parser_guessit.ParserGuessit object at 0x10b86bb38>, 'internal': <flexget.plugins.parsers.parser_internal.ParserInternal object at 0x10b86bb70>})
2017-01-04 23:17 DEBUG    parsing                       setting default series parser to internal. (options: {'guessit': <flexget.plugins.parsers.parser_guessit.ParserGuessit object at 0x10b86bb38>, 'internal': <flexget.plugins.parsers.parser_internal.ParserInternal object at 0x10b86bb70>})
2017-01-04 23:17 INFO     manager                       Running database cleanup.
2017-01-04 23:17 DEBUG    seen                          TODO: Disabled because of ticket #1321
2017-01-04 23:17 INFO     db_vacuum                     Running VACUUM on database to improve performance and decrease db size.
2017-01-04 23:17 DEBUG    manager                       Removed /Users/koen/.flexget/.config-lock
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: cannot VACUUM from within a transaction

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/flexget", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/flexget/__init__.py", line 42, in main
    manager.start()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 327, in start
    self.handle_cli()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 354, in handle_cli
    options.cli_command_callback(self, command_options)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 16, in do_cli
    cleanup(manager)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 26, in cleanup
    manager.db_cleanup(force=True)
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 896, in db_cleanup
    fire_event('manager.db_cleanup', self, session)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 106, in fire_event
    result = event(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/generic/db_vacuum.py", line 22, in on_cleanup
    session.execute('VACUUM')
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 185, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) cannot VACUUM from within a transaction [SQL: 'VACUUM']
```

**--debug-db-sessions**
```
$ flexget --debug-db-sessions database cleanup
2017-01-04 23:18 INFO     manager                       Running database cleanup.
2017-01-04 23:18 INFO     db_vacuum                     Running VACUUM on database to improve performance and decrease db size.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: cannot VACUUM from within a transaction

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/flexget", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/flexget/__init__.py", line 42, in main
    manager.start()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 327, in start
    self.handle_cli()
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 354, in handle_cli
    options.cli_command_callback(self, command_options)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 16, in do_cli
    cleanup(manager)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/cli/database.py", line 26, in cleanup
    manager.db_cleanup(force=True)
  File "/usr/local/lib/python3.6/site-packages/flexget/manager.py", line 896, in db_cleanup
    fire_event('manager.db_cleanup', self, session)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 106, in fire_event
    result = event(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/flexget/plugins/generic/db_vacuum.py", line 22, in on_cleanup
    session.execute('VACUUM')
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 185, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) cannot VACUUM from within a transaction [SQL: 'VACUUM']
```
This also happens for me. Vacuum doesn't work at all, it just crashes.
```
flexget --debug --debug-db-sessions database cleanup

2017-01-08 12:42 DEBUG    manager                       Figuring out config load paths
2017-01-08 12:42 DEBUG    manager                       Found config: C:\Users\xxx\flexget\config.yml
2017-01-08 12:42 DEBUG    manager                       Config file C:\Users\xxx\flexget\config.yml selected
2017-01-08 12:42 DEBUG    manager                       sys.defaultencoding: utf-8
2017-01-08 12:42 DEBUG    manager                       sys.getfilesystemencoding: utf-8
2017-01-08 12:42 DEBUG    manager                       flexget detected io encoding: utf-8
2017-01-08 12:42 DEBUG    manager                       os.path.supports_unicode_filenames: True
2017-01-08 12:42 DEBUG    plugin                        Trying to load plugins from: ['C:\\Users\\xxx\\flexget\\plugins', 'c:\\users\\xxx\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\flexget\\plugins']
2017-01-08 12:42 DEBUG    safe                          Reading from cache fileC:\Users\xxx\AppData\Local\Temp\safe-0.4.words.cache
2017-01-08 12:42 DEBUG    deluge                        Looking for deluge install in C:\Program Files (x86)\Deluge
2017-01-08 12:42 DEBUG    plugin                        ('Plugin `%s` requires `%s` to load.', 'memusage', 'ext lib `guppy`')
2017-01-08 12:42 DEBUG    plugin                        Plugins took 2.14 seconds to load. 267 plugins in registry.
2017-01-08 12:42 DEBUG    manager                       Connecting to: sqlite:///C:\\Users\\xxx\\flexget\\db-config.sqlite
2017-01-08 12:42 DEBUG    manager                       config_name: config
2017-01-08 12:42 DEBUG    manager                       config_base: C:\Users\xxx\flexget
2017-01-08 12:42 DEBUG    manager                       New config data loaded.
2017-01-08 12:42 DEBUG    schema                        current flexget versionalready exist in db 2.8.20
2017-01-08 12:42 DEBUG    parsing                       setting default movie parser to internal. (options: {'guessit': <flexget.plugins.parsers.parser_guessit.ParserGuessit object at 0x063BF4B0>, 'internal': <flexget.plugins.parsers.parser_internal.ParserInternal object at 0x063BF4D0>})
2017-01-08 12:42 DEBUG    parsing                       setting default series parser to internal. (options: {'guessit': <flexget.plugins.parsers.parser_guessit.ParserGuessit object at 0x063BF4B0>, 'internal': <flexget.plugins.parsers.parser_internal.ParserInternal object at 0x063BF4D0>})
2017-01-08 12:42 INFO     manager                       Running database cleanup.
2017-01-08 12:42 DEBUG    debug_db_sess                 Transaction 0x0661A490 opened ('flexget.utils.simple_persistence', 77, 'db_cleanup', 'session.query(SimpleKeyValue).filter(~SimpleKeyValue.task.in_(existing_tasks)).delete(synchronize_session=False)')
2017-01-08 12:42 DEBUG    seen                          TODO: Disabled because of ticket #1321
2017-01-08 12:42 INFO     db_vacuum                     Running VACUUM on database to improve performance and decrease db size.
2017-01-08 12:42 DEBUG    debug_db_sess                 Transaction 0x0661A490 closed ('flexget.utils.sqlalchemy_utils', 144, '__exit__', 'self.rollback()') (open time 0.0243985652923584)
2017-01-08 12:42 DEBUG    manager                       Removed C:\Users\xxx\flexget\.config-lock
Traceback (most recent call last):
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: cannot VACUUM from within a transaction
```
The above exception was the direct cause of the following exception:
```
Traceback (most recent call last):
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\runpy.py",line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\runpy.py",line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\Scripts\flexget.exe\__main__.py", line 9, in <module>
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\__init__.py", line 42, in main
    manager.start()
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\manager.py", line 327, in start
    self.handle_cli()
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\manager.py", line 354, in handle_cli
    options.cli_command_callback(self, command_options)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\plugins\cli\database.py", line 16, in do_cli
    cleanup(manager)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\plugins\cli\database.py", line 26, in cleanup
    manager.db_cleanup(force=True)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\manager.py", line 896, in db_cleanup
    fire_event('manager.db_cleanup', self, session)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\event.py", line 106, in fire_event
    result = event(*args, **kwargs)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\flexget\plugins\generic\db_vacuum.py", line 22, in on_cleanup
    session.execute('VACUUM')
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\orm\session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
    context)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\util\compat.py", line 185, in reraise
    raise value.with_traceback(tb)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "c:\users\xxx\appdata\local\programs\python\python36-32\lib\site-packages\sqlalchemy\engine\default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) cannot VACUUM from within a transaction [SQL: 'VACUUM']
```
On Windows 8.1.
I think the problem is when vacuum is needed and there's stuff to cleanup too, vacuum should probably not hook the cleanup event.
Try running vacuum first and then cleanup, just to verify:
 `flexget database vacuum`
vacuum works?
Afaik py3.6 has new faster dict implementation which might have ended up running event hooks in different order, causing this. Vacuum should probably be handled differently ..
there's a 2.7 log too above @paranoidi...
@anttikeranen can you paste just vacuum logs with those flags as well? 
hmm, just saw this: https://github.com/Flexget/Flexget/issues/917
do you have free space on the drive?
yeah, i saw that. i'm not sure if that's relevant since not ALL vacuum actions crash. but just to be thorough, why don't you try to run that compact database using sqlitebrowser? 
yeah, the way we use VACUUM is wrong I guess, should not be attached to an existing transaction. Weird that this issue doesn't happen more often. 
@paranoidi I think for a quick patch we should remove the `db_cleanup` hook for vacuum, until we decide how to do it correctly?
For me, I just commented lines 895 and 896 in manager.py:

```python
 if force or expired:
            log.info('Running database cleanup.')
           # with Session() as session:
               # fire_event('manager.db_cleanup', self, session)`
```

This way the database cleanup doesn't fire when flexget runs, although maybe a more permanent solution should be implemented.
Thanks for the tip but I installed Python 2.7.13 and now it works!

Resolving patch:
diff --git a/flexget/_version.py b/flexget/_version.py
--- a/flexget/_version.py
+++ b/flexget/_version.py
@@ -7,4 +7,4 @@
 The jenkins release job will automatically strip the .dev for release,
 and update the version again for continued development.
 """
-__version__ = '2.10.112.dev'
+__version__ = '2.11.0.dev'
diff --git a/flexget/manager.py b/flexget/manager.py
--- a/flexget/manager.py
+++ b/flexget/manager.py
@@ -894,6 +894,8 @@ def db_cleanup(self, force=False):
             log.info('Running database cleanup.')
             with Session() as session:
                 fire_event('manager.db_cleanup', self, session)
+            # Try to VACUUM after cleanup
+            fire_event('manager.db_vacuum', self)
             # Just in case some plugin was overzealous in its cleaning, mark the config changed
             self.config_changed()
             self.persist['last_cleanup'] = datetime.now()
diff --git a/flexget/utils/sqlalchemy_utils.py b/flexget/utils/sqlalchemy_utils.py
--- a/flexget/utils/sqlalchemy_utils.py
+++ b/flexget/utils/sqlalchemy_utils.py
@@ -80,6 +80,7 @@ def table_add_column(table, name, col_type, session, default=None):
     type_string = session.bind.engine.dialect.type_compiler.process(col_type)
     statement = 'ALTER TABLE %s ADD %s %s' % (table.name, name, type_string)
     session.execute(statement)
+    session.commit()
     # Update the table with the default value if given
     if default is not None:
         # Get the new schema with added column
@@ -89,6 +90,7 @@ def table_add_column(table, name, col_type, session, default=None):
         default._set_parent(getattr(table.c, name))
         statement = table.update().values({name: default.execute(bind=session.bind)})
         session.execute(statement)
+        session.commit()
 
 
 def drop_tables(names, session):


Test patch: