sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
17.92k stars 1.54k forks source link

app.create_server fails after version 21.6.2 #2532

Open ironhacker opened 1 year ago

ironhacker commented 1 year ago

Describe the bug Every version after 21.6.2 fails when running app with app.create_server. The error is

venv/lib/python3.9/site-packages/sanic/signals.py", line 73, in get
    group, param_basket = self.find_route(
TypeError: 'NoneType' object is not callable

I realize this isn't the recommended way to run Sanic, but the web server is only part of my application, does not run on startup or continuously, and shares memory with other async code. This has worked very well for me so far.

Side node: I noticed while troubleshooting that the event loop is changed to uvloop just by importing sanic. I'm of the opinion that imports shouldn't "do things". Would it be better to set the event loop when the application starts?

Code snippet

import asyncio

from sanic import Sanic                                                       
import sanic.response                                                                                                     

async def main():                                                             

    app = Sanic('HelloWorld')                                                 

    @app.get('/')                                                             
    async def helloworld(request):                                            
        return sanic.response.text('hello world')                             

    server = app.create_server(host='127.0.0.1', port=9999, return_asyncio_server=True)
    asyncio.create_task(server)                                                              
    await asyncio.sleep(30)                                                   

  if __name__ == '__main__':                                                                     
    asyncio.run(main(), debug=True)

Expected behavior successful http response

Environment (please complete the following information):

Additional context Interesting bit in the status line:

[2022-08-19 12:15:06 -0400] - (sanic.access)[INFO][UNKNOWN]: NONE http:///* 503 666

ahopkins commented 1 year ago

Check the examples

https://github.com/sanic-org/sanic/blob/main/examples/run_async.py

You can disable uvloop either by config, or at install time with env variables.

https://sanic.dev/en/guide/deployment/configuration.html#builtin-values

costa commented 1 month ago

I want to +1 this issue; will gladly see an example with several asyncio servers within the same process; within my server, which runs HTTP server (sanic) and also listens to some MQ, I get this after upgrading to the latest version at the time of writing:

[2024-06-19 18:10:29 +0000] [9] [INFO] mode: production, single worker                                                         
[2024-06-19 18:10:29 +0000] [9] [INFO] server: sanic, HTTP/1.1                                                                 [2024-06-19 18:10:29 +0000] [9] [INFO] python: 3.8.19                                                                          
[2024-06-19 18:10:29 +0000] [9] [INFO] platform: Linux-5.15.0-1017-gcp-x86_64-with-glibc2.34                                   
[2024-06-19 18:10:29 +0000] [9] [INFO] packages: sanic-routing==23.12.0                                                        
[2024-06-19 18:10:32 +0000] [9] [ERROR] protocol.connection_task uncaught                                                      
Traceback (most recent call last):                                                                                             
  File "/usr/local/lib/python3.8/site-packages/sanic/server/protocols/http_protocol.py", line 150, in connection_task          
    await self.app.dispatch(                                                                                                   
  File "/usr/local/lib/python3.8/site-packages/sanic/signals.py", line 304, in dispatch                                        
    return await dispatch                                                                                                      
  File "/usr/local/lib/python3.8/site-packages/sanic/signals.py", line 216, in _dispatch                                       
    group, handlers, params = self.get(event, condition=condition)                                                             
  File "/usr/local/lib/python3.8/site-packages/sanic/signals.py", line 177, in get                                             
    group, param_basket = self.find_route(                                                                                     
TypeError: 'NoneType' object is not callable                                                                                   
Task exception was never retrieved                                                                                             
future: <Task finished name='Task-19' coro=<HttpProtocol.connection_task() done, defined at /usr/local/lib/python3.8/site-packa
ges/sanic/server/protocols/http_protocol.py:141> exception=TypeError("'NoneType' object is not callable")>