home / skills / andrueandersoncs / claude-skill-effect-ts / resource-management

resource-management skill

/skills/resource-management

This skill helps you manage resources safely with acquireRelease, scope, and finalizers, ensuring cleanup even on errors or interruption.

npx playbooks add skill andrueandersoncs/claude-skill-effect-ts --skill resource-management

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.7 KB
---
name: Resource Management
description: This skill should be used when the user asks about "Effect resources", "acquireRelease", "Scope", "finalizers", "resource cleanup", "Effect.addFinalizer", "Effect.ensuring", "scoped effects", "resource lifecycle", "bracket pattern", "safe resource handling", "database connections", "file handles", or needs to understand how Effect guarantees resource cleanup.
version: 1.0.0
---

# Resource Management in Effect

## Overview

Effect provides structured resource management that **guarantees cleanup** even when errors occur or the effect is interrupted. This is essential for:

- Database connections
- File handles
- Network sockets
- Locks and semaphores
- Any resource requiring cleanup

## Core Concept: Scope

A `Scope` is a context that tracks resources and ensures their cleanup:

```typescript
Effect<A, E, R | Scope>
//              ^^^^^ Indicates resource needs cleanup
```

## Basic Resource Acquisition

### Effect.acquireRelease

The fundamental pattern for safe resource management:

```typescript
import { Effect } from "effect"

const managedFile = Effect.acquireRelease(
  Effect.sync(() => fs.openSync("file.txt", "r")),
  (fd) => Effect.sync(() => fs.closeSync(fd))
)
```

### Using the Resource

```typescript
const program = Effect.gen(function* () {
  const fd = yield* managedFile
  const content = yield* Effect.sync(() => fs.readFileSync(fd, "utf-8"))
  return content
})

// Run with automatic scope management
const result = yield* Effect.scoped(program)
```

## Effect.scoped

Converts a scoped effect into a regular effect by managing the scope:

```typescript
const runnable = Effect.scoped(program)
```

The scope closes when the scoped block completes, triggering all finalizers.

## acquireUseRelease Pattern

For simpler cases, combine acquire/use/release in one call:

```typescript
const readFile = (path: string) =>
  Effect.acquireUseRelease(
    Effect.sync(() => fs.openSync(path, "r")),
    (fd) => Effect.sync(() => fs.readFileSync(fd, "utf-8")),
    (fd) => Effect.sync(() => fs.closeSync(fd))
  )
```

## Finalizers

### Effect.addFinalizer

Add cleanup logic to the current scope:

```typescript
const program = Effect.gen(function* () {
  yield* Effect.addFinalizer(() =>
    Effect.log("Cleanup running!")
  )

  // ... do work ...

  return result
})
```

### Effect.ensuring

Run cleanup after effect completes (success or failure):

```typescript
const withCleanup = someEffect.pipe(
  Effect.ensuring(
    Effect.log("Always runs after effect")
  )
)
```

### Effect.onExit

Run different cleanup based on exit status:

```typescript
const withExitHandler = someEffect.pipe(
  Effect.onExit((exit) =>
    Exit.isSuccess(exit)
      ? Effect.log("Succeeded!")
      : Effect.log("Failed or interrupted")
  )
)
```

## Multiple Resources

### Sequential Acquisition

```typescript
const program = Effect.gen(function* () {
  const db = yield* acquireDbConnection
  const cache = yield* acquireRedisConnection
})

const result = yield* Effect.scoped(program)
```

### Parallel Acquisition

```typescript
const program = Effect.gen(function* () {
  const [db, cache] = yield* Effect.all([
    acquireDbConnection,
    acquireRedisConnection
  ])
})
```

## Resource Patterns

### Database Connection Pool

```typescript
const DbPool = Effect.acquireRelease(
  Effect.promise(() => createPool({
    host: "localhost",
    database: "mydb",
    max: 10
  })),
  (pool) => Effect.promise(() => pool.end())
)

const query = (sql: string) =>
  Effect.gen(function* () {
    const pool = yield* DbPool
    return yield* Effect.tryPromise(() => pool.query(sql))
  })
```

### File Handle

```typescript
const withFile = <A>(
  path: string,
  use: (handle: FileHandle) => Effect.Effect<A>
) =>
  Effect.acquireUseRelease(
    Effect.promise(() => fs.promises.open(path)),
    use,
    (handle) => Effect.promise(() => handle.close())
  )
```

### Lock/Mutex

```typescript
const withLock = <A>(
  lock: Lock,
  effect: Effect.Effect<A>
) =>
  Effect.acquireUseRelease(
    lock.acquire,
    () => effect,
    () => lock.release
  )
```

## Layered Resources

Use `Layer.scoped` for service-level resources:

```typescript
const DatabaseLive = Layer.scoped(
  Database,
  Effect.gen(function* () {
    const pool = yield* Effect.acquireRelease(
      createPool(),
      (pool) => Effect.promise(() => pool.end())
    )

    return {
      query: (sql) => Effect.tryPromise(() => pool.query(sql))
    }
  })
)
```

## Error Handling in Cleanup

Finalizers should not fail, but if they do:

```typescript
const safeRelease = (resource: Resource) =>
  Effect.sync(() => resource.close()).pipe(
    Effect.catchAll((error) =>
      Effect.logError("Cleanup failed", error)
    )
  )

const managed = Effect.acquireRelease(
  acquire,
  safeRelease
)
```

## Interruption Safety

Resources are cleaned up even on interruption:

```typescript
const program = Effect.gen(function* () {
  const resource = yield* acquireResource

  yield* Effect.sleep("1 hour")
})

const result = yield* program.pipe(
  Effect.scoped,
  Effect.timeout("1 second")
)
```

## Best Practices

1. **Use acquireRelease for paired operations** - Guarantees cleanup
2. **Keep finalizers simple and infallible** - Log errors instead of throwing
3. **Use Effect.scoped at appropriate boundaries** - Not too wide, not too narrow
4. **Clean up in reverse acquisition order** - Effect handles this automatically
5. **Use Layer.scoped for service-level resources** - Lifecycle tied to layer

## Additional Resources

For comprehensive resource management documentation, consult `${CLAUDE_PLUGIN_ROOT}/references/llms-full.txt`.

Search for these sections:
- "Introduction" (Resource Management) for core concepts
- "Scope" for detailed scope mechanics
- "Managing Layers" for Layer.scoped patterns

Overview

This skill explains how to manage resources safely using Effect so cleanup is guaranteed even on errors or interruptions. It covers scopes, acquire/release patterns, finalizers, scoped effects, and common resource examples like files, DB connections, and locks. The focus is practical: how to acquire, use, and reliably release resources in real programs.

How this skill works

Effect tracks resources inside a Scope and attaches finalizers that run when the scope closes. acquireRelease/acquireUseRelease pair acquisition with deterministic cleanup. Effect.scoped runs a scoped effect and automatically closes the scope, running all finalizers in reverse acquisition order. Tools like addFinalizer, ensuring, and onExit provide flexible hooks for cleanup and exit-aware logic.

When to use it

  • When opening files, sockets, or database connections that must be closed
  • When acquiring locks or semaphores that require release
  • When you need deterministic cleanup on error or interruption
  • When building long-lived services with layered resources
  • When composing multiple resources sequentially or in parallel

Best practices

  • Use acquireRelease or acquireUseRelease for paired acquire/release patterns
  • Keep finalizers simple and make them infallible; log errors instead of throwing
  • Call Effect.scoped at clear boundaries (not too wide, not too narrow)
  • Rely on Effect’s reverse-order finalization when composing multiple resources
  • Use Layer.scoped for service-level resources tied to application lifecycle

Example use cases

  • Read a file safely with acquireUseRelease to ensure the handle is closed after use
  • Create a database pool with acquireRelease and expose query methods that use the pool
  • Wrap a lock with acquireUseRelease to guarantee release after critical sections
  • Compose acquiring DB and cache connections sequentially or in parallel inside a scoped block
  • Attach logging finalizers with addFinalizer or use ensuring/onExit for exit-specific cleanup

FAQ

What’s the difference between acquireRelease and acquireUseRelease?

acquireRelease pairs an acquire and a release; use it with Effect.scoped to access the resource. acquireUseRelease combines acquire, use, and release into one call for simpler flows.

Will cleanup run if the effect is interrupted?

Yes. Finalizers registered in the Scope run on interruption, ensuring resources are released even when effects are canceled or time out.