---
title: "Getting Serverless Errors Out Before The Function Freezes"
url: https://deallocatedobjects.com/posts/getting-serverless-errors-out-before-the-function-freezes
author: Jake Marsh
date: 2026-11-29
published: 2026-11-29
updated: 2026-11-29
topics: ["Web", "Xcode & Tools"]
description: "A function on a serverless host can be frozen the moment it returns its response. If your error reporter was going to send the exception a second later, that second never comes. The user sees a 500, and you see nothing, which is the worst combination."
---

# Getting Serverless Errors Out Before The Function Freezes

By Jake Marsh, November 29, 2026. HTML: https://deallocatedobjects.com/posts/getting-serverless-errors-out-before-the-function-freezes

A function on a serverless host can be frozen the moment it returns its response. If your error reporter was going to send the exception a second later, that second never comes. The user sees a 500, and you see nothing, which is the worst combination.

I ran into this wiring up the Node SDK for [Runheed](https://runheed.com/?ref=deallocated-post). The SDK batches events for up to 5 seconds before sending them. That's fine on a server that stays up. On a function it means the batch can still be sitting in memory when the host puts the process to sleep.

### The Failing Version

This looks right and loses errors:

```typescript
import runheed from 'runheed';

runheed.init({ dsn: process.env.RUNHEED_DSN! });

export default {
  async fetch(request: Request): Promise<Response> {
    try {
      const { cartId } = await request.json();
      return Response.json({ ok: true, cartId });
    } catch (err) {
      runheed.captureException(err as Error);
      return Response.json({ error: 'Internal error' }, { status: 500 });
    }
  },
};
```

`captureException` queues the event. The function returns, the host freezes it, and the queue waits for a wake-up that may not come before the instance is thrown away.

### Flush Before You're Frozen

The reporter has a `flush()` that sends whatever is queued and returns a promise. You could `await` it before returning the response, but then every failing request waits on a network call to your error tracker. Vercel has a better hook for this: `waitUntil` from `@vercel/functions` tells the platform to keep the function alive until a promise settles, after the response has gone out.

```typescript
import runheed from 'runheed';
import { waitUntil } from '@vercel/functions';

runheed.init({
  dsn: process.env.RUNHEED_DSN!,
  environment: process.env.VERCEL_ENV ?? 'development',
  release: process.env.VERCEL_GIT_COMMIT_SHA,
});

export default {
  async fetch(request: Request): Promise<Response> {
    try {
      const { cartId } = await request.json();
      return Response.json({ ok: true, cartId });
    } catch (err) {
      runheed.captureException(err as Error, { method: request.method, url: request.url });
      waitUntil(runheed.flush());
      return Response.json({ error: 'Internal error' }, { status: 500 });
    }
  },
};
```

The user gets their response right away, and the function stays up just long enough to send the error.

In a Next.js 15.1 or later route handler, `after()` from `next/server` does the same job, and it's what Vercel recommends there:

```typescript
import { after } from 'next/server';
import runheed from 'runheed';

export async function POST(request: Request) {
  try {
    // ...your code
    return Response.json({ ok: true });
  } catch (err) {
    runheed.captureException(err as Error, { route: '/api/checkout' });
    after(() => runheed.flush());
    return Response.json({ error: 'Internal error' }, { status: 500 });
  }
}
```

### Why The Release Line Is There

Vercel sets `VERCEL_GIT_COMMIT_SHA` on every deploy, and passing it as `release` means each error is stamped with the commit that was live when it happened. That matters once your coding agent is reading the errors. It can see which deploy introduced one, fix it, and resolve it with the commit hash of the fix. If the same error comes back, it reopens with the note from that fix attached.

### Check It

The way I test a setup like this is to throw on purpose from a deployed preview, return the 500, and look for the event a moment later. If it never shows up, the flush isn't running before the freeze. Runheed's `runheed_send_test_event` tool sends a sample event from your agent, which is handy for checking the DSN and project, but it won't tell you about the freeze. Only a real failure from the deployed function does.

Any host that stops a process after it responds has the same problem, so check what yours offers for finishing work after the response. The Vercel setup is written up with the other frameworks in [Runheed's docs](https://runheed.com/for/vercel?ref=deallocated-post).
