Issue: Interface error connections already closed on ubuntu
Author: vctrferreira
State: closed
Created: 2017-10-02 18:04:27

This is the traceback

```
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/web.py", line 1467, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "<string>", line 4, in raise_exc_info
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/stack_context.py", line 316, in wrapped
    ret = fn(*args, **kwargs)
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/web.py", line 1680, in future_complete
    f.result()
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 4, in raise_exc_info
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/gen.py", line 292, in wrapper
    result = func(*args, **kwargs)
  File "/home/app/webapp/env/lib/python3.5/types.py", line 243, in wrapped
    coro = func(*args, **kwargs)
  File "bothub/app/server.py", line 230, in get
    answer = bm.ask(message, uuid)
  File "bothub/app/server.py", line 87, in ask
    questions_queue = self._get_questions_queue(bot_uuid)
  File "bothub/app/server.py", line 80, in _get_questions_queue
    return self._get_bot_data(bot_uuid)['questions_queue']
  File "bothub/app/server.py", line 65, in _get_bot_data
    instance = Bot.get(Bot.uuid == bot_uuid)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 4926, in get
    return sq.get()
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3181, in get
    return next(clone.execute())
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3233, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 2912, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3775, in execute_sql
    self.commit()
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3598, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3766, in execute_sql
    cursor = self.get_cursor()
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3714, in get_cursor
    return self.get_conn().cursor()
peewee.InterfaceError: connection already closed

```
This is my code
```
from peewee import *
from datetime import datetime
from decouple import config

import uuid


DATABASE = PostgresqlDatabase(
    config('BOTHUB_POSTGRES_DB'),
    user=config('BOTHUB_POSTGRES_USER'),
    password=config('BOTHUB_POSTGRES_PASSWORD'),
    host=config('BOTHUB_POSTGRES'),
    port=config('BOTHUB_POSTGRES_PORT'), threadlocals=True)


class BaseModel(Model):
    class Meta:
        database = DATABASE

class Bot(BaseModel):
    uuid = UUIDField(primary_key=True, default=uuid.uuid4)
    bot = BlobField()
    created_at = DateTimeField(default=datetime.now)
    updated_at = DateTimeField()

    def save(self, *args, **kwargs):
        self.updated_at = datetime.now()
        return super(Bot, self).save(*args, **kwargs)

    class Meta:
        db_table = 'bots'


```

I don't know the reason for it, I saw a similar problem in issues (#334) and to "solved" it I did started my db with `threadlocals=True`, but it no solve my problem, you know to solve this?

IDK, some weirdness in tornado? No idea, you might try StackOverflow or track down the original exception?

This is the original traceback, you can help me? @coleifer 

```
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/web.py", line 1467, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "<string>", line 4, in raise_exc_info
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/stack_context.py", line 316, in wrapped
    ret = fn(*args, **kwargs)
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/web.py", line 1680, in future_complete
    f.result()
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 4, in raise_exc_info
  File "/home/app/webapp/env/lib/python3.5/site-packages/tornado/gen.py", line 292, in wrapper
    result = func(*args, **kwargs)
  File "/home/app/webapp/env/lib/python3.5/types.py", line 243, in wrapped
    coro = func(*args, **kwargs)
  File "bothub/app/server.py", line 230, in get
    answer = bm.ask(message, uuid)
  File "bothub/app/server.py", line 87, in ask
    questions_queue = self._get_questions_queue(bot_uuid)
  File "bothub/app/server.py", line 80, in _get_questions_queue
    return self._get_bot_data(bot_uuid)['questions_queue']
  File "bothub/app/server.py", line 65, in _get_bot_data
    instance = Bot.get(Bot.uuid == bot_uuid)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 4926, in get
    return sq.get()
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3181, in get
    return next(clone.execute())
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3233, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 2912, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3775, in execute_sql
    self.commit()
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3598, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/home/app/webapp/env/lib/python3.5/site-packages/peewee.py", line 3768, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: SSL error: decryption failed or bad record mac
```

Something's the matter in your database connection, apparently.