sanic: Triggering a worker reload does not take the code change into consideration

Describe the bug Changing the code - like the response returned by a view - should be updated when the Sanic instance is reloaded (triggered either via the CLI --trigger-reload or via app.m.restart("__ALL_PROCESSES__").

Right now, it doesn’t seems to be the case

Code snippet

from sanic import Sanic
from sanic.response import text

app = Sanic('Test')

@app.get('/reload')
async def re(request):
    request.app.m.restart("__ALL_PROCESSES__")
    return text('Restart triggered')

@app.get('/')
async def index(request):
    return text('Hello world')

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=1337, access_log=True, debug=True)

Expected behavior

  1. Run the above code
  2. Go to “http://127.0.0.1:1337/
  3. See the data : “Hello world”
  4. Change the code and replace “Hello world” by something else
  5. Go to “http://127.0.0.1:1337/reload
  6. The console shows that the server was restarted
  7. Go back to “http://127.0.0.1:1337/
  8. The text displayed is still “Hello world”, not the new one.

(Restarting the server via the command line sanic --trigger-reload app:app has the same behavior.

Environment (please complete the following information):

  • Sanic Version: 22.9.0

About this issue

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

Most upvoted comments

(Except if the spawn vs fork is defined at the initial run, and cannot be changed after that?)

Correct. This needs to be decided up front. My point earlier would be to always select spawn if ALLOW_RELOAD="always" or something like that. In the meantime, you could monkeypatch it so that auto-reload is off, but you can manually reload with spawn context:

from sanic import Sanic

Sanic._get_context = lambda *_: get_context("spawn")