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

RuntimeError: Event loop stopped before Future completed. #2948

Open
1 task done
KlimchevKonstantin opened this issue May 13, 2024 · 3 comments
Open
1 task done

RuntimeError: Event loop stopped before Future completed. #2948

KlimchevKonstantin opened this issue May 13, 2024 · 3 comments
Labels

Comments

@KlimchevKonstantin
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Python 3.12.3
sanic-23.12.1

very often (not 100%) after restarting worker, for example:
@apt.get("/restart")

I get this output after Ctrl-C:

(.venv) koka@BEELINK-GTR6:~/work/python/saymon/netscan-sanic$ /home/koka/work/python/saymon/netscan-sanic/.venv/bin/python /home/koka/work/python/saymon/netscan-sanic/run.py
[2024-05-13 11:56:27 +0300] [37003] [WARNING] Sanic is running in PRODUCTION mode. Consider using '--debug' or '--dev' while actively developing your application.
[2024-05-13 11:56:27 +0300] [37013] [INFO] I'm worker
[2024-05-13 11:56:27 +0300] [37012] [INFO] Sanic Extensions:
[2024-05-13 11:56:27 +0300] [37012] [INFO]   > injection [0 dependencies; 0 constants]
[2024-05-13 11:56:27 +0300] [37012] [INFO]   > openapi [http://0.0.0.0:8000/docs]
[2024-05-13 11:56:27 +0300] [37012] [INFO]   > http 
/home/koka/work/python/saymon/netscan-sanic/.venv/lib/python3.12/site-packages/sanic/touchup/schemes/ode.py:70: DeprecationWarning: Attribute s is deprecated and will be removed in Python 3.14; use value instead if hasattr(event, "s"):
[2024-05-13 11:56:27 +0300] [37012] [INFO] Starting worker [37012]
[2024-05-13 11:56:31 +0300] [37083] [INFO] I'm worker
^Cdone
[2024-05-13 11:56:34 +0300] [37003] [INFO] Received signal SIGINT. Shutting down.
[2024-05-13 11:56:34 +0300] [37012] [INFO] Stopping worker [37012]
[2024-05-13 11:56:34 +0300] [37012] [ERROR] Event loop stopped before Future completed.
Traceback (most recent call last):
  File "/home/koka/work/python/saymon/netscan-sanic/.venv/lib/python3.12/site-packages/sanic/worker/serve.py", line 117, in worker_serve
    return _serve_http_1(
           ^^^^^^^^^^^^^^
  File "/home/koka/work/python/saymon/netscan-sanic/.venv/lib/python3.12/site-packages/sanic/server/runners.py", line 301, in _serve_http_1
    _run_server_forever(
  File "/home/koka/work/python/saymon/netscan-sanic/.venv/lib/python3.12/site-packages/sanic/server/runners.py", line 187, in _run_server_forever
    cleanup()
  File "/home/koka/work/python/saymon/netscan-sanic/.venv/lib/python3.12/site-packages/sanic/server/runners.py", line 283, in _cleanup
    loop.run_until_complete(asyncio.sleep(0.1))
  File "uvloop/loop.pyx", line 1515, in uvloop.loop.Loop.run_until_complete
RuntimeError: Event loop stopped before Future completed.
[2024-05-13 11:56:34 +0300] [37003] [INFO] Server Stopped

is this a mistake or am I doing something wrong?

Code snippet

from time import sleep
from sanic import Sanic, Request
from sanic.log import logger
from sanic.response import text

app = Sanic(__name__)


def worker() -> None:
    try:
        while True:
            logger.info(f"I'm worker")
            sleep(30)
    except KeyboardInterrupt:
        print("done")


@app.main_process_ready
async def ready(app: Sanic):
    app.manager.manage("SomeWorker", worker, {}, transient=True, restartable=True)


@app.get("/restart")
async def restart_handler(request: Request):
    request.app.m.restart("Sanic-SomeWorker-0")
    return text(request.app.m.name)


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, motd=False)

Expected Behavior

No response

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Linux

Sanic Version

23.12.1

Additional context

No response

@ahopkins
Copy link
Member

I do not think there is anything wrong,but I also cannot replicated this behavior.

@prryplatypus
Copy link
Member

prryplatypus commented Jul 5, 2024

Someone (invk7244) posted the following example in our Discord to replicate this:

import asyncio

from sanic import Sanic
from sanic.response import text


Sanic.START_METHOD_SET = True
Sanic.start_method = "fork"


async def index(request):
    return text("dummy")


async def wait_some_time(app, loop):
	"""Operations to perform on server shutdown. 
	For demo just a sleep"""
    await asyncio.sleep(5)


def make_app() -> Sanic:
    app = Sanic(name="dummy")
    app.add_route(index, "/")
    app.register_listener(wait_some_time, "before_server_stop")

    return app


def run_server():
    app = make_app()
    app.run(host="0.0.0.0", port=8000)


if __name__ == "__main__":
    run_server()

@NikPaushkin
Copy link

Yes. Facing the same, can't gracefully shut down my tasks due to this issue.

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

No branches or pull requests

4 participants