0

I'm running a python bot in shared hosting it runs fine but after few hours daily 1 or 2 times it gets crash. So what is causing this error and how to solve this problem.

RuntimeError: can't start new thread
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f80a690b6d8>
Task exception was never retrieved
future: <Task finished coro=<WebSocketCommonProtocol.run() done, defined at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:428> exception=ConnectionResetError(104, 'Connection reset by peer')>
Traceback (most recent call last):
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 434, in run
    msg = yield from self.read_message()
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 456, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 511, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 546, in read_frame
    self.reader.readexactly, is_masked, max_size=max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/framing.py", line 86, in read_frame
    data = yield from reader(2)
  File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 674, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
    yield from self._waiter
  File "/home/demotry/.local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.run() running at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:434> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f80a6970f48>()]>>

I added my bot.py script code like below with some commands just one bot.py no other files. everything is working fine but crashes 2 or 3 times in a day.

token = "This is my Token" # This is what the bot uses to log into Discord.
prefix = "?" # This will be used at the start of commands.

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

@bot.command(pass_context=True)
async def ping(ctx):
    msg = 'Pong {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def hello(ctx):
    msg = 'hello... {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)
Demotry
  • 869
  • 4
  • 21
  • 40
  • 1
    [error: can't start new thread](https://stackoverflow.com/a/1835043) – Smart Manoj May 30 '18 at 07:45
  • 1
    @SmartManoj No idea what your link helps for my problem. i updated my script above check it. – Demotry May 30 '18 at 08:00
  • 1
    Looking at the code, I can't really see anything wrong with it. Rather, I agree with the link that @SmartManoj posted, so the error is not related to `discord.py`. What system are you running your bot on? Do you have other programs running on it? – Benjin May 30 '18 at 09:22
  • 1
    @Benjin I'm running this script in shared hosting linux server. no only running this script in background using `Screen` in bash. – Demotry May 30 '18 at 09:44
  • 2
    It might be that your resource allocation is too low in the shared environment, or that other users are running costly processes. – Benjin May 30 '18 at 09:48
  • 1
    @Benjin Is there any option to add in code like cooldown every xx hours so does it solves. – Demotry May 30 '18 at 10:02
  • 1
    An option is to have something that automatically restarts your bot for you. An example to run this in Python is by creating another script that calls `bot.py` with `os.system` – Benjin May 31 '18 at 06:55

0 Answers0