Describe the bug
I have issue with starting application in latest version of Sanic. Everything works correctly in v20.12.1.
Code snippet
async def start_server(self):
# ...
# Let's get listeners
self.before_server_start = server_settings.get('before_start', [])
self.after_server_start = server_settings.get('after_start', [])
self.before_server_stop = server_settings.get('before_stop', [])
self.after_server_stop = server_settings.get('after_stop', [])
# ...
# start server
> self.server = await serve(**server_settings)
E TypeError: serve() got an unexpected keyword argument 'main_start'
Environment (please complete the following information):
Additional context
Here is link to commit where issue was introduced.
Can you let me know in what context this is coming up? Are you trying to use serve directly and not via app.run?
If so, you probably should be using serve_single or serve_multiple instead.
@ahopkins is right. The sanic serve() method is not really meant to be used by applications. It is always subject to change, and will (as you found out) change and break between versions. It should probably be changed to be flagged as a private method, and probably communicated in the docs.
You might be able to do what you need to do using app.create_server() with return_asyncio_server=True.
See the run_async_advanced.py file for an example.
Addressing your specific issue directly:
It looks like you're created your own serve_server method, that calls into the sanic app.serve() is that right? In your code snippet It is not clear where the server_settings variable come from, and where the serve() method is calling to.
I think simple fix on your end would be to change your last line to this:
main_start = server_settings.pop("main_start", None)
main_stop = server_settings.pop("main_stop", None)
# start server
self.server = await serve(**server_settings)
Thanks for answers!
I double checked it and it looks like issue is present only when I run tests. When application is runned with app.run() everything is works perfect.
I use pytest-sanic for tests and use pytest_sanic.plugin.test_client fixture in my tests. This fixture (in method pytest_sanic.utils.TestServer.start_server) is using self.app._helper(...) method which they, probably, should not use.
Should I report this as issue to pytest-sanic?
As temporary fix to this I just fallback to v20 of Sanic.
Oh, I see. Yes, it is known that pytest-sanic doesn't support Sanic v21.3 yet. Yes, please open a report against that repository.
It looks like issue is already reported.
I will close this issue.
Thanks for help!
Most helpful comment
Addressing your specific issue directly:
It looks like you're created your own
serve_servermethod, that calls into the sanicapp.serve()is that right? In your code snippet It is not clear where the server_settings variable come from, and where theserve()method is calling to.I think simple fix on your end would be to change your last line to this: