Fork me on GitHub
#holy-lambda
<
2021-11-04
>
steveb8n02:11:39

Q: is there any way to attach a shutdown hook fn in HL? i.e. when lambda is unloaded because it’s idle too long, I want a fn to run which disposes network connections

Karol Wójcik05:11:20

You mean something like: 1. When the lambda has handled the payload. 2. And there is no more payloads, but Lambda is alive. 3. Call the hook?

Karol Wójcik05:11:56

Or maybe something like: 1. Lambda called the user handler. 2. Lambda is waiting for the response. 3. It's close to timeout, so call the hook?

steveb8n07:11:25

neither. when the lambda is shutting down because it’s been idle for too long. then I want to be able to call a hook so I can clean up network connections held in an atom

Karol Wójcik07:11:56

This is not possible.

steveb8n07:11:01

would be useful for anyone wanting to see when their lambdas are shutdown since it could write to the logs

steveb8n07:11:45

I thought I read somewhere that it is possible but only with a custom runtime

Karol Wójcik07:11:51

Basically AWS Lambda does not provide any CPU or memory to the idle lambda. All the operations are stopped.

Karol Wójcik07:11:13

Otherwise the runtime loop white true would run forever

Karol Wójcik07:11:18

But it doesn't.

steveb8n07:11:24

and no events while shutting down?

steveb8n07:11:29

yeah, that makes sense

Karol Wójcik07:11:27

Hmm. Only the regular Linux shutdown hooks, but I don't know whether the code will be invoked. I can try.

steveb8n07:11:29

cool. I’m sure I’m not the only person who would like to use this if it’s possible

Karol Wójcik07:11:45

I will have some more time in few hours, so I will investigate it. 🙂

Karol Wójcik07:11:00

I'm reading this: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html. It seems that I would need to create an extension for this.

Karol Wójcik07:11:37

It looks like the runtime is killed immediately, while the extensions have 500ms to shutdown gracefully. ;O

Karol Wójcik07:11:10

Ok I do understand what to do, but be aware that you will pay additional $ for this.

steveb8n08:11:44

sure. it would only be for the extra cpu time of the hook fn right? these hooks should be very small/fast so that this cost is low

steveb8n08:11:43

even just an extra log line will be useful for most people because then they can see how quickly lambdas are unloaded i.e. when cold starts will be required again

steveb8n08:11:08

this will force me to upgrade HL 🙂

Karol Wójcik08:11:02

I'm not sure how this gonna work. I think after the shutdown-hook is called the AWS will not wait? Or maybe I should manually exit the process.

Karol Wójcik08:11:14

> sure. it would only be for the extra cpu time of the hook fn right? these hooks should be very small/fast so that this cost is low Yeah

Karol Wójcik08:11:20

Cold start would be a little bit higher, since I have to register the internal extension.

Karol Wójcik08:11:38

Probably few ms, when shutdown-hook is used

steveb8n20:11:22

It would be optional? If so, we can choose the small trade-off

Karol Wójcik21:11:50

Yeah. It will be optional.

steveb8n21:11:50

cool. they are small tradeoffs for this benefit

Karol Wójcik13:11:06

Shutdown event is not supported via internal extension. If I create an external extension still you would not be able to access the resources, since the extension runs in a separate process.

{:headers {nil #object[java.util.Collections$UnmodifiableRandomAccessList 0x176ce308 [HTTP/1.1 403 Forbidden]], Date #object[java.util.Collections$UnmodifiableRandomAccessList 0x3ce7b033 [Fri, 05 Nov 2021 13:29:08 GMT]], Content-Length #object[java.util.Collections$UnmodifiableRandomAccessList 0x1ccbf9ac [116]], Content-Type #object[java.util.Collections$UnmodifiableRandomAccessList 0xd4b0649 [application/json]]}, :success? false, :status 403, :body {:errorMessage SHUTDOWN: ShutdownEventNotSupportedForInternalExtension, :errorType Extension.InvalidEventType}}

steveb8n00:11:26

ok. not possible at all then?

Karol Wójcik10:11:26

Nope. Only via external extension that run in the different process.

Karol Wójcik10:11:34

But this is not something I want to do

steveb8n22:11:31

thanks for looking into it

steveb8n23:11:24

my use-case for this was cleaning up Redis connections but I’ve discovered (through load testing) that they are cleaned up automatically as the lambdas are unloaded

steveb8n23:11:42

so don’t really need to do it in a shutdown hook

Karol Wójcik06:11:03

Cool. Also great you’re doing load testing. You prove how HL is a stable runtime