workerd icon indicating copy to clipboard operation
workerd copied to clipboard

CC-6219: Implement setInactivityTimeout in ContainerClient

Open gpanders opened this issue 2 months ago • 0 comments

setInactivityTimeout allows a container to outlive its associated actor. The current implementation tightly couples the lifetime of the container client to the lifetime of the actor, so that the ContainerClient is always destroyed whenever the actor is destroyed.

To fix this we make ContainerClient reference counted and provide a reference to the actor. Whenever setInactivityTimeout is called we add a reference to the client. If the actor shuts down, the remaining reference keeps the container alive until the timeout expires. If setInactivityTimeout was never called then there is no remaining reference, and the container shuts down when the actor does.

We also implement the ability for an actor to reconnect to an already running container when it restarts. We do this by maintaining a map of containerId->ContainerClient in the ActorNamespace. This map does not hold a reference to the ContainerClient: only actors and the inactivity timeout promise hold references to ensure we do not leak resources by holding a reference indefinitely. Whenever an actor starts it consults the map to see if a ContainerClient already exists. If it doesn't we create one and add it to the map.

In order to clear entries from the map when a ContainerClient is destroyed we provide a callback function (cleanupCallback) to the ContainerClient which is run when ContainerClient is destroyed.

gpanders avatar Nov 10 '25 21:11 gpanders