Skip to content

Possible memory leak #816

@v-dev-cl

Description

@v-dev-cl

Using NestjS v10
and "telegram": "^2.26.22",

i'm fetching more than 100 channels data with their members and after some executions the "session._entities.table" keeps growing adding more items to the array (duplicated items)

my telegram client is like the example from the gramjs website being it a singleton used across more services on my Nestjs project, creating it only at the start of the service (OnModuleInit) and deleting it when shutting down the service/pod (OnModuleDestroy)

createTelegramClient = async () => {
    if (this.telegramClient) {
      this.logger.log(`[createTelegramClient] telegram client was already created`);
      return;
    }

    const telegramSession = this.configService.get('TELEGRAM_SESSION', '');
    const stringSession = new StringSession(telegramSession);

    const apiId = +this.configService.get<number>('TELEGRAM_API_ID');
    const apiHash = this.configService.get('TELEGRAM_API_HASH_ID');

    this.telegramClient = new TelegramClient(stringSession, apiId, apiHash, {
      connectionRetries: 5,
    });

    await this.telegramClient.start({
      phoneNumber: async () => await input.text('Please enter your number: '),
      password: async () => await input.text('Please enter your password: '),
      phoneCode: async () => await input.text('Please enter the code you received: '),
      onError: (err) => console.log(err),
    });

    this.telegramClientCreatedAt = Date.now();

    this.logger.log('You should now be connected.');
    this.logger.log(this.telegramClient.session.save());
    await this.telegramClient.sendMessage('me', {
      message: 'Logged! store the previous token and set it as env param',
    });

    return true;
  };

i'm executing those methods every 30 minutes

getChannels = async (username: string) => {
    const response = await this.telegramClient.invoke(
      new Api.channels.GetChannels({
        id: [username],
      }),
    );
    this.logger.log(JSON.stringify(response));

    return response;
  };

getFullChannel = async (username: string) => {
    const response = await this.telegramClient.invoke(
      new Api.channels.GetFullChannel({
        channel: username,
      }),
    );
    this.logger.log(JSON.stringify(response));

    return response;
  };

Heap dump
Image

Duplicated item
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions