sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing app name breaks reloading

Tronic opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Start a dev server with some application name, rename the app and it can no longer auto-reload.

Code snippet

Traceback (most recent call last):
  File "c:\users\user\dev\sanic\sanic\app.py", line 1489, in get_app
    return cls._app_registry[name]
           ~~~~~~~~~~~~~~~~~^^^^^^
KeyError: 'Fizz_buzz'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\user\dev\sanic\sanic\worker\serve.py", line 68, in worker_serve
    a = Sanic.get_app(app_name)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\users\user\dev\sanic\sanic\app.py", line 1495, in get_app
    raise SanicException(
sanic.exceptions.SanicException: Sanic app name 'Fizz_buzz' not found.
App instantiation must occur outside if __name__ == '__main__' block or by using an AppLoader.
See https://sanic.dev/en/guide/deployment/app-loader.html for more details.

Expected Behavior

Sanic could (e.g. while handling this) look up the app object again to figure out the new name, or keep track of the old name - whichever is the issue here, so that it Just Works.

Not show unrelated error message.

How do you run Sanic?

Sanic CLI

Operating System

Windows

Sanic Version

22.12+

Additional context

Low priority, especially with Sanic(__name__) name changes should not occur frequently if ever.

We could do something like this:

if len(cls._app_registry) == 1:
    return list(cls._app_registry.values())[0]

But, it happens on an explicit get for a named app. We would need to differentiate between an actual get and one that comes from the internal worker process. If it is the latter, and there is only one, then yes I can see how it makes sense to do the above and resolve the ambiguity to "just works."

However, that seems an additional layer of complexity for a potentially obscure use case. Why would we need to support changing an app name while running?

I see that mainly happening during early development, where it is presumably fairly common to change the name while using --dev for reloads. It might be worth exploring the possibility to bind apps by module:app (which module, which variable) or other means rather than app name string.

Still, I am fine with closing this and just documenting the case properly, if it brings too much complexity.

Same with changing any arguments you might put into app.run. I do not think we need to support those on reload either as it brings a lot more to the project for a fairly limited gain. If we start seeing more requests regarding this we can return to the issue. Closing for now.