Report this

What is the reason for this report?

When running a python based discord bot on a droplet, I encounter a "Connection reset by peer" error not present when running locally.

Posted on April 24, 2019

I am just getting started learning about bots, droplets and everything else, but I have spent several hours bug testing my code on a local system and it runs without issue within its design scope. That said, please bear with any novice questions. I have tried to look for information, and while I understand that the error means Discord is closing the connection on their end, I can’t understand why it would only happen when running the program on the droplet.

The primary function of the bot is to translate English words into a glyph based conlang.

--------------------------------Explanation of bot function, may not be important------------------------------- To do so, when a user sends a message starting with the proper command, the contents of the message are split by space into individual strings for each word, those strings are checked for matches in the keys of a dict variable, that is an actual dictionary in this case. The equivalent word is pulled, then all the translated strings are merged, and saved as a png file (required to preserve the visual due to font limitations), which is sent as a message attachment by the bot. -----------------------------------------------------End of Explanation------------------------------------------------------

When running on my local system this process executes correctly, but when running from the droplet, the bot frequently fails to respond to translation commands, and eventually this error log appears in the console:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/synake/pybot/six/lib/python3.6/site-packages/discord/client.py", line 255, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 61, in on_message
    await message.channel.send(file=discord.File("ancient.png"), content = rep.str1)
  File "/home/synake/pybot/six/lib/python3.6/site-packages/discord/abc.py", line 759, in send
    content=content, tts=tts, embed=embed, nonce=nonce)
  File "/home/synake/pybot/six/lib/python3.6/site-packages/discord/http.py", line 158, in request
    async with self.__session.request(method, url, **kwargs) as r:
  File "/home/synake/pybot/six/lib/python3.6/site-packages/aiohttp/client.py", line 1005, in __aenter__
    self._resp = await self._coro
  File "/home/synake/pybot/six/lib/python3.6/site-packages/aiohttp/client.py", line 497, in _request
    await resp.start(conn)
  File "/home/synake/pybot/six/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 844, in start
    message, payload = await self._protocol.read()  # type: ignore  # noqa
  File "/home/synake/pybot/six/lib/python3.6/site-packages/aiohttp/streams.py", line 588, in read
    await self._waiter
aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

Other info: The bot was written on a local system that is running windows 8.1 while the droplet is using Ubuntu 18.04



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The error message you’re seeing is aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer. This is a low-level networking error indicating that the other side (in this case, Discord’s servers) unexpectedly closed the connection.

The fact that this error occurs when running on your DigitalOcean droplet but not your local system is interesting. It suggests that the issue might be related to the droplet’s environment or network, rather than the bot’s code itself. Here are a few things to consider:

  1. Rate Limiting: Discord has strict rate limits. If you exceed them, Discord might start dropping your connections. Make sure your bot respects Discord’s rate limits. You could be hitting these rate limits on the droplet but not your local machine due to differences in network latency or timing.

  2. Network Issues: There could be transient network issues between your droplet and Discord’s servers. You could try running a network diagnostic tool like mtr to check for packet loss or high latency. Also, make sure your droplet’s firewall (if any) isn’t blocking or interfering with outbound connections.

  3. Resource Limits: If your droplet is running low on resources (like memory or disk space), it could cause your bot to behave unpredictably. Check your droplet’s resource usage to ensure it has enough resources.

  4. Python and Library Versions: Ensure that the Python version and the versions of the discord.py and aiohttp libraries are the same on both your local system and the droplet.

  5. Concurrency Issues: If your bot is handling multiple requests concurrently, it could be running into concurrency issues. These issues can be hard to reproduce and might only occur under certain conditions (like when running on your droplet).

  6. File System Differences: Your bot is creating and sending a file (ancient.png). There might be differences between your local file system and your droplet’s file system that are causing issues. For example, there might be permissions issues on the droplet that prevent your bot from creating or reading the file.

As a first step, I would recommend adding more logging to your bot to help pinpoint where the error is occurring. You could also try catching and handling the ClientOSError in your bot’s code to see if you can recover from the error or log additional information.

Best,

Bobby

Adjust aiohttp Timeout Settings

Since you’re using aiohttp, try increasing the timeout settings for the requests. You can do this by adjusting the ClientSession timeout in your bot code. For example:

from aiohttp import ClientTimeout

timeout = ClientTimeout(total=60)  # Increase the timeout to 60 seconds
client = discord.Client(session=aiohttp.ClientSession(timeout=timeout))

Check Bot Version and aiohttp Version

  • Make sure that the discord.py and aiohttp versions are up to date on both your local machine and the droplet. Differences in versions might cause inconsistencies in behavior.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.