CodeGenieApp/serverless-express

Potential Issue with getCurrentInvoke handling concurrent Lambda invocations

corentindesfarges opened this issue · 1 comments

Description

I am considering using the @codegenie/serverless-express package in a NestJS application, specifically the getCurrentInvoke function to access the Lambda event and context.

My controller's code looks like this:

const { getCurrentInvoke } = require('@codegenie/serverless-express')
...
const currentInvoke = getCurrentInvoke()
const context = currentInvoke.event.requestContext

However, I came across a concern regarding the implementation of getCurrentInvoke. It appears that this function might not be handling concurrent Lambda invocations correctly.

const currentInvoke = {}

function getCurrentInvoke () {
  return currentInvoke
}

function setCurrentInvoke ({ event, context }) {
  currentInvoke.event = event
  currentInvoke.context = context
}

Lambda can reuse the same instance to serve multiple requests concurrently, but getCurrentInvoke does not seem to account for this, which could lead to incorrect event and context data being accessed across different invocations.

Potential Impact

This issue could result in incorrect handling of concurrent requests within the same Lambda instance, leading to data inconsistencies and unexpected behavior in the application.

Suggested Action

I would appreciate any insights or suggestions on how to address this issue. If there are any workarounds or plans to update the implementation of getCurrentInvoke to handle concurrent invocations properly, that information would be very helpful.

Hi @corentindesfarges each Lambda container will only be handling 1 event at a time. If another request comes in while one is still in flight, it will use a separate Lambda container.