Resource Not Released When Using ManagedRuntime in Effect Library

Hello! I'm trying out Effect for the first time 🙂 Why isn't my resource being released when I provide it through a
ManagedRuntime
?

I'm using this to test things out:

import { Effect } from "effect"

export class Database extends Effect.Service<Database>()("Database", {
  scoped: Effect.acquireRelease(
    Effect.tryPromise({
      try: () =>
        Promise.resolve({
          client: "sql client goes here",
          close: () =>
            new Promise<void>((resolve) => {
              console.log("Connection closed")
              resolve()
            }),
        }),
      catch: (cause) => new Error("Oops", { cause }),
    }),
    (resource) => Effect.promise(() => resource.close())
  ),
}) {}


And it works:

const program = Effect.gen(function* () {
  const database = yield* Database
  yield* Console.log(database.client)
})

await Effect.runPromise(
  Effect.provide(
    program,
    Layer.mergeAll(
      Database.Default
      // Other services
    )
  )
)


But I want to create a
ManagedRuntime
that includes all my services to run in my Next.js app.

import { Layer, ManagedRuntime } from "effect"
import { Database } from "../database"

export const Runtime = ManagedRuntime.make(
  Layer.mergeAll(
    Database.Default
    // Other services
  )
)


Calling the same program this way doesn't release the resource:

await Runtime.runPromise(program)


I don't see any requirements for a scope at the type level. I'm sure I'm missing something. I just don't know what it is exactly 😅
Was this page helpful?