Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding source of PyMySQL error - err.InterfaceError("(0, '')")

I am a Discord bot developer, and recently completed an order. The client upon setting the application up on their server initially had no issues, but according to them after running for "about three hours" the program begins spitting a specific stack trace error and no longer accepting commands.

The bot is built using Discord.py and uses Peewee as an ORM, using PyMySQL as the database driver. The server the client is running it on is hosted by DigitalOcean and if any information about the hardware, etc. is needed the client is able to give me that information on request. We have already attempted uninstalling and reinstalling all dependencies, as well as trying different distributions of them, but the errors persist.

This is the exact trace that the client is receiving:

  File "/usr/local/lib/python3.6/dist-packages/peewee.py", line 2666, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/local/lib/python3.6/dist-packages/peewee.py", line 179, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/dist-packages/peewee.py", line 2875, in execute_sql
    cursor.execute(sql, params or ())
  File "/usr/local/lib/python3.6/dist-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 516, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/usr/local/lib/python3.6/dist-packages/pymysql/connections.py", line 750, in _execute_command
    raise err.InterfaceError("(0, '')")
peewee.InterfaceError: (0, '')

The relevant portions from my database.py file, where the database connection is opened:

import discord
from peewee import *

from config import db_host, db_port, mysql_db_name, \
    mysql_db_username, mysql_db_password

db_connection = MySQLDatabase(
    mysql_db_name,
    user = mysql_db_username,
    password = mysql_db_password,
    host = db_host,
    port = db_port
)

def create_db_tables():
    # create_tables() does safe creation by default, and will simply not create
    # table if it already exists
    db_connection.create_tables([User])

The relevant portions from my bot.py file, specifically the bot startup function that runs when the bot is first opened, and the lines that create and start the bot client:

client = discord.Client()

async def bot_startup():
    # Check for config changes
    if client.user.name != config.bot_username:
        print("Username Updated To: {}".format(config.bot_username))
        await client.edit_profile(username=config.bot_username)

    # Start 'playing' message
    await client.change_presence(
        game=discord.Game( name=config.playing_message )
    )

    # Prepare database
    database.create_db_tables()
    print("Database Connected")

    print("Connected Successfully")

# ...

@client.event
async def on_ready():
    await bot_startup()

# ...

client.run(config.token)

According to the client, restarting the bot temporarily solves the problem and it runs fine for a few hours before the errors start up again. The bot no longer responds to any incoming commands once the errors start, and if enough errors are thrown, crashes completely.

What is typically the cause of this error, and what steps should be taken to fix whatever is causing it?

like image 454
Sparrow Avatar asked Nov 17 '22 08:11

Sparrow


1 Answers

discord.py is asynchronous whilst PyMySQL is not - therefore it is blocking the discord.py runtime. Instead of PyMySQL use AIOMySQL which is non-blocking and might just solve your error.

like image 185
Syntaxツ Avatar answered Dec 09 '22 10:12

Syntaxツ