Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core) - PDE-5490 calling lambda callback doesnt wait for event loop #904

Merged
merged 6 commits into from
Oct 31, 2024

Conversation

standielpls
Copy link
Contributor

@standielpls standielpls commented Oct 25, 2024

In debugging some apps, it seems like sometimes lambdas may be hanging indefinitely until the timeout expires.
This causes the action to fail, even if all the app code has completed.
Debugging further it looks like it's possible that when processing larger datasets, some open connections will be kept open and block the event loop from clearing.

This heuristic is something we can add from Lambda
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
the gist is, if we set context.callbackWaitsForEmptyEventLoop = false; then once the callback is called, we exit, despite having other connections

https://zapierorg.atlassian.net/browse/PDE-5490

@standielpls standielpls requested a review from a team as a code owner October 25, 2024 18:16
// In some cases, perhaps due to fetching large data blobs, the Lambda function
// will hang waiting for the event loop to drain. Setting this to false will
// prevent that and return when the callback is called.
context.callbackWaitsForEmptyEventLoop = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I'm a bit nervous about enabling this by default. It seems like a good default for Lambda to wait until the event loop is empty before returning. Otherwise, I imagine we can introduce unpredictability that's quite hard to debug since some async tasks will complete sometimes and not other times.

We're actually explicitly setting this to true a bit above, here.

What about enabling this per-app to start with? At this point in the code we have access to the event payload from Lambda in which we could provide app-specific flags.

context.callbackWaitsForEmptyEventLoop = true;
// In some cases, the code hangs and never exits because the event loop is not
// empty, so we can override the default behavior and exit after the app is done.
context.callbackWaitsForEmptyEventLoop = !event.skipWaitForAsync || true;
Copy link
Member

@rnegron rnegron Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@standielpls I think this is always true, no? false || true === true, undefined || true === true.

Suggested change
context.callbackWaitsForEmptyEventLoop = !event.skipWaitForAsync || true;
context.callbackWaitsForEmptyEventLoop = true;
if (event.skipWaitForAsync === true) {
context.callbackWaitsForEmptyEventLoop = false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: event.skipWaitForAsync !== true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, yes you're right!

@standielpls standielpls merged commit 12a8699 into main Oct 31, 2024
14 checks passed
@standielpls standielpls deleted the pde-5490/exit-on-callback branch October 31, 2024 13:03
eliangcs added a commit that referenced this pull request Nov 1, 2024
rnegron pushed a commit that referenced this pull request Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants