unit: strange error when serving app from starlette

I get 503 error when running nginx+unit serving the example starlette app (https://unit.nginx.org/howto/starlette/). The complete log for unit is:

2022/01/26 00:37:51 [info] 33216#139127 discovery started
2022/01/26 00:37:51 [notice] 33216#139127 module: python 3.10.2 "/home/shared/Builds/unit-1.26.1/modules/python3.10.unit.so"
2022/01/26 00:37:51 [info] 36261#518939 controller started
2022/01/26 00:37:51 [notice] 36261#518939 process 33216 exited with code 0
2022/01/26 00:37:51 [info] 58715#394694 router started
2022/01/26 00:37:51 [info] 58715#394694 OpenSSL 1.1.1l  24 Aug 2021, 101010cf
2022/01/26 00:37:51 [info] 93430#495270 "myapp" prototype started
2022/01/26 00:37:51 [info] 32427#513572 "myapp" application started
2022/01/26 00:38:30 [alert] 32427#513572 [unit] #8: Python failed to create 'client' pair
2022/01/26 00:38:30 [alert] 32427#513572 [unit] Python failed to call 'loop.call_soon'
Traceback (most recent call last):
  File "/home/shared/Builds/Python-3.10.2/lib/python3.10/asyncio/base_events.py", line 745, in call_soon
    self._check_closed()
ValueError: invalid literal for int() with base 10: ''

I am using Python3.10.2. I have nginx pointing to unit via unix socket. The error is very strange I am not able to figure what could be wrong. Happy to provide any more information as needed.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @alejandro-colomar please have a look here. as discussed.

https://github.com/nginx/unit/blob/aeed86c6829c62359e79f239b849766efb8857a7/src/python/nxt_python_asgi.c#L1084

I think this is failing even before the port thing. I remember seeing this error in the log too:

https://github.com/nginx/unit/blob/aeed86c6829c62359e79f239b849766efb8857a7/src/python/nxt_python_asgi.c#L666

Line 666, BTW xD

We’ll check later.

Okay, we have a fix. There’s something I don’t know, and that’s if I need to Py_INCREF(Py_None) or not. I’ll open a PR soon, so you can check it, but the fix is already in my test branch, so you can have a look at it: https://github.com/alejandro-colomar/unit/tree/sock-test.

I changed your repository a bit to serve a static website instead of a python/falcon app, and it works. So we need to fix the code for python ASGI, as @mar0x said.

@VBart I am not sure I follow. It would be great if there is workaround for this. Could we add support of unix sockets?

The problem is caused by the fact that the client IP address in your case appears to be not IP, but unix socket of nginx. If you’d like to provide the real client IP address that is (I assume) connecting to your nginx server via TCP, then you need to configre X-Forwarded-For header field on your nginx: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

See here for nginx configuration: https://unit.nginx.org/howto/integration/#fronting-unit-with-nginx

On the Unit side in order to support passing real client IP via X-Forwarded-For, you need to configure the client_ip option.

@VBart, Good morning!

Unfortunately, the solution you suggested, with the implementation of the PR extension https://github.com/nginx/unit/pull/655 by @alejandro-colomar, only solves the client_ip part of the problem using X-Forwarded-For.

I have created a repository with the ability to quickly reproduce this problem in just one command: https://github.com/echolimazulu/unit-asgi-503

@alejandro-colomar, Good morning!

This is what you requested earlier in issue https://github.com/nginx/unit/issues/642

Please pay attention to the possibility of finding a solution to this problem based on the data I provided.

Thank you very much for your attention to this issue!