jordaneremieff / mangum

AWS Lambda support for ASGI applications
https://mangum.io/
MIT License
1.67k stars 119 forks source link

`OSError` in Vercel Serverless Functions deployment #315

Open taesungh opened 7 months ago

taesungh commented 7 months ago

Hello,

My team has a Python ASGI application (specifically FastAPI) deployed on Vercel Serverless Functions which uses AWS Lambda internally. Vercel's Python Runtime for ASGI is based on Mangum as noted in vercel/vercel/packages/python/vc_init.py:173-175 with a very similar runner system of running the ASGI app instance as mangum.protocols.HTTPCycle.__call__.

The API works great most of the time but sporadically encounters a runtime issue causing requests to fail every now and then.

[ERROR] OSError: [Errno 24] Too many open files
Traceback (most recent call last):
  File "/var/task/vc__handler__python.py", line 305, in vc_handler
    response = asgi_cycle(__vc_module.app, body)
  File "/var/task/vc__handler__python.py", line 201, in __call__
    loop = asyncio.new_event_loop()
  File "/var/lang/lib/python3.9/asyncio/events.py", line 761, in new_event_loop
    return get_event_loop_policy().new_event_loop()
  File "/var/lang/lib/python3.9/asyncio/events.py", line 659, in new_event_loop
    return self._loop_factory()
  File "/var/lang/lib/python3.9/asyncio/unix_events.py", line 54, in __init__
    super().__init__(selector)
  File "/var/lang/lib/python3.9/asyncio/selector_events.py", line 53, in __init__
    selector = selectors.DefaultSelector()
  File "/var/lang/lib/python3.9/selectors.py", line 350, in __init__
    self._selector = self._selector_cls()

where /var/task/vc__handler__python.py refers to vc_init.py.

The only fix we found is redeploying our project on Vercel to create a new deployment, but this is only temporary, and the issue resurfaces every now and then, especially when traffic is high.

Any thoughts on what might be the root cause?