Cleanup of FastAPI Deployments

Hello everyone. I’m trying to gracefully shutdown deployments due to resource allocation. Here’s the somewhat self-explanatory code snippet:

from ray import serve
from fastapi import FastAPI

app = FastAPI()

class MyDeployment:
    def __init__(self):
        print("I was called in init!")

    async def startup_event():
        print("I was called in startup!")

    async def shutdown_event():
        print("I was not called")

    def __del__(self):
        print("Neither was I")

    async def root(self):
        return f"Hello, world!"


Why neither __del__ nor shutdown events are called upon re-deployment or deletion? How do I implement cleanup routines on instances? I’m using Ray latest (1.6.0), btw.


Thanks for bringing this to our attention, @amiasato! I was able to reproduce the same locally on latest master, so this looks like a bug. I filed a GitHub issue and we will prioritize a fix.

1 Like