beeb/pancaketrade

Errors and the bot does not answer. (KeyError: (<pancaketrade.network.bsc.Network object at 0x7f3249d0b370>,))

Opened this issue · 8 comments

Hello genius!
First I want to thank you for the work you did. This is a very useful tool for people who operate in the defi space.
I dedicate myself more to trading than to programming, so understanding how this program works in depth is a bit difficult for me.
I already managed to make work and configure it without a problem, what happens to me now is that after running for a while, it remains frozen. When I look at the terminal I get the following error:

ERROR |Exception while handling an update
ERROR |(<pancaketrade.network.bsc.Network object at 0x7f3249d0b370>,)
ERROR |An uncaught error was raised while handling the error.
Traceback (most recent call last):
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/ext/handler.py", line 198, in handle_update
return self.callback(update, context)
File "/home/morpheus/pancaketrade/pancaketrade/utils/generic.py", line 64, in wrapper_check_chat_id
return func(this, update, context, *args, **kwargs)
File "/home/morpheus/pancaketrade/pancaketrade/bot.py", line 163, in command_status
status, balance_value = self.get_token_status(token)
File "/home/morpheus/pancaketrade/pancaketrade/bot.py", line 313, in get_token_status
token_price, base_token_address = self.net.get_token_price(token_address=token.address)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/cachetools/decorators.py", line 26, in wrapper
v = func(*args, kwargs)
File "/home/morpheus/pancaketrade/pancaketrade/network/bsc.py", line 253, in get_token_price
return self.get_token_price_for_lp(token, base_token, ignore_poolsize=True), base_token_address
File "/home/morpheus/pancaketrade/pancaketrade/network/bsc.py", line 287, in get_token_price_for_lp
elif (base_amount / self.get_bnb_price()) / Decimal(10
18) < self.min_pool_size_bnb and not ignore_poolsize:
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/cachetools/decorators.py", line 28, in wrapper
cache[k] = v
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/cachetools/ttl.py", line 87, in setitem
self.expire(time)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/cachetools/ttl.py", line 167, in expire
cache_delitem(self, curr.key)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/cachetools/cache.py", line 66, in delitem
del self.__data[key]
KeyError: (<pancaketrade.network.bsc.Network object at 0x7f3249d0b370>,)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 567, in process_update
self.dispatch_error(update, exc)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 810, in dispatch_error
callback(update, context)
File "/home/morpheus/pancaketrade/pancaketrade/bot.py", line 400, in error_handler
chat_message(update, context, text=f"⛔️ Exception while handling an update\n{context.error}", edit=False)
File "/home/morpheus/pancaketrade/pancaketrade/utils/generic.py", line 91, in chat_message
return context.bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=reply_markup)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/bot.py", line 130, in decorator
result = func(*args, **kwargs)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/bot.py", line 522, in send_message
return self._message( # type: ignore[return-value]
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/ext/extbot.py", line 200, in _message
result = super()._message(
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/bot.py", line 336, in _message
result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/bot.py", line 295, in _post
return self.request.post(
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/utils/request.py", line 361, in post
result = self._request_wrapper(
File "/home/morpheus/pancaketrade/.venv/lib/python3.9/site-packages/telegram/utils/request.py", line 279, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities: unsupported start tag "pancaketrade.network.bsc.network" at byte offset 43

Any guidance on what may be happening and how to resolve it? I already tried to run it in Docker and it also freezes after a while.
Greetings!

beeb commented

Hey there!

This is why I kinda stopped developing this project, Python is really not that great for this type of stuff. It looks like some memoization (in-memory caching) is not happening properly but I have no idea why.

I tried to re-install pancaketrade recently and I had to tinker a bunch with different dependencies version to make it run, and I still face issues sometimes.

I'm currently working on a much better version written in Rust but it's not ready for public eyes yet and it will take a long time to finish with my current schedule and free time.

What would be those versions of dependencies that work in a more stable way? Python 3.7, 3.8? Poetry? Docker? Or any other advice to make it work better?
Perhaps if it would restart or reload the dependencies every certain period of time it could be more stable. When I see that it is frozen, I restart it and it works normal again for a while.

beeb commented

I used Python 3.9, poetry 1.5.1 (had to erase the poetry.lock file and re-generate it). I pinned the python-telegram-bot version to ^13.11 and added a dependency on tzlocal = "<5.0" in the pyproject.toml

I already implemented the instructions you gave me and it keeps giving me an error. I'm trying to correct it using code interpreter, here are some of your suggestions:

  1. Cache Issue:
    The core problem seems to originate from the cachetools library used within the bsc.py file. The error suggests there's an issue accessing a key in the cache.
    Steps to Address the Cache Issue:
    A. Identify the problematic cache operations:
    Navigate to the bsc.py file, especially around the lines mentioned in the traceback.
    Look for operations where keys are being accessed or deleted from the cache.
    B. Handle missing cache keys:
    Whenever you're trying to access or remove a key from the cache, wrap the operation in a try-except block to catch potential KeyError exceptions.
    C. Review cache insertion logic:
    Ensure that when keys are added to the cache, they are being added correctly. A mismatch in the key when storing and retrieving could cause this issue.
    D. Consider cache expiration:
    If the cache has a TTL (Time-To-Live) or expiration mechanism, make sure that you're not trying to access a key that might have expired.
    E. Logging for diagnostics:
    It can be helpful to add logging statements around cache operations. This way, if the issue arises again, you can have more detailed information on what might be causing it.

  2. Error Handler Issue:
    The secondary problem arises when the bot tries to handle the initial error. The message it tries to send through the Telegram API contains entities that Telegram cannot parse.
    Steps to Address the Error Handler Issue:
    A. Inspect the error message format:
    Navigate to the part of the code where the bot sends error messages to Telegram. This is likely in your error handler function or method.
    Check the formatting of the message. Ensure that it doesn't contain any special characters or formatting that Telegram might not understand.
    B. Escape special characters:
    If the message contains any special characters (like <, >, &), make sure to escape them or remove them.
    C. Verify message length:
    Telegram has a maximum message length. If the error message is too long, consider truncating it or sending a more concise version.
    D. Test the error handler:
    Once you've made adjustments, intentionally cause an error (perhaps with a simple raise Exception("Test")) and ensure that the error handler sends the message correctly to Telegram without any issues.

Applying these fixes is beyond my knowledge, but maybe it will help you. In the meantime, I'm going to be studying Python so that one day I can develop my tools and/or be able to correct errors that arise.
It would be a shame if pancaketrade was flawed after all the work and usefulness it has.
Something I can think of that might work is to configure it so that when a cache miss occurs it will automatically restart and reload the data.
If you think that some of the ideas that I shared with you can be useful and you want to apply them, that would be great.
I will be here to help you in whatever.
If you need me to make a humble donation, just let me know.

Greetings!

beeb commented

That's a lot of chatGPT. The caching is handled by the cachetools library and I have no control over its logic. I can't try/catch the mechanism that is implemented in their decorator. I don't think this is a bug with pancaketrade to be honest. Try to upgrade the cachetools dependency maybe? The caching happens in-memory so there should not be a key in there that doesn't exist or something in that vein and all the checks happen in the decorator automatically, it's a "black box"

I understand, now the second issue is how pancaketrade responds to the error in the cache. On the one hand, it does not send you a TG notification that an error occurred, on the other, it does not seek to restart the dependencies or only cachetools. This would be important to implement and also trying to update cachetools would not be bad either

beeb commented

At the moment I cannot reproduce the issue so it's hard to test any solution. Like I said, I don't think I will update pancaketrade since I'm working on a replacement. It will take time for sure, and I might want to wait until the new alloy-rs replacement for ethers-rs comes out.

It's okay, I understand now. I'm currently running it with pyhton 3.9.17 without any pyenv and it definitely works better. Even so, I will be looking forward to the new model of the bot. Greetings and thanks from Argentina!