breqdev/flask-discord-interactions

Unclosed client session warning when using async (Quart) commands

filips123 opened this issue · 3 comments

I made a test Quart app that registers a few Discord commands (based on async_quart.py example). The app works and commands also return the correct response, but every time I use async command I get a warning. This does not happen with sync commands.

Although this seems to only be a warning and app still works, I think it would be nice if this warning is fixed or suppressed.

My code:

import os
import quart.flask_patch

from flask_discord_interactions import DiscordInteractions
from quart import Quart

app = Quart(__name__)
discord = DiscordInteractions(app)

app.config["DISCORD_CLIENT_ID"] = os.environ["DISCORD_CLIENT_ID"]
app.config["DISCORD_PUBLIC_KEY"] = os.environ["DISCORD_PUBLIC_KEY"]
app.config["DISCORD_CLIENT_SECRET"] = os.environ["DISCORD_CLIENT_SECRET"]

discord.update_slash_commands()

@discord.command()
async def ping(ctx):
    return "Pong!"

@discord.command()
def pong(ctx):
    return "Ping!"

discord.set_route_async("/interactions")
discord.update_slash_commands(guild_id=os.environ["TESTING_GUILD"])


if __name__ == '__main__':
    app.run()

And the warning (which happens when I use /ping):

$ python main.py
 * Serving Quart app 'main'
 * Environment: production
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Debug mode: False
 * Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2021-08-11 18:29:56,356] Running on http://127.0.0.1:5000 (CTRL + C to quit)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000018CB8BF4910>
[2021-08-11 18:30:21,070] 127.0.0.1:3050 POST /interactions 1.1 200 149 5213

Versions:

aiohttp==3.7.4.post0
Flask==2.0.1
Flask-Discord-Interactions==1.0.0
Quart==0.15.1

Thanks for filing an issue! I agree -- this is something that should be patched.

As a workaround for now, you can try calling ctx.close() at the end of your async function. This should allow you to manually close the aiohttp ClientSession.

That said, this library really should make this less of a pain point. It's a bit tricky though -- it could close the session at the end of each command invocation, but that would break anyone trying to do 'followup' messages (since those depend on the session being open after the initial response). I'm going to be actively working on this, but I can't give you a timeframe at the moment.

This should be fixed by v1.0.2. Can you install with pip3 install Flask-Discord-Interactions==1.0.2 and verify that the warning is gone in your project?

The release notes for v0.1.2 have more information about the update if you're curious, but the main change is that the client session is created once, used throughout the entire app, and torn down when the app finishes serving. Thus, as of v1.0.2, ctx.close() does nothing and doesn't need to be called.

I'm going to close this for now due to inactivity, feel free to reopen if you still have issues with this.