sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
17.86k stars 1.53k forks source link

RuntimeError: Event loop stopped before Future completed. #2948

Open KlimchevKonstantin opened 1 month ago

KlimchevKonstantin commented 1 month ago

Is there an existing issue for this?

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 commented 1 week ago

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