Zappa: Random 'NoneType' object is not callable
Randomly my site will go down (perhaps once or twice a month) and display the following:
{
“message”: “An uncaught exception happened while servicing this request. You can investigate this with the zappa tail command.”
}
The logs say the following:
[1492620032065] 'NoneType' object is not callable
[1492620147882] 'NoneType' object is not callable
[1492620330874] 'NoneType' object is not callable
etc. I don’t see any further information in the logs that would be helpful. Is there a way I can get more detailed information about the error? It only happens about once a month or less. When it get into this state, the only thing I found has fixed the problem is redeploying the code.
Other side notes: My Django project uses Amazon RDS. I can see in the graph that during this time, the DB connections goes to 0. And then back up once I have redeployed the code.
Possible Fix
Re-deploying my lambda code fixes the issue.
Steps to Reproduce
I haven’t been able to reproduce it consistently, it just will happen randomly and won’t be fixed until I redeploy my code.
Your Environment
- Zappa version used: 0.39.0
- Operating System and Python version: Ubuntu 16.04.2 LTS / Python 2.7.12 My project is a Django 1.9 project, I’m not able to pos the code publicly though.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 10
- Comments: 62 (12 by maintainers)
I only have the zappa examples in my virtual env: https://github.com/Miserlou/Zappa/tree/master/example
The biggest part of the env is importing scipy and sklearn and these two together make up for about 60M. I need them in order to deploy my machine learning model.
Why is slim_handler = true not working? Unless I’m missing something, I don’t think there’s any chance I can get my zip under 50M. Any other idea for using zappa in this scenario?
I’m having the same problem. I first get it to work, then I add the dependencies sklearn numpy scipy and setting
"slim_handler": true. After that I get:'NoneType' object is not callableI too turned off slim_handler, and the issue hasn’t happened again.
I stopped having this issue when I stopped using the slim_handler= true option. Not sure if this is it but I never experienced this issue for about a month until I started using the slim_handler, after that It happened about every day.
Well I came across the issue lately and took time to investigate the problem. The issue is that lambda is timing out, at least for my case and I believe for many cases here too. Go over the logs in cloudwatch because unlike zappa tail logs, cloudwatch separate logs into logs from each container created by lambda as streams. I observed when a faulty container (one returning NoneType) diverged from the normal one. This is what happens.
So once a request comes in and all the fired up containers are in use, a new container is created and during initialization, because we’re downloading files from s3 to /tmp and also due to sometimes lagged connectivity + vpc/db initilializations, lambda doesn’t always finish setting up within the default 30s lambda time_out set by zappa. Hence, the container times out and starts receiving request pre-maturely when in reality the modules have not all been imported. Hence the NoneType error. For every NoneType eror that I saw in cloudwatch, there was an import issue beforehand which confirms this. Therefore when new requests hit that container periodically, we see the NoneType. But when new request hit other containers without this issue, it works. And that explains for the periodic NoneType Error we get.
Hence these are what you can do.
If you can reduce the size of your code/dependencies please do so for two reasons
1)As others have mentioned above, if you can get your code + dependencies to meet the 250M unzipped / 50M zipped limit, then you can set the slim_lambda==false flag or remove it entirely as the default is false.2)If you cannot get it to meet the aws limit, the little code you remove will help because now you have reduced the files + time it has to download from s3 and maybe you never experience any time out again.Now if you can’t get your code base reduced to to the 250M unzipped / 50M zipped limit, maybe you’re in case (2) above or can’t even reduce anything at all,
increase timeout_seconds from 30s to something like 60s.. The worst that could happen by increasing timeout_seconds is that you may hit api gateway timeout once because of the 30s api gateway limit, and after that by the next request your lambda must have already been set up and ready to serve requests. Therefore you’ll not come across theNoneType object is not callable objecterror any longer.To everyone else here that might have some issues - by commenting one line at a time and uploading again and again, I’ve realized that an import statement in the top of the
app.pyfailed to execute, failed silently in zappa, and then raised the oddNoneType object is not callableexception. Surrounding the failing import statement with:I was able to find the actual issue in the dependent package, solve it, and upload my zappa function with no issues this time.
I’m not sure why the import exception was not visible by zappa in the first place. BTW, the actual issue was in another package installing the
typingmodule while running in python 3.7…For some reason it got broken only on AWS.Would it be possible and safe to ignore .pyc files from source project directory? Deleting .pyc files from my source project and
zappa updatehelps everytime for me to solve'NoneType' object is not callable. I am using Python3.6 runtime for lambdas, but everytime I accidentaly run the flask app locally with Python2, the pyc files are created and the problem emerges in AWS afterzappa updateI found a cause for my problem with NoneType is not callable. I had pyc files between some of my source files that became part of zip. Deleting them and running zappa update solved my problem. I found error like this https://teamtreehouse.com/community/importerror-bad-magic-number-in-time-bx03xf3rn and it lead me to the pyc files.
Someone who is looking for a solution this might help.
pycryptoto my requirements file.Hope this helps.
In my case, the issue was caused by:
slim_hander: trueSolution: Increase
memory_sizeand/ortimeout_seconds.These are the steps I took:
memory_size: 128,timeout_seconds: 20, andslim_handler: false(worked as expected)slim_handler: true. Received the “NoneType…” error.Thanks to @eexwhyzee for his comment prompting me in the right direction. Also in general, it’s a good idea to check metrics/logs in the lambda console or cloudwatch. This revealed that memory was maxing out.
You’re also going to want to delete
__pycache__directoriesI had the same problem. Just
'NoneType' object is not callableand nothing else in the logs.“Refreshing” my virtualenv solved the problem:
I decided to do that because I noticed that Zappa was packaging some modules that I clearly uninstalled before. So may be it’s
pipenvthat caused the problem.(this issue is the first on google for the NoneType exception) In my case the issue was caused by using a remote config (on s3) and one of the values being a number. This resulted in “Environment variable keys must be non-unicode” error, which I saw in the
zappa tail.Finally found the cause of my issue. A build script that ran before zappa included a sed command that did not flush the output buffer, which sometimes truncated my app.py file. If you are experiencing the variant of this issue that goes away when you re-deploy, check your build scripts for this sort of race condition.
@Miserlou Any update on this ? I started facing this issue after using the slim_handler= true option.