Interruption Exception with `ctx.waitUntil(dispose())` in Cloudflare Worker

If i do ctx.waitUtil(dispose()) in my cloudflare worker i get an interrupted exception

import { HttpServer } from '@effect/platform'
import { RpcSerialization, RpcServer } from '@effect/rpc'
import { ConfigProvider, Layer } from 'effect'
import { Resource } from 'sst'
import { WaitlistRpcsLive } from './router.js'
import { WaitlistRpcs } from './schema.js'
import { HttpRequest } from './services/http-request.js'

export default {
  async fetch(
    req: Request,
    env: Record<string, string>,
    ctx: ExecutionContext,
  ) {
    const config = Object.entries({
      ...env,
      REDIS_URL: `https://${Resource.Redis.endpoint}`,
      REDIS_REST_TOKEN: Resource.Redis.restToken,
    })

    const { handler, dispose } = RpcServer.toWebHandler(WaitlistRpcs, {
      layer: Layer.mergeAll(
        Layer.provide(
          WaitlistRpcsLive.pipe(
            Layer.provide(Layer.succeed(HttpRequest, {
              remoteIp: req.headers.get('cf-connecting-ip') ?? undefined,
              countryCode: (req.cf?.country) as string | undefined,
              userAgent: req.headers.get('user-agent') ?? undefined,
            })),
          ),
          Layer.setConfigProvider(ConfigProvider.fromMap(new Map(config))),
        ),
        RpcSerialization.layerNdjson,
        HttpServer.layerContext,
      ),
    })

    try {
      return await handler(req)
    } finally {
      ctx.waitUntil(dispose())
    }
  },
}
Was this page helpful?