Python 3.x – The Uvicorn server shuts down unexpectedly

I am working with the FastAPI framework, served by the Uvicorn server.
My application should execute some numerical calculation that consumes a lot of time in a final point (/ execute). For this I am using & # 39; background_task & # 39; of fastAPI (which is basically & # 39; background_task & # 39; from Starlette).

When executing the application, after some moments of nominal behavior, the server shuts down for some reason.

The application records look like this:

INFO: server process started [922]
INFO: Waiting for the start of the application.
DEBUG: None - ASGI [1] Started
DEBUG: None - ASGI [1] Sent {& # 39; type & # 39 ;: & # 39; lifespan.startup & # 39;}
DEBUG: None - ASGI [1] Received {& # 39; type & # 39; lifespan.startup.complete & # 39;}
INFO: Uvicorn runs on http://0.0.0.0:8000 (Press CTRL + C to exit)
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - Connected
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57397) - Connected
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [2] Started
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [2] Received {& # 39 ;: & # 39; http.response.start & # 39 ;, & # 39 ;: & # 39 ;: 200, & # 39; headlines & # 39 ;: & # 39;<...>& # 39;
INFO: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - "GET / run HTTP / 1.1" 200
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [2] Received {& # 39; type & # 39 ;: & # 39; http.response.body & # 39 ;, & # 39; body & # 39 ;: & # 39;<32 bytes>& # 39;
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [3] Started
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [3] Received {& # 39 ;: & # 39; http.response.start & # 39 ;, & # 39 ;: & # 39 ;: 404, & # 39; headlines & # 39 ;: & # 39;<...>& # 39;
INFO: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - "GET /favicon.ico HTTP / 1.1" 404
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [3] Received {& # 39; type & # 39 ;: & # 39; http.response.body & # 39 ;, & # 39; body & # 39 ;: & # 39;<22 bytes>& # 39;
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [3] Finished

...

DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - Offline
... The background task is complete.
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57396) - ASGI [2] Finished
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57397) - Offline
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57405) - Connected

...
The application continues, with requests and background tasks completed.
At some point, during the execution of a task in the background:

INFO: turning off
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57568) - Offline
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57567) - Offline
INFORMATION: Pending completion of tasks in the background. (CTRL + C to force the exit)
DEBUG: (& # 39; 10 .0.2.111 & # 39 ;, 57567) - ASGI [6] Finished
INFO: Waiting for the application to close.
DEBUG: None - ASGI [1] Sent {& # 39; type & # 39 ;: & # 39; lifespan.shutdown & # 39;}
DEBUG: None - ASGI [1] Received {& # 39 ;: & # 39; lifespan.shutdown.complete & # 39;}
DEBUG: None - ASGI [1] Finished
INFO: Process of the finished server [922]

I really do not understand why this happens. I have no idea what to try to fix it.

My code looks like this.

#! / usr / bin / env python3.7
import time
from fastapi import FastAPI, BackgroundTasks
import uvicorn
from starlette.responses import JSONResponse
import my_imports_from_project

analysis_api = FastAPI ()

@ analysis_api.get ("/")
Async def root ():
returns {"message": "root"}


@ analysis_api.get ("/ test")
async def test ():
returns {"message": "test"}

@ analysis_api.get ("/ run")
async def run (name: str, background_task: BackgroundTasks):
try:
some_checks (name)
except RaisedExceptions:
body = {"running": False,
"name name,
"cause": "Not found in the database"}
return JSONResponse (state_code = 400, content = body)
body = {"running": True,
"name name}
background_task.add_task (run_analysis, name)
return JSONResponse (state_code = 200, content = body)


yes __name__ == "__main__":
uvicorn.run ("api: analysis_api", host = "0.0.0.0", log_level = "debug")