diff --git a/tunerlistd/main.py b/tunerlistd/main.py index 8093050..fe5ba8d 100644 --- a/tunerlistd/main.py +++ b/tunerlistd/main.py @@ -9,6 +9,7 @@ from uvicorn import run app = FastAPI() event = Event() +background_tasks = [] tunerlist = TunerList() bluetooth = Bluetooth() @@ -43,7 +44,12 @@ async def startup_event(): tunerlist_listener(), bluetooth_listener() ]: - create_task(i) + background_tasks.append(create_task(i)) + +@app.on_event("shutdown") +async def shutdown_event(): + for task in background_tasks: + task.cancel() @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket):