Skip to content

fix: use async Attachment.read() instead of blocking requests.get in Discord client#8186

Merged
kausmeows merged 5 commits into
agno-agi:mainfrom
kratos0718:fix/blocking-requests-in-discord-on-message
Jun 1, 2026
Merged

fix: use async Attachment.read() instead of blocking requests.get in Discord client#8186
kausmeows merged 5 commits into
agno-agi:mainfrom
kratos0718:fix/blocking-requests-in-discord-on-message

Conversation

@kratos0718

Copy link
Copy Markdown
Contributor

Summary

DiscordClient's async on_message handler called the synchronous, blocking requests.get() to download video and document attachments. A blocking network call inside a coroutine freezes the entire asyncio event loop for the full duration of the download, stalling every other message, typing indicator, and concurrent agent run handled by the bot.

This replaces requests.get(media_url) with discord.py's native await media.read(), which downloads the attachment without blocking the event loop. It also downloads through the bot's authenticated session, avoiding the 403 errors that plain HTTP downloads of Discord attachment URLs can hit. The now-unused requests import is removed.

Fixes #8185

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

Additional context

ruff check and ruff format pass. Adds libs/agno/tests/unit/integrations/test_discord_client.py with regression tests asserting the handler no longer uses requests.get and reads attachments via await media.read().

…Discord client

The Discord on_message handler is an async event handler, but it called the
synchronous, blocking requests.get() to download video and document
attachments. A blocking network call inside a coroutine freezes the entire
asyncio event loop for the full duration of the download, stalling every other
message, typing indicator, and concurrent agent run handled by the bot.

Replace requests.get(media_url) with discord.py's native await media.read(),
which downloads the attachment without blocking the event loop. This also
downloads through the bot's authenticated session, avoiding the 403 errors that
plain HTTP downloads of Discord attachment URLs can hit. The now-unused
requests import is removed.
@kratos0718 kratos0718 requested a review from a team as a code owner May 31, 2026 12:50
kratos0718 and others added 3 commits May 31, 2026 18:48
The unit test suite does not install discord.py, so importing
agno.integrations.discord.client (which raises ImportError without it) failed
collection on CI. Guard the import with pytest.importorskip("discord").

@kausmeows kausmeows left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

@kausmeows kausmeows merged commit 2c3317c into agno-agi:main Jun 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Blocking requests.get() in async Discord on_message handler freezes the event loop

3 participants