Skip to content

Instantly share code, notes, and snippets.

@whyvez
Created August 21, 2015 17:35
Show Gist options
  • Select an option

  • Save whyvez/9907f4bcc63d54ab0279 to your computer and use it in GitHub Desktop.

Select an option

Save whyvez/9907f4bcc63d54ab0279 to your computer and use it in GitHub Desktop.
lambda container reuse state issue
/*
The current container reuse strategy in AWS Lambda makes it very hard to ensure consistent environments between invocations.
Managing environments is currently the responsibility of the user. There should be some additional cleanup tasks ran after
an invocation to ensure at least a freah new nodejs context. As an example I added the setInterval issue we've seen.
*/
// new container a
// * invocation 1 on container a
// - using setInterval to check remaining time every 1 sec
// - the handler will call context.success() after 45 sec without clearing setInterval
// * invocation 2 on container a
// - if the container is reused the setInterval will fire immediately on invocation and call context.fail prematurely
exports.handler = function (event, context) {
setInterval(function () {
// around 55 seconds if we haven't finished cleanup and exit
if (context.getRemainingTimeInMillis() < 5000) {
// we do /tmp cleanup and child process cleanup here.
// i.e. rm -rf /tmp/pg-import & killall gawk
// invocation a2 exits prematurely here
context.fail('we hit timeout');
}
}, 1000);
setTimeout(function () {
// invocation a1 exits here
context.success();
}, 45000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment