I have a very simple function (py3.6) that I'm running on sam-local
via start-api
that I'm testing with a shell script full of curl
posts:
lambda_handler(event, context):
try:
json_body = event["body"]
if not validate_json(json_body):
raise Exception("Bad JSON")
post_to_dynamo_db_local(json_body)
except Exception as err:
return {
'statusCode': 999,
'headers': {"x-custom-header": "my custom header value"},
'body': "\nException! " + str(err)
}
return {
'statusCode': 200,
'headers': {"x-custom-header": "my custom header value"},
'body': "hello world"
}
The test works fine in the correct use-case about 95% of the time, but fails (inconsistently) when validate_json
raises an Exception
despite being caught, or when post_to_dynamo_db_local
shorts and returns early (object already in DB, return).
*I say correct use-case works about 95% of the time, because sometimes I touch stuff and it fails, but me touching stuff is likely the cause. Unconfirmed though!
The function appears to return properly, according to the sam-local
console:
START RequestId: 8c7cf7ce-2926-4ce4-ba3c-0fac95a810b0 Version: $LATEST
END RequestId: 8c7cf7ce-2926-4ce4-ba3c-0fac95a810b0
REPORT RequestId: 8c7cf7ce-2926-4ce4-ba3c-0fac95a810b0 Duration: 162 ms
Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 24 MB
But the curl
doesn't receive a response until the request times out:
{ "message": "Internal server error" }
and around the same time the sam-local
console responds:
Error invoking python 3.6 runtime: io: read/write on closed pipe
Any ideas? The function run-time seems abnormally high for such a simple task, and I don't think memory should be an issue here...
Failing case flow:
Start -> check input(string) -> raise exception -> catch exception -> return response(string)
ADDITION: A couple things that help:
- Sleeping between curls (tried up to 10 seconds, but still errors occasionally
- Not caching references to tables (so they should be collected fairly quickly now