peewee: peewee.InterfaceError: (0, '') | pymysql.err.InterfaceError: (0, '')

I am encountering this exception (using peewee-async high-level api) then my asyncio based daemon is idle (about 15 minutes or so), after that time, this exception occurs, I have tried to close and reopen connection, and was surprised that Manager (high-level api) did not track its state (close/reopen is done using its methods).

Traceback (most recent call last):
  File "/home/ubuntu/dati-server/match/manager.py", line 56, in clean_up_user
    await exam_end_all_handler(current_match_id, user_id)
  File "/home/ubuntu/dati-server/match/manager.py", line 407, in exam_end_all_handler
    match_result = await MatchUserRecord.set_user_score_and_winner(match_id)
  File "/home/ubuntu/dati-server/models/record.py", line 260, in set_user_score_and_winner
    score1 = await user_record1.get_match_score()
  File "/home/ubuntu/dati-server/models/record.py", line 108, in get_match_score
    answer_records = await objects.execute(UserAnswerRecord.filter(match_id=self.match.id, user_id=self.user_id))
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 1386, in __get__
    return self.get_object_or_id(instance)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 1377, in get_object_or_id
    obj = self.rel_model.get(self.field.to_field == rel_id)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 4988, in get
    return sq.get()
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 3220, in get
    return next(clone.execute())
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 3274, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 2939, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee_async.py", line 1041, in execute_sql
    return super().execute_sql(*args, **kwargs)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 3837, in execute_sql
    self.commit()
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 3656, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/peewee.py", line 3830, in execute_sql
    cursor.execute(sql, params or ())
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/pymysql/cursors.py", line 165, in execute
    result = self._query(query)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
    conn.query(q)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/pymysql/connections.py", line 859, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/home/ubuntu/dati-server/env/lib/python3.6/site-packages/pymysql/connections.py", line 1075, in _execute_command
    raise err.InterfaceError("(0, '')")
peewee.InterfaceError: (0, '')

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

I think we found the issue. Looking up earlier log, we noticed mysqld server closed the only connection we had and we did not reconnect after.

ref: http://docs.peewee-orm.com/en/latest/peewee/database.html#error-2006-mysql-server-has-gone-away ref: http://docs.peewee-orm.com/en/latest/peewee/database.html#flask

Ty!

I’d suggest carefully debugging this because it almost certainly indicates connection mis-management somewhere in the call-stack. This can be especially true with asyncio, e.g. https://github.com/coleifer/peewee/issues/1546#issuecomment-832784804

Ran into this issue and after digging and this does appear to be an issue with peewee itself. Database.is_closed returns the value of _ConnectionState.closed which does not mirror the various database engines concept of “closed”.

The reason I believe this is actually an issue with peewee: is_closed is checked regularly (connection, cursor, begin, etc…) to see if a DB connection should be (re)initiated via connect. As is_closed does not return the given DBs concept of “closed”, but instead it’s own internal flag, it’s will occasionally fail to reinitiate the connect for some common cases. If is_closed wasn’t used heavily in this way, I would agree this is an external issue.

Basically, peewee is maintaining state that doesn’t cleanly align with a given DB engine but is using that state to engage with said engine.

Here’s a very simple example of this behavior:

import peewee


class BaseModel(peewee.Model):
    class Meta:
        database = peewee.SqliteDatabase(":memory:")


bm = BaseModel()
assert bm._meta.database.is_closed() is True

bm._meta.database.get_tables()
assert bm._meta.database.is_closed() is False

# Close DB engine connection directly
bm._meta.database._state.conn.close()

# XXX This fails as `is_closed` needs to know about the DB engine its representing
assert bm._meta.database.is_closed() is True

Overriding is_closed for each Database child to align with a given engines concept of “closed” fixes this issue.

An example solution for SqliteDatabase

      def is_closed(self):
          try:
              if self._state.conn:
                  # afaict sqlite3 doesn't cleanly expose connection checking,
                  # try a simple call and see if it throws the expected error
                  self._state.conn.in_transaction
              else:
                  self._state.closed = True
          except sqlite3.ProgrammingError:
              self._state.closed = True
          return self._state.closed

An example solution for MySqlDatabase

      def is_closed(self):
          self._state.closed = self._state.conn._closed if self._state.conn else True
          return self._state.closed

Happy to provide a cleaned up PR if needed.

As I’ve said before, I’m quite confident this is always attributable to user error or mismanagement of the connection lifecycle.

We encounter the same problem with peewee with use recent flask version 1.0.2 without relying on flask-peewee.

We do not open/close connection between each request, I was expecting to be able to reuse the connection across request.

We do not use the connection pool.