title: How do I let an executing azure function complete during a shut down?
body: <p>Active functions are killed when a Function App is restarted which includes restarts, stop/starts, publishing on top, and swapping deployment slots.  For a functions that are run frequently, that's a problem.</p>
  <p>We are sticking with the goal of our functions to be very fast running.  Maybe a few seconds at most.</p>
  <p>But, when a Function App is stopped or restarted, the host is not providing the &quot;actively running&quot; functions a chance to complete before stopping.  This means we're going to have to manage the state of every function execution which defeats the purpose of stateless, fast-running functions.  And to make it worse, managing that state (like writing it to a store of some kind) is not guaranteed to complete either.</p>
  <p>Is there any way to either configure the Function App to NOT do this (and wait X seconds before forceably cancelling the function executions)?  If not, is there a way to hook into the shutdown process and make it &quot;wait&quot; while active functions complete?</p>
  <p>The <code>CancellationToken</code> is great for triggering something to run for static cleanup (like flushing logs) but when it runs the active functions have already been cancelled.</p>
  <p>I created a simple repro with instructions on setup and posted it to GitHub:
  <a href="https://github.com/retsvek/UngracefulUnloadOfFunctions" rel="nofollow noreferrer">https://github.com/retsvek/UngracefulUnloadOfFunctions</a></p>