Uvicorn: Uvicorn with reload hangs when using a ProcessPoolExecutor

Created on 13 Jan 2021  路  2Comments  路  Source: encode/uvicorn

Checklist

  • [x] The bug is reproducible against the latest release and/or master.
  • [x] There are no similar issues or pull requests to fix it yet.

Describe the bug

When at last 1 task is submitted to a ProcessPoolExecutor uvicorn fails to reload when a file change has been detected. It detects the file change and the server is shutdown but it doesn't start again. As long as no tasks are submitted uvicorn is able to reload properly.

To reproduce

"""ProcessPoolExecutor Example.

Run
---
uvicorn main:app --reload

Versions
--------
fastapi~=0.63.0
uvicorn[standard]~=0.13.3
"""
from concurrent.futures import ProcessPoolExecutor
from typing import Any, Dict

from fastapi import FastAPI

app = FastAPI(title="Example API")
POOL = ProcessPoolExecutor(max_workers=1)


def task() -> None:
    """."""
    print("Executed in process pool")


@app.get("/")
def index() -> Dict[str, Any]:
    """Index."""
    POOL.submit(task)
    return {"message": "Hello World"}

Expected behavior

Uvicorn should reload when file changes are detected.

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [9042] using watchgod
INFO:     Started server process [9044]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
WARNING:  WatchGodReload detected file change in '['/Users/maartenhuijsmans/main.py']'. Reloading...
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [9044]
INFO:     Started server process [9047]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:61607 - "GET / HTTP/1.1" 200 OK

Actual behavior

Uvicorn doesn't start

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [9054] using watchgod
INFO:     Started server process [9056]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:61615 - "GET / HTTP/1.1" 200 OK
Executed in process pool
WARNING:  WatchGodReload detected file change in '['/Users/maartenhuijsmans/main.py']'. Reloading...
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [9056]

Debugging material

Environment

  • macOS 10.13.6 / python 3.8.6 / uvicorn 0.13.3
  • uvicorn main:app --reload

Additional context

bug multiprocessing user experience

Most helpful comment

incidentally it seems to not happen on #853

All 2 comments

incidentally it seems to not happen on #853

@euri10 thanks. I'll be patient and wait for the MR

Was this page helpful?
0 / 5 - 0 ratings