feral icon indicating copy to clipboard operation
feral copied to clipboard

Properly finalize resources on Lambda shutdown

Open armanbilge opened this issue 4 years ago • 0 comments

Spinning @bpholt's comment out of https://github.com/typelevel/feral/issues/134#issuecomment-1003730364.

Currently, we don't run the finalizer for the Resource acquired at the Lambda's startup, which is probably okay in many instances but not ideal. Knowing when to run this finalizer is difficult without some notification/callback from the Lambda environment, and also being explicitly granted some time/backpressure to complete such finalization.

There does seem to be a Lambda Extensions API that supports a shutdown hook, but it's not obvious to me how that would help us inside the actual Lambda.

Shutdown: This phase is triggered if the Lambda function does not receive any invocations for a period of time. In the Shutdown phase, Lambda shuts down the runtime, alerts the extensions to let them stop cleanly, and then removes the environment. Lambda sends a Shutdown event to each extension, which tells the extension that the environment is about to be shut down.

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html

I also recall someone mentioning that running these finalizers can be time-consuming (i.e. expensive) and doesn't make sense in many cases. So, something to chew on.

armanbilge avatar Jan 02 '22 16:01 armanbilge