hikari-py/hikari

Referenced message entries exceed the max messages settings

Closed this issue · 1 comments

Steps to reproduce

There's no steps but to run the bot as usual. Send a message, let's say it's message A. Then, reply to message A, this reply is message B. And finally, reply to message B.

Expected result

Cache.get_messages_view() returns a bit more than n messages if not exact, where n is the max messages we put in the config. Message B has the referenced_message attribute set to A.

Actual result

It returns a lot more messages than it should. Turned out, Cache._referenced_messages seems to be caching messages that should've been GC'd as they're no longer referenced. Message B has the referenced_message attribute set to None. This would lead to a memory leak if people were to reply a lot as it would leave some referenced messages untouched when garbage collecting.

System info

hikari (2.0.0.dev104) [HEAD]
located at /home/nori/.local/lib/python3.9/site-packages/hikari
CPython 3.9.7 GCC 11.1.0
Linux arch 5.14.16-zen1-1-zen #1 ZEN SMP PREEMPT Tue, 02 Nov 2021 22:23:00 +0000 x86_64

Further info

Might be somewhat relevant to #877 and perhaps #894 if we were to hide referenced messages?

Checklist

  • I have made sure to remove ANY sensitive information (bot token, passwords, credentials, personal details, etc.).
  • I have searched the issue tracker and have made sure it's not a duplicate. If it is a follow up of another issue, I have specified it.

Alright, so the problem was that Discord doesn't send second-level referenced_message. Meaning if we have B references to A and C references to B, B's referenced message will be None as opposed to A, thus leaving A's ref count to remain the same when garbage collecting. And since Discord provides you with the ID of the referenced message, this is what I do to get around currently https://github.com/norinorin/nokari/blob/31fd0078af81269204dd3370e932840c916cb03a/nokari/core/cache.py#L154-L162. I could make a PR if you'd like me to.

Jump link to relevant messages: https://discord.com/channels/574921006817476608/700378161526997003/910704551131246663