Errors Suppressed When Using `addFinalizer` in Effect Program

why errors blocked when using addFinalizer ?

import {
  Config,
  Console,
  Context,
  Data,
  Effect,
  Fiber,
  HashMap,
  Layer,
  Logger,
  Option,
  pipe,
  Queue,
  Redacted,
  Ref,
  Schedule,
} from "effect";

export class TestService extends Effect.Tag("TestService")<
  TestService,
  {
    action: () => Effect.Effect<number>;
  }
>() {
  accessor = true;
}

export const TestServiceLive = Layer.effect(
  TestService,
  Effect.gen(function* () {
    return {
      action: () =>
        Effect.gen(function* () {
          const result = yield* Effect.promise(async () => {
            throw new Error("this is Error");
            return 123;
          });
        }),
    };
  }),
);

const program = Effect.gen(function* () {
  yield* Effect.log("start");
  const queue = yield* Queue.unbounded<number>();

  const fb1 = yield* Effect.fork(
    Effect.gen(function* () {
      const item = yield* Queue.take(queue);
    }).pipe(Effect.repeat(Schedule.forever)),
  );

  yield* Effect.addFinalizer(() => {
    return Effect.gen(function* () {
      yield* fb1;
    });
  });

  console.log("hello");

  const testService = yield* TestService;

  yield* testService.action();
  yield* Effect.log("program finished");
});

const runnable = program.pipe(
  Effect.provide(TestServiceLive),
  Effect.provide(Logger.pretty),
  Effect.scoped,
);
Effect.runPromise(runnable).catch(console.error);
Was this page helpful?