skip to content

Function Sandbox Lifecycle

Functions run in sandboxes the platform freezes, reuses and tears down on its own schedule. Interviewers ask what that does to background work, telemetry and isolation between tenants.

on this pageshow

questions

6

On a function platform, what happens to work a handler leaves running after it has returned its HTTP response?

level: juniorimportance: must knowfreq 60%

answer

  1. the platform's unit of work ends at return
  2. sandbox frozen, not stopped
  3. resumes only on the next invocation
  4. await it or hand it to a queue

basics

~20 s

Work left running after the response is not guaranteed to finish. A microVM-based platform freezes the sandbox once the handler returns, so un-awaited calls pause until a later invocation thaws it, or vanish if the sandbox is reclaimed first.

solid answer

~50 s

A function platform treats the handler's return as the end of the invocation it schedules and bills. On a microVM-based platform the sandbox is then **frozen**: timers stop and an un-awaited upload or analytics flush pauses mid-flight. It resumes only if a later invocation lands on the same sandbox, which may be much later or never, because idle sandboxes are reclaimed on the platform's own schedule. An isolate-based edge runtime commonly cancels outstanding work unless it was registered with the runtime's after-response hook, and a container-instance platform that allocates CPU only during requests starves it instead. So the fix is to await the work before returning, publish it to a queue for another function, or use the platform's sanctioned after-response mechanism for short best-effort work, and to make it idempotent, since a retried invocation repeats it.

code

pseudocode · 11 lines
pseudocode
// leaks work past the response
handler(request):
    order = saveOrder(request)
    sendAnalytics(order)          // not awaited: frozen or cancelled after return
    return response(201, order)

// keeps the work inside the invocation, or hands it off
handler(request):
    order = saveOrder(request)
    await publish(analyticsQueue, order)   // fast, durable hand-off
    return response(201, order)

go deeper

for a junior

Recall the core rule: the invocation ends when the handler returns, and nothing started but not awaited is guaranteed to finish. Await it, or put it on a queue.

for a middle

Explain the mechanics per sandbox model: a microVM is frozen and may thaw later, an isolate cancels unregistered work, a request-only-CPU container starves it.

for a senior

Show you can recognise the symptoms in production (losses that track quiet periods, work logged under the wrong request) and choose between awaiting, a queue hand-off and an after-response hook.

for a principal

Frame it as a team standard: which after-response mechanisms are allowed, which work must go through a queue, and how code review and tests catch un-awaited calls before production does.

## What "after the response" means to a function platform A **function platform** schedules and bills work per **invocation**. From its point of view an invocation starts when an event or request is handed to the handler and ends when the handler returns (for an HTTP function, when the response is sent). Anything the handler started and did not wait for (an un-awaited network call, a timer, a buffered write, a batch of analytics events) is **leftover work**. It lives in the sandbox's memory, but the platform has no record of it and makes no promise about it. The **sandbox** is the isolated environment the code runs in: a microVM, a V8 isolate or a container instance. Platforms keep sandboxes warm between invocations and reclaim idle ones on their own schedule, so what happens to leftover work depends on what the platform does to an idle sandbox. ## What each sandbox model does with leftover work | Sandbox model | Between invocations | Leftover work | |---|---|---| | microVM-based function platform | The whole sandbox is **frozen**: no CPU is scheduled for it | Pauses mid-flight; resumes only if a later invocation thaws this sandbox; lost if the sandbox is reclaimed first | | isolate-based edge runtime | The request's context ends; the isolate goes on serving other requests | Commonly cancelled, unless registered with the runtime's **after-response hook**, which extends it for a bounded time | | container-instance platform, CPU allocated only during requests | The process stays alive, but its CPU is throttled to almost nothing | Crawls or stalls until another request arrives on that instance | | container-instance platform, CPU always allocated | The process keeps running with CPU | Usually continues, until the platform scales the instance in and sends a termination signal with a short grace window | On a microVM platform, three consequences follow from the freeze: - **Late completion.** Under steady traffic the next invocation thaws the sandbox quickly and the paused work carries on, so the bug hides under load and appears at night or on quiet functions. - **Wrong attribution.** Work that resumes during a later invocation logs at that invocation's time, often under its request identifier, and it competes for CPU with that invocation, lengthening its billed duration. - **Silent loss.** If no invocation arrives before the platform reclaims the idle sandbox, the paused work disappears with it. Nothing is logged, because nothing ran. ## Symptoms an operator sees - Analytics events or audit records missing in proportion to how quiet the function is. - A file the request "uploaded" appears much later, or never. - A later invocation's duration spikes because it is finishing an earlier request's work. - An error from leftover work surfaces in a different request's logs, or fails an unrelated invocation. - Code that passes every local test, because a local process is never frozen. ## Fixes, in order of preference 1. **Await the work before returning.** The caller waits a little longer, but the work becomes part of the invocation: it is billed, it is covered by the invocation's time limit, and its errors are reported against the right request. 2. **Hand the work off durably.** Publish a message to a queue or stream and let another function do the slow part. The handler awaits only the publish, which is fast, and the receiving function gets its own retries and failure handling. 3. **Use the platform's sanctioned mechanism where one exists.** An isolate runtime's after-response hook, or a container-instance platform set to keep CPU allocated, lets short work finish after the response. Both are bounded, platform-specific and best-effort; neither replaces a queue for work that must happen. 4. **Make the work idempotent.** Whichever path you choose, a retried invocation can repeat it, so writes should tolerate a second attempt. ## The freeze is not the time limit The freeze comes after a successful return and pauses work. The **hard per-invocation time limit** fires while the handler is still running: the platform stops the invocation, reports a timeout and commonly resets or discards the sandbox, so in-flight work is lost rather than paused. Both break the assumption that started work finishes, but they need different fixes. The freeze is fixed by awaiting or handing off; the time limit by splitting the work into steps that each fit inside it.

  • Why might work a request started show up in the logs long after that request finished?
    On a microVM-based platform the sandbox was frozen with the work paused. When a later invocation thawed the same sandbox, the work resumed and logged at that later time, often under the later invocation's request identifier. The delay is however long the sandbox sat idle, which is why the symptom is worst on quiet functions.
  • When is an after-response hook an acceptable alternative to awaiting the work?
    When the work is short and best-effort, such as a cache warm-up or a non-critical metric, and the platform documents the hook. The hook extends the sandbox's life for a bounded time only. Anything that must happen, such as an audit record or a payment step, goes through a queue so it gets retries and failure handling of its own.
  • How does the hard per-invocation time limit differ from the freeze after the response?
    The time limit fires while the handler is still running: the invocation is stopped, reported as a timeout, and the sandbox is commonly reset, so in-flight work is lost, not paused. The freeze follows a successful return and pauses leftover work, which may resume later. One is fixed by splitting long work into steps, the other by awaiting or handing off.

A film that pauses the moment the last viewer leaves the room: it carries on from the same frame only if someone walks back in, and if the cinema closes first, nobody ever sees the ending.

  • Fire-and-forget calls finish in the background after the handler returns.
  • Returning early is a free way to take slow side work off the response time.
  • A frozen sandbox resumes its paused work by itself after a short while.
  • Work paused by the freeze is always discarded and can never resume later.
  • Every function platform treats work after the response the same way.
open as a page

What does a microVM, a V8 isolate and a container instance each isolate a function from, and what does that mean for multi-tenancy?

level: middleimportance: must knowfreq 50%

basics

~20 s

A microVM gives each sandbox its own guest kernel behind hardware virtualization; an isolate separates heaps inside one shared runtime process; a container separates processes that share the host kernel. Stronger boundaries cost start time and density.

open as a page

Which workloads cannot run on an isolate-based edge runtime, and how do you check a function fits one before porting it?

level: middleimportance: should knowfreq 35%

basics

~20 s

An isolate-based edge runtime cannot run native binaries, spawn processes, use a general writable disk, open arbitrary raw sockets or do long CPU-bound work. Check by auditing dependencies, building for the target and measuring CPU time per request.

open as a page

A function's traces and metrics are missing for a fraction of invocations, though its logs show every request succeeded; how do you diagnose and fix it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Telemetry libraries buffer data and export it on a background timer, which stops when the sandbox is frozen after the response, so buffered spans arrive late or are lost. Flush before returning, or export through a local extension or log lines.

open as a page

After quiet periods, a function's first call to a downstream service fails with a connection-reset error and succeeds on retry; how does the sandbox lifecycle cause this?

level: seniorimportance: should knowfreq 35%

basics

~20 s

A pooled connection kept from an earlier invocation sits idle while the sandbox is frozen. The server, load balancer or NAT closes it at its idle timeout, the frozen client never notices, and the first write after the thaw fails.

open as a page

A platform team must run customers' untrusted code as functions; how do you choose between microVMs, isolates and hardened containers as the sandbox?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Choose by the code you accept and the risk you can carry: arbitrary binaries need a microVM per tenant, single-language scripts can share isolate processes, containers need an extra kernel layer. Whatever you pick, never reuse a sandbox across tenants.

open as a page