# Concurrent work needs an owner and a bound

> For agents: start with the [agent guide](https://ratstack.sh/llms.txt). Every page is Markdown by default; add `Accept: text/html` for HTML.

New on /news: [Foldkit joins Effect as its full-stack framework](/news).

Choose how many operations may run at once. Give child work an owner that can stop it.
A completed caller must not silently leave its work running.

## Bound collection processing before running it

A traversal processes each input in a collection. An Effect describes work before execution.

`Effect.forEach` describes one Effect per input and collects results in input order.
Its concurrency option bounds active operations.
The input collection can be larger than that bound.
The checked fixture measures active work with a Ref, an Effect value that stores mutable state.

packages/capability/test/fixtures/wiki/bounded-traversal.ts at 4019cffbcbdb282d6f006854ff5b1f26d3b407e0; lines 8-30; highlighted none.

```typescript
    const activity = yield* Ref.make({ active: 0, maximum: 0 });
    const input = Array.from({ length: options.count }, (_, index) => index);

    const results = yield* Effect.forEach(
      input,
      (index) =>
        Effect.acquireUseRelease(
          Ref.update(activity, (state) => ({
            active: state.active + 1,
            maximum: Math.max(state.maximum, state.active + 1),
          })),
          () => Effect.yieldNow.pipe(Effect.andThen(Effect.succeed(index))),
          () =>
            Ref.update(activity, (state) => ({
              ...state,
              active: state.active - 1,
            }))
        ),
      { concurrency: options.concurrency }
    );

    return { ...(yield* Ref.get(activity)), results };
  }
```

The generated test varies both the input count and the bound.
It requires completed work, no remaining activity, input-order results, and a peak that does not exceed the supplied bound.

packages/capability/test/concurrency-ownership.test.ts at 4019cffbcbdb282d6f006854ff5b1f26d3b407e0; lines 7-26; highlighted none.

```typescript
const Traversal = Schema.Struct({
  concurrency: Schema.Literals([1, 2, 3, 4]),
  count: Schema.Literals([1, 2, 3, 4, 5, 6, 7, 8]),
});

it.effect.prop(
  "traversal never exceeds its supplied bound and preserves input order",
  { scenario: Arbitrary.schema(Traversal) },
  ({ scenario }) =>
    Effect.gen(function* traversalBound() {
      const observed = yield* boundedTraversal(scenario);
      expect(observed.maximum).toBeGreaterThan(0);
      expect(observed.maximum).toBeLessThanOrEqual(scenario.concurrency);
      expect(observed.active).toBe(0);
      expect(observed.results).toEqual(
        Array.from({ length: scenario.count }, (_, index) => index)
      );
    })
);

```

The expectation uses the supplied bound. It is independent of the traversal's scheduling algorithm.

A planted violation replaced the bound with `unbounded`.
The property failed with seven active operations against a bound of one. The restored implementation passes.
Measure production limits separately from these generated test counts.

## Stop child work with its Scope

The sandbox writes queued input to a child process through a scoped fiber: child work that stops when its owning Scope closes.
A fiber is an independently running Effect. A Scope owns cleanup.

packages/capability/src/sandbox-subprocess.ts at b4d6b8f499e57bc76914ad70df3a0c99751ae28c; lines 237-242; highlighted none.

```typescript
      const outbox = yield* Queue.unbounded<Uint8Array, Cause.Done>();
      yield* Stream.fromQueue(outbox).pipe(
        Stream.run(handle.stdin),
        Effect.ignore,
        Effect.forkScoped
      );
```

The checked ownership test starts a child that waits indefinitely.
When the owner closes, the child must observe interruption before the enclosing Effect returns.

packages/capability/test/concurrency-ownership.test.ts at 4019cffbcbdb282d6f006854ff5b1f26d3b407e0; lines 30-46; highlighted none.

```typescript
    Effect.gen(function* childOwnership() {
      const started = yield* Deferred.make<boolean>();
      const interrupted = yield* Ref.make(0);
      yield* Effect.scoped(
        Effect.gen(function* ownChild() {
          yield* Deferred.succeed(started, true).pipe(
            Effect.andThen(Effect.never),
            Effect.onInterrupt(() =>
              Ref.update(interrupted, (count) => count + 1)
            ),
            Effect.forkScoped
          );
          yield* Deferred.await(started);
        })
      );
      expect(yield* Ref.get(interrupted)).toBe(1);
    })
```

A Deferred is an Effect value completed once to signal that work started. The test does not depend on an arbitrary sleep.

Use [scopes](/lore/scopes-own-resources) to decide the lifetime before choosing a fork operation.
Different fork operations carry different ownership rules; do not treat their names as interchangeable.

## Consume a stream without promising a bounded queue

The sandbox decodes stdout, splits it into lines, and interprets each protocol message with an Effect.
Its consumer stops after the first completed outcome.

packages/capability/src/sandbox-subprocess.ts at b4d6b8f499e57bc76914ad70df3a0c99751ae28c; lines 254-267,289-298; highlighted none.

```typescript
      const outcome = yield* Stream.decodeText(handle.stdout).pipe(
        Stream.splitLines,
        Stream.filter((line) => line.trim() !== ""),
        Stream.mapEffect(
          (line) =>
            decodeChildMessage(line).pipe(
              Effect.mapError(
                (error) =>
                  new SandboxError({
                    logs: [],
                    message: `Unreadable sandbox message: ${error.message}`,
                    reason: "protocol",
                  })
              ),
            ),
          { concurrency: 8 }
        ),
        Stream.filter(Option.isSome),
        Stream.map((option) => option.value),
        Stream.runHead,
        Effect.mapError((error) =>
          isSandboxError(error)
            ? error
            : new SandboxError({
```

`Stream.mapEffect` keeps decoding and dispatch in the stream description. The consumer does not launch eager Promises for each line.

Backpressure lets the consumer limit how quickly the producer can continue.
An unbounded queue can let the producer keep adding data.

The producer uses `Queue.unbounded`. This code does not prove a bounded producer queue or bounded total memory.
A bounded consumer is not enough when producers can accumulate data faster than it is consumed.

For a growing input, choose a stream and inspect each buffer's policy.
For a known finite collection, a bounded traversal can be enough.
Measure the service's limits before selecting an operational bound.

## Common mistakes

- Create eager Promises before building the owned Effect traversal.
- Detach work that should stop with the caller.
- Bound worker concurrency and claim every upstream buffer is bounded too.
- Copy test counts into production without measuring the provider and host.

## Effect idiom and house rule

- Fibers, traversal bounds and streams compose owned work without replacing the typed Effect description.
- In rat-stack, measure operational limits instead of inventing caps.
- In rat-stack, [machines own finite domain retries and cancellation](/lore/lifecycles-are-machines). Effect's native fibers and schedules still exist.

Next, [validate HTTP responses](/lore/http-responses-need-validation) before treating concurrent provider work as successful.

For work that must survive a crash, read [every task needs an owner](/lore/every-task-needs-an-owner) and the [durability index](/lore/durability).

Return to the [Effect reading order](/lore/effect-basics#read-next).

## Sources

1. [Effect contributors. 2026. Effect. Effect 4.0.0.](https://github.com/Effect-TS/effect/blob/67ba4e46a11ccda0b6761578bfd22c04ae00167d/packages/effect/src/Effect.ts)
   Effect-TS. forEach limits traversal concurrency and retains input order; forkScoped attaches child interruption to the current Scope. Accessed 2026-10-06.

2. [Effect contributors. 2026. Stream.](https://github.com/Effect-TS/effect/blob/67ba4e46a11ccda0b6761578bfd22c04ae00167d/packages/effect/src/Stream.ts)
   Effect-TS. mapEffect performs effectful transformation; splitLines and runHead consume line-oriented output. Accessed 2026-10-06.

3. [Effect contributors. 2026. Consuming and transforming streams.](https://github.com/Effect-TS/effect/blob/67ba4e46a11ccda0b6761578bfd22c04ae00167d/ai-docs/src/03_stream/20_consuming-streams.ts)
   Effect-TS. Effectful stream transformation and consumption with explicit concurrency choices. Accessed 2026-10-06.

4. [rat-stack contributors. 2026. Subprocess sandbox ownership.](https://github.com/joelhooks/rat-stack/blob/65e9465f38092e24486392f22c08f45d61230c20/packages/capability/src/sandbox-subprocess.ts)
   rat-stack. A scoped stdin writer and decoded stdout stream; its producer queue is explicitly unbounded. Accessed 2026-10-06.

5. [rat-stack contributors. 2026. Traversal and child-ownership fixtures.](https://github.com/joelhooks/rat-stack/blob/4019cffbcbdb282d6f006854ff5b1f26d3b407e0/packages/capability/test/concurrency-ownership.test.ts)
   rat-stack. Generated traversals check peak active work, completion and order. A separate seam proves scoped child interruption. The bound property catches a planted unbounded traversal. Accessed 2026-10-06.

6. [Langton, Kit. 2026. Testing. Effect Solutions.](https://github.com/kitlangton/effect-solutions/blob/09f82e6c5c928e7232cd32daf04d7c6a830b63f7/packages/website/docs/08-testing.md)
   Kit Langton. Fiber examples frame the ownership question; the snapshot has no complete concurrency guide. Independent fixtures establish this page's claims. Accessed 2026-10-06.

## Lore on this page

- [Scopes release resources when work ends](/lore/scopes-own-resources)
- [Backpressure reaches the producer](/lore/backpressure-reaches-the-producer)
- [Workers answer requests; Durable Objects own named state](/lore/workers-answer-requests-durable-objects-own-named-state)

## Linked from

- Admission control protects recovery → Refuse or defer work that cannot be served usefully under current load. → [Read page](https://ratstack.sh/lore/admission-controls-new-work)

- Agent guide → An Effect stack so pure (aspirational) Kit Langton will blush. → [Read page](https://ratstack.sh/llms.txt)

- Backpressure reaches the producer → Bounded consumption helps only when upstream respects waiting or refusal. → [Read page](https://ratstack.sh/lore/backpressure-reaches-the-producer)

- Change log → What changed in the files served here, newest first. → [Read page](https://ratstack.sh/log)

- Checkpoints follow the promised commit → Persist state and progress together when one store can transact both. → [Read page](https://ratstack.sh/lore/checkpoints-follow-commits)

- Dead letters need a return path → Exhausted work needs durable evidence, an owner, and deliberate redrive. → [Read page](https://ratstack.sh/lore/dead-letters-need-a-return-path)

- durability → Keep a rat-stack app standing when a provider, pool, or dependency slows down or a worker dies. Use at any I/O boundary, connection pool, provider call, shared waiter, fan-out, flag, gate, or work that can outlive a request, or when asked "what happens when this is slow", "add a timeout", "make this durable", or "what survives a crash". → [Read page](https://ratstack.sh/skills/durability)

- Durability starts with a recovery record → A reading order for storage, replay, worker ownership, overload, and recovery tests. → [Read page](https://ratstack.sh/lore/durability)

- Durable work needs storage → A restarted worker cannot recover intent that existed only in memory. → [Read page](https://ratstack.sh/lore/durable-work-needs-storage)

- Effect basics → An Effect describes work, expected failures, and required services before a runtime executes it. → [Read page](https://ratstack.sh/lore/effect-basics)

- Effect is not BEAM → Fibers share a runtime; scope ownership does not supply isolated heaps or OTP restart strategies. → [Read page](https://ratstack.sh/lore/effect-is-not-beam)

- Every task needs an owner → Give child work a lifetime, a failure observer, and a recovery decision. → [Read page](https://ratstack.sh/lore/every-task-needs-an-owner)

- Full agent guide → An Effect stack so pure (aspirational) Kit Langton will blush. → [Read page](https://ratstack.sh/llms-full.txt)

- Golden signals expose overload → Measure latency, traffic, errors, and saturation, including failed work. → [Read page](https://ratstack.sh/lore/golden-signals-expose-overload)

- Queries need result bounds → Limit result cardinality before the driver materializes an oversized response. → [Read page](https://ratstack.sh/lore/queries-need-result-bounds)

- Rat Stack lore | rat-stack → Short, source-grounded notes on the ideas and decisions behind rat-stack. → [Read page](https://ratstack.sh/lore)

- Request lifetime is not durability → Worker waitUntil has a shared post-response allowance; required work needs a durable marker and redrive. → [Read page](https://ratstack.sh/lore/request-lifetime-is-not-durability)

- Restart must reconstruct known state → Execution recovery and state recovery need separate contracts. → [Read page](https://ratstack.sh/lore/restart-from-known-state)

- Retry load needs a budget → One retry owner must control attempts, delay, and aggregate load. → [Read page](https://ratstack.sh/lore/retry-load-needs-a-budget)

- Retry needs idempotent effects → Repeating an operation must preserve its intended observable result. → [Read page](https://ratstack.sh/lore/retry-needs-idempotency)

- Scopes release resources when work ends → Acquire resources lazily, keep their use inside the owner, and release them after success, failure, or interruption. → [Read page](https://ratstack.sh/lore/scopes-own-resources)

- Source change history (log.md) → The generated change log as Markdown. → [Read page](https://ratstack.sh/log.md)

- Timeouts bound waiting → A local timeout stops waiting; it does not prove the remote action never happened. → [Read page](https://ratstack.sh/lore/timeouts-bound-waiting)

## Unlinked mentions

- [durability](https://ratstack.sh/skills/durability) → Its structured fiber ownership is useful, but it does not rebuild lost state.¶

- [Effect 4 study: September 2026](https://ratstack.sh/resources/effect-4-reference-projects) → Shape: createEffectActor(machine) returns Effect\<EffectActor, never, R | Scope> and drives XState 6’s pure transition through xstate/durable on an Effect fiber.

- [Effect Days 2025: Building Effect 4](https://ratstack.sh/lore/effect-days-2025-building-effect-4-michael-arnaldi) → A fiber is independently running work.

- [Hosted agent interfaces](https://ratstack.sh/systems/agent-front-door) → Pending reads must not use a shared promise, fiber, latch, or response body.

- [profile-a-worker](https://ratstack.sh/skills/profile-a-worker) → In an Effect program, the fiber runtime (runLoop, makePrimitiveProto, succeedWith) is the floor every request pays.

- [Scopes release resources when work ends](https://ratstack.sh/lore/scopes-own-resources) → A fiber is an independently running Effect.
