Skip to content

Commit d368c8c

Browse files
author
Lukas Wingerberg
committed
improve async task shutdown handling
1 parent ed2dfc4 commit d368c8c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

server/grpc-ffmpeg.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,25 @@ async def health_check(request):
207207
await site.start()
208208
logger.info('Health check server started on http://localhost:8080/health')
209209

210+
# Keep the server alive until shutdown
211+
try:
212+
await asyncio.Future() # Placeholder to keep the server running
213+
except asyncio.CancelledError:
214+
logger.info("HTTP server shutdown initiated.")
215+
finally:
216+
await runner.cleanup() # Cleanup on shutdown
217+
218+
210219
async def ffmpeg_server():
211220
grpc_task = asyncio.create_task(start_grpc_server())
212-
http_task = asyncio.create_task(start_http_server())
213221
health_task = asyncio.create_task(health_check_runner())
214-
await asyncio.gather(grpc_task, http_task, health_task)
222+
http_task = asyncio.create_task(start_http_server()) # Treat HTTP server as a task
223+
224+
try:
225+
await asyncio.gather(grpc_task, health_task, http_task)
226+
except asyncio.CancelledError:
227+
logger.info("Server tasks canceled. Cleaning up...")
228+
raise
215229

216230
def handle_signals():
217231
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)