Skip to content

πŸ› Bug Report: Appwrite freezes randomly on function executionΒ #4626

Closed as not planned

Description

@netapy

πŸ‘Ÿ Reproduction steps

I have an Appwrite server on decent hardware (2 core CPU and 4 gigs of RAM).
There are many collections in my database and I use functions to retrieve and edit data before sending it to the client β€” functions are called very often.

I still haven’t figured out why/when exactly but at some point the server doesn’t serve functions, I can requests accumulating on the functions logs :
(this get_feed function only queries data, compacts it and send it back to the client)
IMG_0416

At first the backend interface is still responsive but at some point other functions freeze and i’m unable to load pages or query anything.

The appwrite doctor doesn’t show any issues :
IMG_0417

Thanks

πŸ‘ Expected behavior

It should not crash as the functions don’t require a lot of memory, and runs without any issues during my stress tests.

πŸ‘Ž Actual Behavior

Appwrite freezes for a while and then comes back after 15-20 minutes.

🎲 Appwrite version

Version 1.0.x

πŸ’» Operating system

Linux

🧱 Your Environment

Only pure Appwrite is running on the server.

πŸ‘€ Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏒 Have you read the Code of Conduct?

Activity

  1. added
    bug
    on Nov 2, 2022
  2. stnguyen90 commented on Nov 2, 2022

    @stnguyen90
    Contributor

    @netapy thanks for raising this issue! πŸ™πŸΌ Would you please provide the executor logs and check docker ps -a to see if the runtime container is still running? If it is, the logs from the runtime container can be helpful too.

  3. added
    product / functions
    on Nov 2, 2022
  4. netapy commented on Nov 2, 2022

    @netapy
    Author

    Sure, as of writing this message the issue is ongoing and this is the log :

    TAINER ID   IMAGE                        COMMAND                  CREATED          STATUS                  PORTS                                                                      NAMES
    c7140174e711   openruntimes/python:v2-3.9   "/usr/local/src/star…"   17 minutes ago   Up 17 minutes           3000/tcp                                                                   62e0fcfca3b3c5124f16-633d37fa36cb75b79045
    af36e0d71f8e   openruntimes/python:v2-3.9   "/usr/local/src/star…"   39 minutes ago   Up 39 minutes           3000/tcp                                                                   62e0fcfca3b3c5124f16-634d2fe7204a7c470294
    8150a559904b   openruntimes/python:v2-3.9   "/usr/local/src/star…"   4 hours ago      Up 4 hours              3000/tcp                                                                   62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    f513055af40d   appwrite/appwrite:1.0.3      "worker-functions"       4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-functions
    1a3d67b1302a   traefik:2.7                  "/entrypoint.sh --pr…"   4 hours ago      Up 4 hours              0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   appwrite-traefik
    49db3fb1a079   appwrite/appwrite:1.0.3      "executor"               4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-executor
    ada529598a25   appwrite/appwrite:1.0.3      "realtime"               4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-realtime
    cf3ba23161dc   appwrite/appwrite:1.0.3      "maintenance"            4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-maintenance
    f2efb28da48b   appwrite/appwrite:1.0.3      "worker-webhooks"        4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-webhooks
    1a5f0181a33a   appwrite/appwrite:1.0.3      "worker-databases"       4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-databases
    d2ec773125d8   appwrite/appwrite:1.0.3      "worker-mails"           4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-mails
    2f219a79b78b   appwrite/appwrite:1.0.3      "worker-builds"          4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-builds
    c1c41ef43feb   appwrite/appwrite:1.0.3      "schedule"               4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-schedule
    bdd9ea078789   appwrite/appwrite:1.0.3      "worker-certificates"    4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-certificates
    7629d292ce88   appwrite/appwrite:1.0.3      "worker-deletes"         4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-deletes
    b1e21b3fa110   appwrite/appwrite:1.0.3      "worker-audits"          4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-audits
    b2c5a6652e70   appwrite/appwrite:1.0.3      "usage --type=timese…"   4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-usage-timeseries
    254e19f06e4b   appwrite/appwrite:1.0.3      "usage --type=databa…"   4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-usage-database
    43dbf896b882   appwrite/appwrite:1.0.3      "docker-php-entrypoi…"   4 hours ago      Up 4 hours              80/tcp                                                                     appwrite
    a8709093f11a   appwrite/appwrite:1.0.3      "worker-messaging"       4 hours ago      Up 4 hours              80/tcp                                                                     appwrite-worker-messaging
    5e8a0ec85c12   redis:7.0.4-alpine           "docker-entrypoint.s…"   4 hours ago      Up 4 hours              6379/tcp                                                                   appwrite-redis
    c1ff63fd3336   mariadb:10.7                 "docker-entrypoint.s…"   4 hours ago      Up 4 hours              3306/tcp                                                                   appwrite-mariadb
    16bb528d243a   appwrite/influxdb:1.5.0      "/entrypoint.sh infl…"   4 hours ago      Up 4 hours              8086/tcp                                                                   appwrite-influxdb
    96701ceb4b35   appwrite/telegraf:1.4.0      "/entrypoint.sh tele…"   4 hours ago      Up 4 hours              8092/udp, 8125/udp, 8094/tcp                                               appwrite-telegraf
    dc8df12ede3b   hello-world                  "/hello"                 5 days ago       Exited (0) 5 days ago                                                                              cranky_kepler
    
    
  5. netapy commented on Nov 2, 2022

    @netapy
    Author

    And this is the executor log :

    Function executed in 0.11850380897522 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.044380903244019 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.11296200752258 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.035964012145996 seconds, status: completed
    [Error] Type: Exception
    [Error] Message: Runtime not found. Please create the runtime.
    [Error] File: /usr/src/code/app/executor.php
    [Error] Line: 469
    Building container : 62e0fcfca3b3c5124f16-633d37fa36cb75b79045
    Build Stage completed in 1 seconds
    Executing Runtime: 62e0fcfca3b3c5124f16-633d37fa36cb75b79045
    [Error] Type: Exception
    [Error] Message: An internal curl error has occurred within the executor! Error Msg: Connection refused
    [Error] File: /usr/src/code/app/executor.php
    [Error] Line: 541
    Executing Runtime: 62e0fcfca3b3c5124f16-633d37fa36cb75b79045
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.12101793289185 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.04660701751709 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.11787605285645 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.040542840957642 seconds, status: completed
    [Error] Type: Exception
    [Error] Message: An internal curl error has occurred within the executor! Error Msg: Operation timed out
    [Error] File: /usr/src/code/app/executor.php
    [Error] Line: 544
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.18129801750183 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.22170782089233 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.088666915893555 seconds, status: completed
    Function executed in 0.26568603515625 seconds, status: completed
    Executing Runtime: 62e0fcfca3b3c5124f16-6347cbfd4d6a53ef838e
    Function executed in 0.18561196327209 seconds, status: complet
    

    edit : how can I get the log from the runtime container ?

  6. stnguyen90 commented on Nov 2, 2022

    @stnguyen90
    Contributor

    @netapy, docker logs <container name or id>. The container should be named <project id>-<deployment id>. Are these for when the function was frozen? What was the deployment ID for the frozen function?

  7. netapy commented on Nov 2, 2022

    @netapy
    Author

    Yes these logs were taken when the function was frozen.

    It’s weird my containers are able to display logs, except for the two python openruntimes containers.
    IMG_0422

  8. stnguyen90 commented on Nov 2, 2022

    @stnguyen90
    Contributor

    @netapy, which deployment ID is the frozen one?

  9. netapy commented on Nov 2, 2022

    @netapy
    Author

    @netapy, which deployment ID is the frozen one?

    Usually the one ending with 838e freezes first, but then the freeze generalizes to the whole appwrite application.
    Also it recovers by itself after 15-20 minutes.

  10. stnguyen90 commented on Nov 2, 2022

    @stnguyen90
    Contributor

    @netapy is this get_feed function being executing asynchronously or synchronously?

  11. self-assigned this
    on Nov 2, 2022
  12. netapy commented on Nov 2, 2022

    @netapy
    Author

    @netapy is this get_feed function being executing asynchronously or synchronously?

    β€œget_feed” is launched synchronously, as are all my functions.

  13. Shiba-Kar commented on Nov 3, 2022

    @Shiba-Kar

    Message: An internal curl error has occurred within the executor! Error Msg: Operation timed out
    check your variables for the function APPWRITE_FUNCTION_ENDPOINT http://<domain>.com/v1 you must have forgotten v1

  14. netapy commented on Nov 3, 2022

    @netapy
    Author

    Message: An internal curl error has occurred within the executor! Error Msg: Operation timed out
    check your variables for the function APPWRITE_FUNCTION_ENDPOINT http://<domain>.com/v1 you must have forgotten v1

    Thanks for your answer. Unfortunately I re-checked my code and it includes the right variables β€” my functions run correctly until some point in time (which I haven’t determined yet).

    Not sure if this helps but when I docker compose down it seems that my appwrite_runtimes container has trouble shutting down. Logs below :

    $ sudo docker compose down 
    [+] Running 23/24
     β Ώ Container appwrite-worker-certificates  Removed                                                                                                           11.8s
     β Ώ Container appwrite-worker-deletes       Removed                                                                                                           11.7s
     β Ώ Container appwrite-usage-timeseries     Removed                                                                                                           11.1s
     β Ώ Container appwrite-realtime             Removed                                                                                                           11.2s
     β Ώ Container appwrite-worker-builds        Removed                                                                                                           11.7s
     β Ώ Container appwrite-maintenance          Removed                                                                                                           11.7s
     β Ώ Container appwrite-worker-mails         Removed                                                                                                           11.8s
     β Ώ Container appwrite-schedule             Removed                                                                                                           10.5s
     β Ώ Container appwrite-worker-audits        Removed                                                                                                           11.5s
     β Ώ Container appwrite-worker-messaging     Removed                                                                                                           11.2s
     β Ώ Container appwrite-usage-database       Removed                                                                                                           11.8s
     β Ώ Container appwrite-worker-databases     Removed                                                                                                           11.2s
     β Ώ Container appwrite-traefik              Removed                                                                                                            0.7s
     β Ώ Container appwrite-worker-webhooks      Removed                                                                                                           11.4s
     β Ώ Container appwrite-telegraf             Removed                                                                                                            0.4s
     β Ώ Container appwrite-worker-functions     Removed                                                                                                           11.8s
     β Ώ Container appwrite-executor             Removed                                                                                                           10.3s
     β Ώ Container appwrite                      Removed                                                                                                            0.2s
     β Ώ Container appwrite-mariadb              Removed                                                                                                            0.5s
     β Ώ Container appwrite-redis                Removed                                                                                                            1.1s
     β Ώ Container appwrite-influxdb             Removed                                                                                                            0.2s
     β Ώ Network appwrite_appwrite               Removed                                                                                                            0.1s
     β Ώ Network appwrite_gateway                Removed                                                                                                            0.2s
     β Ώ Network appwrite_runtimes               Error                                                                                                              0.0s
    failed to remove network appwrite_runtimes: Error response from daemon: error while removing network: network appwrite_runtimes id a066910e107af1054e6f805b6397f09791183321b302fda52005b78b3d22b565 has active endpoints
    
  15. Shiba-Kar commented on Nov 3, 2022

    @Shiba-Kar

    docker kill appwrite_runtimes

  16. 14 remaining items

  17. redarflap commented on Dec 4, 2022

    @redarflap

    @netapy
    Are you by any chance running a docker version installed through snapd on Ubuntu?

    After lots of testing, i noticed, that the functions are far less likely to get stuck on my Docker Desktop Windows environment.
    So I tested different things and finally I replaced the snapd version with the version from the official Docker repo, while copying all my volumes over and it seems much more stable now.
    When spamming a simple function 10.000 times (10 parallel), it still fails between 0-50 times. But it seems that now at least it takes much more effort to pull down the whole system.

    I'm running Ubuntu 22.04 LTS in a VM with 4 cores/4Gb.
    Before: Docker installed via OS installer/snap (20.10.17)
    Now: Docker installed using Docker repo (20.10.21)

    Some other interesting things that i noticed..
    I got two functions

    • testFunction, that creates one simple document in the DB (called 10.000 times)
    • monitoring function which gets health data and collection info every 30 seconds

    1.) When spamming the testFunction too hard (like 20 in parallel), often it is the monitoring function that gets completely stuck, and fails to recover indefinitely, while the testFunction is still able to create Documents.
    Only after waiting some minutes and starting calling the testFunction again (or occasionally even refreshing the web console) the monitoring function suddenly starts working again.

    2.) Functions that don't access the appwrite API don't seem to be affected by those problems at all.

    So to me it seems like the problem (at least in my case) lies somewhere between the runtime container and the appwrite API, which causes it to randomly get stuck if stressed too much.
    And it recovers most of the time, but not always.
    Also it can still work for one function, but not for another one.
    Plus, causing another unrelated call on the API, sometimes gets everything running again.

  18. redarflap commented on Dec 4, 2022

    @redarflap

    Another thing i noticed just now:
    Increasing _APP_WORKER_PER_CORE also seems to help quite a bit to get rid of this.
    I set it from 6 to 12 and now i get much less failed executions, despite spamming 15 parallel executions.

    Well, unfortunately it's still happening from time to time, even under less load. I'm out of ideas...

    grafik

  19. netapy commented on Dec 5, 2022

    @netapy
    Author

    @redarflap thanks for your input.
    Yes I am running my server on Ubuntu 22.04LTS, but my Docker always was from the official repository..

    I have found that when I decreased the function timeout somewhere between 5-10 seconds, my server would not crash anymore, only the function(s).

    I tried out the solution with the _APP_WORKER_PER_CORE but it doesn't seem to fix it, I still get random functions that don't launch, rendering the following error : An internal curl error has occurred within the executor! Error Msg: Operation timed out

    On the logs of the executor container, I get the following :

    Executing Runtime: 62e0fcfca3b3c5124f16-638dd2d795d639e0cacd
    [Error] Type: Exception
    [Error] Message: An internal curl error has occurred within the executor! Error Msg: Operation timed out
    [Error] File: /usr/src/code/app/executor.php
    [Error] Line: 544
    

    Are your error logs on the Appwrite executor the same as mine ?

    I also noticed that only the functions that work with the Appwrite API get this kind of error/freeze.
    I tried rewriting my functions in Node JS (previously Python) but that doesn't change a thing.

    A similar issue is opened I believe : #2940

  20. TheDarkX commented on Jan 3, 2023

    @TheDarkX

    image

    Intermittent.
    Appwrite version: v:0.15.2.402
    Windows WSL 2
    Docker Version: 4.10.1 (82475)
    Server spec: 4cores 8gb rams
    Function runtime: Node16
    No logs from runtime container

    docker compose logs appwrite-executor
    image

    Based on users above commented, looks like this is still occurring on latest version.
    Upgrading my Appwrite version to latest might have minor chances being fixed.
    but I'll try.

    This is a function that aggregate and generate an end day report which have dozen of loops.

    image

    Noticed that after continuous 4-5 execution in a row, the function failed to perform and resulted Operation Timed Out error.
    Also checked the x-ratelimit-limit = 60 & x-ratelimit-remaining = 54, nothing seems related to rate limit.

    @eldadfux apologize for the ping.

    Appwrite Cloud Function is a crucial feature.
    Hope someone look into this seriously.
    I will deep dig the mentioned .php line error when leisure.

    Edit: 4 Jan 2023
    tried docker compose down && docker compose up -d
    and trigger the function execution continuously 10 times + until it Timed Out, maybe redis cache issue.

  21. stnguyen90 commented on Jan 6, 2023

    @stnguyen90
    Contributor

    @TheDarkX, I tried creating dozens of executions of:

    const sdk = require("node-appwrite");
    
    function sleep(ms) {
      return new Promise((resolve) => {
        setTimeout(resolve, ms);
      });
    }
    
    module.exports = async function (req, res) {
      const client = new sdk.Client();
    
      if (
        !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
        !req.variables['APPWRITE_FUNCTION_API_KEY']
      ) {
        console.warn("Environment variables are not set. Function cannot use Appwrite SDK.");
      } else {
        client
          .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
          .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
          .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
          .setSelfSigned(true);
      }
    
      await sleep(200);
    
      res.json({
        areDevelopersAwesome: true,
      });
    };

    but they all executed fine:

    image

    Is there some promise in your code that isn't resolving? Perhaps you could try adding console.stdlog() (yes, stdlog) and check the runtime container logs?

  22. garvitomer commented on Feb 2, 2023

    @garvitomer

    Hi,

    I am facing the same issue in production now. Is there any temporary work around for this?

  23. stnguyen90 commented on May 12, 2023

    @stnguyen90
    Contributor

    just in case, make sure to add try/except so you catch all exceptions and call res.json or res.send at least once before the function exits.

    Alright I will do that.

    @netapy, are you still experiencing the problem after adding the try/except?

  24. netapy commented on May 15, 2023

    @netapy
    Author

    @netapy, are you still experiencing the problem after adding the try/except?

    @stnguyen90
    Should have came back to share it but it seems that I kind of solved my problem by setting the _APP_FUNCTIONS_INACTIVE_THRESHOLD appwrite env variable to a month or so.

    The constant restart of the function apparently caused the crashes.
    Not sure about it though but it seems more stable since then.

  25. pomarec commented on May 24, 2023

    @pomarec
    Contributor

    It does not look like an issue you should close to me. Is there a fix I missed ?

    FYI, we had to completely switch to a docker based node backend to satisfy our client as the cloud functions were not reliable.
    Tweaking _APP_FUNCTIONS_INACTIVE_THRESHOLD might be a workaround but it also has its reason to exist and to be used.

  26. byawitz commented on May 31, 2023

    @byawitz
    Contributor

    Hey @stnguyen90 , found a way to reproduce it with a clear use-cases. And, I think maybe there is a solution to overcome it.
    Is there any option to reopen the issue and get it reassigned?

    Thanks

  27. dokind commented on Nov 2, 2023

    @dokind

    _APP_FUNCTIONS_INACTIVE_THRESHOLD to how much ?

  28. stnguyen90 commented on Aug 21, 2024

    @stnguyen90
    Contributor

    Will use #5629 to track this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugproduct / functions

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions