C
C#9mo ago
SiloCitizen3

✅ async/await Task in an AWS Lambda

Given the following code
var someWorkTasks = Task.Run(async () =>
{
....
});
var someWorkTasks = Task.Run(async () =>
{
....
});
without explicitly doing await someWorkTasks; it is possible that the lambda execution will get cut off/finish before that has a change to complete, right? I'm not insane? I need to explicitly await someWorkTasks; to ensure everything inside it finished, right?
13 Replies
JakenVeina
JakenVeina9mo ago
my intuition says "yeah", but I don't really know let's see.... are you asking because AWS Lambdas can't be async? looking at the docs, I think that's possibly true? nope, nevermind, there it is okay, here we go, the docs explicitly state that this is the case the lambda is subject to teardown as soon as it returns or as soon as its returned Task completes so, if you have something within that's not being awaited, that can definitely get terminated before it finishes
JakenVeina
JakenVeina9mo ago
near the bottom of the page
If you create an async Lambda function without implementing the await operator, .NET will issue a compiler warning and you will observe unexpected behavior. For example, some async actions will run while others won't. Or some async actions won't complete before the function invocation completes.
are you just curious, or is there some reason why you want to not await something? are you maybe trying to run some background work, where it keeps going after the lambda returns a basic acceptance result?
Tvde1
Tvde19mo ago
Yeah you have to await it somewhere
SiloCitizen3
SiloCitizen39mo ago
so, inside the block, there's various webservices and records being created in different places, along with logging, as things are happening starting XYZ.... dbContext insert record finished XYZ... starting ABC... webservice called finsihed ABC... and at times, all the data and accompanying log lines are there. other times, it doesnt. there's no errors being thrown, because they're handled. what I am notificing is the timestamps. in the cases where all the expected stuff isn't there, the overall execution of the log entries is getting to be over 1min. in the cases where all the expected stuff IS there, the execution duration is much shorter
JakenVeina
JakenVeina9mo ago
well, that could certainly be explained by you not awaiting something
SiloCitizen3
SiloCitizen39mo ago
this is after a stupid switch from elastic beanstalk over to lambdas. in EB, these issues were never happening, because the damn process was always "alive" in lambda, it's finish quickly or it gets killed off
JakenVeina
JakenVeina9mo ago
it's an entirely different issue if AWS is killing your stuff prematurely, because it takes too long
SiloCitizen3
SiloCitizen39mo ago
essentially stuffed the entire damn api website into a lambda and now all sorts of issues are cropping up. and that's the only thing I can think of, because after repeated tests on my machine, everything works
JakenVeina
JakenVeina9mo ago
according to docs, the timeout for a lambda to run is configurable by you, up to 15 minutes
SiloCitizen3
SiloCitizen39mo ago
right, but if i'm not awaiting someWorkTasks i dont think lambda is aware that there's still some shit taking place in the background, right? like that's what im left with, after many many tests on my local machine
JakenVeina
JakenVeina9mo ago
yes, if you're not awaiting something, that would explain it and could also explain you not seeing the issue locally
SiloCitizen3
SiloCitizen39mo ago
awesome. thank you guys for chiming in and confirming my sanity.