Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak Triggered by "Server replied with a wrong session ID" #4213

Open
3 tasks done
nbFZY opened this issue Sep 28, 2023 · 5 comments
Open
3 tasks done

Memory Leak Triggered by "Server replied with a wrong session ID" #4213

nbFZY opened this issue Sep 28, 2023 · 5 comments

Comments

@nbFZY
Copy link

nbFZY commented Sep 28, 2023

Code that causes the issue

def test(self):

    self.api_id = self.api_id_text.GetValue()
    self.api_hash = self.api_hash_text.GetValue()


    checked_sessions = [self.list_ctrl.GetItemText(i, 1)
                        for i in range(self.list_ctrl.GetItemCount())
                        if self.list_ctrl.IsItemChecked(i)]

    self.loop = asyncio.new_event_loop()
    asyncio.set_event_loop(self.loop)

    self.tasks = []
    for account in checked_sessions:
        task = self.loop.create_task(
            self.Group_sending(account, self.api_id, self.api_hash))
        self.tasks.append(task)

    self.future = asyncio.gather(*self.tasks)
    try:
        self.loop.run_until_complete(self.future)
    except asyncio.CancelledError:
        for task in asyncio.all_tasks(self.loop):
            task.cancel()
    except Exception as e:
        print(str(e))
    finally:
        self.loop.close()

async def Group_sending(self, account, api_id, api_hash):
    client = TelegramClient(account, api_id, api_hash)
    await client.start()

    while True:  
        await asyncio.sleep(10)  

Expected behavior

This is the reduced group messaging code that can run multiple accounts in parallel to execute the message-sending task. It used to run smoothly without any issues.

Actual behavior

Recently, a severe problem emerged. If an account has too many groups or too many messages, when logging in with about 10 such accounts simultaneously, the error 'Server replied with a wrong session ID' occurs. Along with this, memory leakage happens continuously until all the memory is filled up. I initially thought it was a problem with my code. However, even after I removed all operations and only retained the client creation and infinite delay loop, the problem persisted. After a few rounds of the loop, the 'Server replied with a wrong session ID' error would still pop up, leading to a memory leak. Through tests, I found out that when logging in with more new accounts in parallel (these accounts don't have many messages), the problem doesn't occur. I'm unsure why this issue happens and how to resolve it.

Traceback

No response

Telethon version

1.30.3

Python version

3.10

Operating system (including distribution name and version)

windows 10

Other details

No response

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.
@Lonami
Copy link
Member

Lonami commented Sep 29, 2023

leading to a memory leak

Are you able to narrow down what is leaking?

I can't think how this catch could lead to memory leaks:

except SecurityError as e:

All it's doing is retrying.

You can also try with multiple processes. Perhaps asyncio is interferring somehow.

@nbFZY
Copy link
Author

nbFZY commented Sep 29, 2023

导致内存泄漏

你能缩小泄漏的范围吗?

我无法想象这个捕获如何导致内存泄漏:

except SecurityError as e:

它所做的只是重试。

您还可以尝试使用多个进程。也许asyncio正在以某种方式进行干扰。

What puzzles me the most is that when I only log in with 5 accounts (each of these accounts has about dozens of groups, and there are a large number of messages in the groups at any time), everything is normal. However, when I log in with around 8-10 accounts, abnormalities begin. The memory starts to rise at a rate of several megabytes per second. I also receive the error "Server replied with a wrong session ID". I'm uncertain whether the memory leak caused the "Server replied with a wrong session ID" error, or if this error led to the memory leak.

I used objgraph to track the increase in objects in memory. When there are only 5 accounts, i.e., everything is normal, the data looks like this:

dict                       27524      +221
list                       11167      +161
PeerChannel                 2496       +70
Message                     2278       +67
PeerUser                    2222       +67
MessageReplies              2177       +67
UpdateNewChannelMessage     2227       +66
Username                    2715       +60
State                       2441       +48
Context                     5413       +43

However, when I re-log in with around 10 accounts, the data starts to skyrocket:

dict               419558    +14301
list               280089     +9812
Username            93332     +3577
User                74188     +2714
PeerChannel         73028     +2664
Channel             71783     +2576
UserProfilePhoto    65638     +2389
Message             65855     +2377
MessageReplies      63517     +2341
PeerUser            63957     +2312

Through testing, I limited the number of accounts logged in concurrently using semaphore, but it was of no use. Whether or not they are logged in concurrently, as soon as the total number of logged-in accounts reaches a certain amount, abnormalities will start to appear and memory will leak. Even if the accounts logged in earlier have executed "await client.disconnect()", and the accounts logged in later start to log in, the problem still occurs.

I have tested all the methods I can think of, but I still haven't found the problem. I finally believe it might be due to Telethon. When multiple accounts with a large number of messages are logged in, I don't know what processing Telethon has done to cause this problem.

@nbFZY
Copy link
Author

nbFZY commented Sep 29, 2023

Additionally, as shown in the code I provided earlier, after my accounts log in, they don't perform any operations and just enter an infinite delay loop. I wonder if Telethon can avoid processing any group messages on the logged-in accounts. Perhaps this could prevent the issues I'm encountering.

@xyco66
Copy link

xyco66 commented Nov 22, 2023

Additionally, as shown in the code I provided earlier, after my accounts log in, they don't perform any operations and just enter an infinite delay loop. I wonder if Telethon can avoid processing any group messages on the logged-in accounts. Perhaps this could prevent the issues I'm encountering.

My problem is the same as yours, have you solved it?

@Sevenzhi
Copy link

Mine is the same, has it been resolved?

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

No branches or pull requests

4 participants