xtrime-ru / TelegramApiServer

Fast, simple, async php telegram api server: MadelineProto + Amp HTTP Server
https://tg.i-c-a.su
MIT License
534 stars 120 forks source link

Issues when session is removed #75

Closed agolaszewski closed 3 years ago

agolaszewski commented 3 years ago

Hello, I have a problem with session removal. When I remove session from the system by successfully calling : http://127.0.0.1:9503/system/removeSession?session=users/958f770f-8f01-40a0-827b-6730a8b77197 and then http://127.0.0.1:9503/system/unlinkSessionFile?session=users/958f770f-8f01-40a0-827b-6730a8b77197 After that I call http://127.0.0.1:9503/system/getSessionList I see no sessions, but the amount of allocated memory doesn't decrease

{
    "success": true,
    "errors": [],
    "response": {
        "sessions": [],
        "memory": "32 MB"
    }
}

what is odd, that after calling unlinkSessionFile the amount actually increase. Also I see that entries in db aren't deleted. In the logs I can spot this

2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_ReferenceDatabase_db; keys left: 0; keys removed: 0
[2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_MinDatabase_db; keys left: 0; keys removed: 0 
[2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_MTProto_chats; keys left: 0; keys removed: 0
[2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_MTProto_full_chats; keys left: 0; keys removed: 0
[2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_MTProto_channelParticipants; keys left: 0; keys removed: 0
[2021-07-07 15:00:37] [info] cache for table: tmp_533b83db386d8544_MTProto_usernames; keys left: 0; keys removed: 0
[2021-07-07 15:00:43] [notice] Waiting for exclusive lock of /app/sessions/users/958f770f-8f01-40a0-827b-6730a8b77197.madeline.safe.php.lock... 
[2021-07-07 15:00:43] [notice] Got exclusive lock of /app/sessions/users/958f770f-8f01-40a0-827b-6730a8b77197.madeline.safe.php.lock... 
[2021-07-07 15:00:43] [notice] Waiting for exclusive lock of /app/sessions/users/958f770f-8f01-40a0-827b-6730a8b77197.madeline.lightState.php.lock...
[2021-07-07 15:00:43] [notice] Got exclusive lock of /app/sessions/users/958f770f-8f01-40a0-827b-6730a8b77197.madeline.lightState.php.lock...
[2021-07-07 15:00:43] [notice] Saved session!

Executing over and over again, around every minute.

Here is a screen from sessions/users directory image

My docker compose:

telegram-api-server:
    image: xtrime/telegram-api-server:latest
    container_name: FWTL-telegram-api-server
    environment:
      WAIT_HOSTS: mysql:3306
    ports:
      - "127.0.0.1:9503:9503"
    volumes:
      - C:/Projects/FWTL/.env.docker:/app/sessions/.env.docker
    depends_on:
      - mysql
  mysql:
    image: mariadb:10.5
    container_name: FWTL-mysql
    ports:
      - "127.0.0.1:9507:3306"
    volumes:
      - FWTL-mysql:/var/lib/mysql
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'

and my env settings (I removed appId and secret just for public) https://gist.github.com/agolaszewski/cf3a1fdee78131e3ece2d10f2edeff29 Docker image version : https://hub.docker.com/layers/xtrime/telegram-api-server/latest/images/sha256-eb5400b87bbcd0bfc3348585795501c19a8ec84cb7ac0775e16e50ac98739676?context=explore

Any ideas what is wrong?

xtrime-ru commented 3 years ago

Hi! This is issue from core lib: madelineProto. Its running in backround threads even after unset of its instance. This causes memory leaks, and prevents garbage collector to clean memory. The only way i know to clean memory: stop container. I restart mine every 12 hours. If you want to remove session: logout, stop container, remove session files manually.

xtrime-ru commented 3 years ago

And i do not recomend to run multiple sessions in one container. Better override ENTRYPOINT and add more containers in docker-compose.yml. Run run one session per container. Because if one session breaks down badly it will affect whole script and other sessions will stop respond until reboot or even remove of broken session.

Healthcheck script can detect hangups and restart container (in case telegram servers went down, for example), but if one session gets permanent ban, or got corrupted reboot wont fix anything.

agolaszewski commented 3 years ago

Hey, thanks for advices. Yes I have noticed that sometimes when one session becomes corrupted the whole container is stopped and there is no other way then remove image and its volume to make it works again