Home
Support Forums

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: