Unable to attach listeners

I’m upgrading an app to Sanic V22.12.0 and the following listeners aren’t been attached anymore.

The methods attached to these listeners are no longer been called, but I get no error.

I’m using the AppLoader object alongside a factory to create the app and serving with Sanic.serve.

def serve_application(port, interface, ssl_context,) -> None:
    app, loader = configure_app()
        partial(load_agent_on_start, model_path, endpoints, remote_storage),

        partial(load_agent_on_start, model_path, endpoints, remote_storage),
    app.register_listener(close_resources, "after_server_stop")
        backlog=int(os.environ.get(ENV_SANIC_BACKLOG, "100")),
    Sanic.serve(primary=app, app_loader=loader)

Any help is appreciated, thanks!

Hey @ahopkins,

Following up to my colleague’s @Tawakalt question, I managed to upgrade to v22.12 in our codebase by retaining the previous functionality with registering before_server_start listeners by using the legacy flag in app.run().

I’ve looked closely in the Sanic codebase and noticed that the serve() method triggers only main_process_start, main_process_ready and main_process_stop events such as here and here. I was not able to read in the code where the other 4 events, such as the before_server_start event listener, were being triggered. Is this correct and intended by design?

Since support for _serve_legacy is dropped in version 23.12LTS, could you please recommend how to continue to use before_server_start listeners or how to update our Sanic usage to achieve the same intention with before_server_start, after_server_start, before_server_stop, after_server_stop listeners?

After further digging, I discovered by using a simple Sanic project that if the before_server_start etc listeners are registered within the factory function create_app passed to the factory parameter of AppLoader, then the listeners are correctly attached when running Sanic. However this requires quite a large effort refactoring in our complex codebase to move the listeners to the module where the create_app factory function is defined. Also using if __name__ = "__mp_main__" to register the listeners after instantiation doesn’t seem a viable solution either in our project structure. Is there anything else you recommend to enable an upgrade to Sanic 23.12?