#๐Ÿ”’ Aiohttp blocking (?) main loop

5 messages ยท Page 1 of 1 (latest)

covert ether
#

Hi, so to be short i have a discord bot that constantly sends requests to an API, but every once in a while aiohttp raises a aiohttp.ConnectionTimeoutError, followed by an asyncio.CancelledError. I would not make to much of an issue about that but after it happens something strange start happening, and the bot (i'm using pycord) stops working.

This issue is occurring in production enviroment (since i never made him run locally for large amount of times, now it is online 24/7), initially it happened every 24 hours more or less, to try to solve it i added a custom session:

import aiohttp

_session: aiohttp.ClientSession | None = None


def get_session() -> aiohttp.ClientSession:
    global _session
    if _session and not _session.closed:
        return _session
    timeout = aiohttp.ClientTimeout(total=30, connect=10, sock_connect=10, sock_read=20)
    connector = aiohttp.TCPConnector(limit=100, ttl_dns_cache=300)
    _session = aiohttp.ClientSession(timeout=timeout, connector=connector)
    return _session

async def close_session():
    global _session
    if _session and not _session.closed:
        await _session.close()
        _session = None

This change seems to work just fine but today the issue appeared again after 5 bot working days

Here you can find the logs: https://pastebin.com/d4E52uEL

After this error occurs it seems like my main loop gots blocked, i can't invoke commands and no errors are shown when i do, the bot responds with interaction error like if the bot is offline, even if so i still got logs from background activities like scheduled cogs and tasks

(More or less the bot sends 80+ request per hour)

torn nightBOT
#

@covert ether

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

median idol
#

yeah you don't want exceptions running away causing havoc in production, you should keep the request in a try/except if you know it might fail

torn nightBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.