Recursive call to serverless function inconsistent in production (works fine in netlify-lamba)

I’ve built a light-weight queue management service using Google Firestore and Netlify’s serverless functions.

There’s a function called “queue_next.js” that checks whether we can place the next order, and if we can, it places an order. The last step should be calling itself so the queue continues to be processed. That last step – calling itself via Axios – is called asynchronously, but not using await so the current function can return and just spawn itself again.

When developing locally it’s working like a charm: an order gets placed, the queue processes the next job up, and calls itself at the last second right before returning the previous result. No timeout, no problem.

When deployed on Netlify in production, however, it’s very inconsistent. Sometime the recursive call makes it’s way through once or more times, sometimes it just stalls completely. There’s nothing in my logs that shows any issues and I’m struggling to figure it out.

Here’s a snippet of the code:

I’m wondering if – because I’m not suings await – that in some cases the function returns before that last moment call is sent, but I have no way of testing.

Any leads would be super helpful.

Thanks,
Eli

Hey there, @Resto

Thanks for writing in the Netlify Forums! Apologies that this thread remained quiet for a few days-- you brought up a great topic!

I know that Jen is assisting you in our Helpdesk, so please continue the conversation there :slight_smile:

Did you find a resolution to this? I’m having the exact same issue.

Ok, so to help anyone in future experiencing the same thing, I got around this by introducing a delay after making a recursive request.

In my application, I have a wrapper function that either calls a netlify function directly (in dev environment) or makes a fetch request to the function (in production). I now await this wrapper function, and add a delay after the fetch (which is not awaited), and lambda-lambda invocations now seem to be working. This is analogous to adding an await Promise.resolve() after the Axios.get(...) call in the OP’s example.

Hey there, @leesus :wave:

Thanks so much for reaching out and sharing that! Knowledge sharing is part of what make the Forums great, and we appreciate you setting other members up for success. :netliconfetti:

Is there any fixes to this? this still seems to be a problem and awaiting the response from one function to another is not going to work for our use-case.

We have two functions

Function A will run on a schedule (now using the new scheduled function)
It fetches 1-100 documents from a database
Then loops through those documents and calls function B with each data point.

Problem is, in production it seems like function B is called maybe 80-90% of the time.
In development it runs perfectly every single time.

Hey @p.aarseth,

I’d personally advise to use recursive calls from the front-end rather than doing it in the function. For example, take a look at this code (not the best code, but does the job):

https://github.com/Hrishikesh-K/netlify-file-browser/blob/main/src/index.vue#L656-L683

I’d say that’s more reliable.

If that doesn’t suit your use case, you can let us know the site name and possibly share the function code so we could check what could be happening.