Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the signal router to finalize without a running event loop #2829

Open
1 task done
eelkevdbos opened this issue Sep 27, 2023 · 0 comments · May be fixed by #2830
Open
1 task done

Allow the signal router to finalize without a running event loop #2829

eelkevdbos opened this issue Sep 27, 2023 · 0 comments · May be fixed by #2830

Comments

@eelkevdbos
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe.

I am running some tasks by awaiting the Sanic.run_delayed_task interface. All is well, except for the fact that I cannot dispatch signals.

The error raised when dispatching a signal is TypeError: 'NoneType' object is not callable and originates from a non-finalized signal router.

The signal router cannot be finalized without a running event loop.

Describe the solution you'd like

Add a loop keyword argument to the signal router's finalize method.

def finalize(self, do_compile: bool = True, do_optimize: bool = False, loop: asyncio.AbstractEventLoop = None):
    self.add(_blank, "sanic.__signal__.__init__")

    try:
        self.ctx.loop = loop or asyncio.get_running_loop()
    except RuntimeError:
        raise RuntimeError("Cannot finalize signals outside of event loop")

    for signal in self.routes:
        signal.ctx.event = asyncio.Event()

    return super().finalize(do_compile=do_compile, do_optimize=do_optimize)

This will allow me to inject a loop instance into the finalize method, circumventing the runtime error caused by asyncio.get_running_loop() in the absence of a running loop.

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant