aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Race condition with fast containers leads to timeouts

Open f3nry opened this issue 7 years ago • 3 comments

Description

I'm running sam local start-api, and then a subsequent test script that executes hundreds of requests against the lambda function. If I don't add a sleep in the startup of the lambda under cold start, I get Function 'func' timed out after 30 seconds.

Steps to reproduce

It seems like there's a race condition between when the container starts, is invoked, and when the "attach" command is run to attach to the stdout of the container.

Observed result

Functions randomly timeout in a CI environment, adding timeouts to the startup of the lambda executing resolves the flakiness.

Expected result

Functions complete without timeouts and report their results to the Flask API gateway shim.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Ubuntu
  2. sam --version: 0.15.0

f3nry avatar May 20 '19 02:05 f3nry

Is the usecase around load-testing? Could you elaborate a bit more on why the test scripts executes hundreds of requests?

sriram-mv avatar Jun 03 '19 20:06 sriram-mv

Also note that SAM CLI's local execution is not optimized for tons for parallel requests. We have made some design decisions that intentionally trade off performance for user experience because it is a development tool. We use Flask as a cheap mult-threaded web server which is not really the intended use case.

There could be two root causes:

  1. With hundreds of invokes, your Docker server might be coming to a grinding halt slowing down everything. I don't know much about Docker's scalability. Is it possible that Docker is unable to handle hundreds of running containers on the same host?

  2. Or, SAM CLI's was coming to a grinding halt and not responding as fast as Docker.

SAM CLI slowdown hypothesis From a quick glance at the code, I don't see how there can be a race. In fact if there was a race, I would expect SAM CLI to return immediately while the container is running. Based on the symptom, I think there is an overall slowdown of the Python process (may be due to GIL) to a point where the attach iterator did not get a chance to complete before the timer went off.

With hundreds of threads all spawning containers and reading sockets, I expect SAM CLI to be heavily IO-bound. Timers, which is a CPU bound operation, probably ran at the precise time and killed the before the IO bound stuff completed.

sanathkr avatar Jun 06 '19 20:06 sanathkr

I'd add to the chorus that this probably isn't a good use of sam local currently. We're continuously making performance improvements, including in some of the areas mentioned, so it may get better over time at this, but I'd recommend running against an actual live AWS Lambda deployment for parallel invocations.

Closing since the feedback is noted and this seems to be asked and answered, but feel free to reopen if you have more details you want to cover.

awood45 avatar Aug 06 '19 17:08 awood45