In C# functions you can take a CancellationToken argument to allow your function to detect that a host shutdown is occurring and abort gracefully. This work item tracks provide an equivalent mechanism for languages that execute out of proc. Exactly what type/data structure would be used would be language dependent.