From 8c4094026efc105f6c8b78742602fdc66e15c0ea Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Fri, 25 Oct 2024 14:14:59 -0400 Subject: [PATCH 1/6] callback doesnt wait for event loop --- packages/core/src/tools/create-lambda-handler.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index 0c69c98b7..7a486dde2 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -264,6 +264,13 @@ const createLambdaHandler = (appRawOrPath) => { const compiledApp = schemaTools.prepareApp(appRaw); const input = createInput(compiledApp, event, logger, logBuffer, rpc); + + // When the app is done, callback() will be called and the Lambda function will end. + // 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; + return app(input); }) .then((output) => { From d114e07dbfa4e82e8465e33a6069ca9798a9ee71 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Tue, 29 Oct 2024 13:57:26 -0400 Subject: [PATCH 2/6] Read val from event --- packages/core/src/tools/create-lambda-handler.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index 7a486dde2..6de6b8d21 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -189,7 +189,10 @@ const createLambdaHandler = (appRawOrPath) => { // Wait for all async events to complete before callback returns. // This is not strictly necessary since this is the default now when // using the callback; just putting it here to be explicit. - context.callbackWaitsForEmptyEventLoop = true; + // In some cases, the code hangs and never exits because the event loop is not + // empty. + context.callbackWaitsForEmptyEventLoop = + event.callbackExitsAfterApp || true; // replace native Promise with bluebird (works better with domains) if (!event.calledFromCli) { @@ -265,12 +268,6 @@ const createLambdaHandler = (appRawOrPath) => { const input = createInput(compiledApp, event, logger, logBuffer, rpc); - // When the app is done, callback() will be called and the Lambda function will end. - // 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; - return app(input); }) .then((output) => { From 9de740460216528596684b1ad5124152e37ca911 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 30 Oct 2024 14:50:06 -0400 Subject: [PATCH 3/6] change name of event to match backend --- packages/core/src/tools/create-lambda-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index 6de6b8d21..1acc0fcd9 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -192,7 +192,7 @@ const createLambdaHandler = (appRawOrPath) => { // In some cases, the code hangs and never exits because the event loop is not // empty. context.callbackWaitsForEmptyEventLoop = - event.callbackExitsAfterApp || true; + event.waitForAsyncBeforeExit ?? true; // replace native Promise with bluebird (works better with domains) if (!event.calledFromCli) { From d5e0defc8b76bf3f30cd949d296975bcb015a82f Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 30 Oct 2024 14:51:31 -0400 Subject: [PATCH 4/6] Better msg --- packages/core/src/tools/create-lambda-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index 1acc0fcd9..a2a115bff 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -190,7 +190,7 @@ const createLambdaHandler = (appRawOrPath) => { // This is not strictly necessary since this is the default now when // using the callback; just putting it here to be explicit. // In some cases, the code hangs and never exits because the event loop is not - // empty. + // empty, so we can override the default behavior and exit after the app is done. context.callbackWaitsForEmptyEventLoop = event.waitForAsyncBeforeExit ?? true; From 82a1ac1edc190af9ef6819ba3ae3fe5a31912934 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 30 Oct 2024 15:06:27 -0400 Subject: [PATCH 5/6] use opt out name --- packages/core/src/tools/create-lambda-handler.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index a2a115bff..3c3eb1988 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -191,8 +191,7 @@ const createLambdaHandler = (appRawOrPath) => { // using the callback; just putting it here to be explicit. // 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.waitForAsyncBeforeExit ?? true; + context.callbackWaitsForEmptyEventLoop = !event.skipWaitForAsync || true; // replace native Promise with bluebird (works better with domains) if (!event.calledFromCli) { From 6d687d7dd66d27b98dea199ad717cae8e9656d7f Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 30 Oct 2024 16:56:37 -0400 Subject: [PATCH 6/6] not always true --- packages/core/src/tools/create-lambda-handler.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/tools/create-lambda-handler.js b/packages/core/src/tools/create-lambda-handler.js index 3c3eb1988..0e695f932 100644 --- a/packages/core/src/tools/create-lambda-handler.js +++ b/packages/core/src/tools/create-lambda-handler.js @@ -191,7 +191,10 @@ const createLambdaHandler = (appRawOrPath) => { // using the callback; just putting it here to be explicit. // 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; + context.callbackWaitsForEmptyEventLoop = true; + if (event.skipWaitForAsync === true) { + context.callbackWaitsForEmptyEventLoop = false; + } // replace native Promise with bluebird (works better with domains) if (!event.calledFromCli) {