Gunicorn: multiprocessing.cpu_count() is dangerous in containers

Created on 2 May 2019  ·  4Comments  ·  Source: benoitc/gunicorn

https://github.com/benoitc/gunicorn/blob/bd833e0009b8b07d13117f71534eb28e8dc24c5d/docs/source/configure.rst#L67

Due to https://bugs.python.org/issue36054, the recommendation in these docs is dangerous when running inside a container. If your container has been allocated (say) a single core and 512 MB of RAM on a machine with 64 cores, multiprocessing.cpu_count() thinks it has 64 cores instead of just 1, and you end up spawning 129 workers. This is way more than the container has resources for, and ends up causing out of memory crashes.

Most helpful comment

@twosigmajab good idea! please go ahead :)

All 4 comments

Hello 😄! Thanks for the notification.

The default in Gunicorn is a single worker, even if the documentation has an example of detecting the number. Given that this looks likely to be a Python bug fix eventually, I'm not sure there's anything we should do in particular here. Filing it downstream at flask-gunicorn makes a lot of sense, though.

it's more something that should be fixed in python not really an issue there. The documentation has been made at the time docker were not even existing but I do think it's still useful for the example since it's very explicit. Closing the issue then.

Adding 1 tiny line to the docs to say ("but beware of BPO-36054 if using containers!") could save many users headaches for the ~years they'll still be using versions of Python that don't have this fixed. If you're not opposed to merging that, I'll submit a PR if no one beats me to it.

@twosigmajab good idea! please go ahead :)

Was this page helpful?
0 / 5 - 0 ratings