Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript

Home Page:https://effect.website

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using timeout in runFork causes warning about unhandled error

maxgr0 opened this issue · comments

What version of Effect is running?

3.1.3

What steps can reproduce the bug?

const task = () => Effect.fail(new Error('test')) as Effect.Effect<string, Error>;

const makeTaskHandler = (testEventEmitter: EventEmitter) =>
  Effect.gen(function* () {
    const runHandler = Runtime.runFork(yield* Effect.runtime<never>());

    const processor = () =>
      new Promise((resolve, reject) =>
        runHandler(
          pipe(
            task().pipe(Effect.timeout(Duration.millis(200))),
            Effect.tapBoth({
              onSuccess: () => Effect.logInfo('success'),
              onFailure: () => Effect.logError('failure'),
            }),
            Effect.matchEffect({
              onSuccess: (result) => Effect.sync(() => resolve(result)),
              onFailure: (exception) =>
                Effect.sync(() =>
                  reject(
                    exception instanceof Error ? exception : new Error('Handler failed'),
                  ),
                ),
            }),
          ),
        ),
      );

    testEventEmitter.on('test-event', processor);
  });

const program = Effect.gen(function* () {
  const eventEmitter = new EventEmitter();

  yield* makeTaskHandler(eventEmitter);

  yield* Effect.sleep('1 second');

  eventEmitter.emit('test-event');
});

Effect.runPromise(program.pipe(Logger.withMinimumLogLevel(LogLevel.Debug)));

What is the expected behavior?

When an effect is wrapped within an Effect.timeout and fails because of any other reason (before the timeout kicks in), it should not lead to a warning about an unhandled error within a Fiber.

When trying to debug this behavior, I removed the .pipe(Effect.timeout(Duration.millis(200))) and got the normal message which was expected:

timestamp=2024-05-09T21:07:53.136Z level=ERROR fiber=#1 message=failure

What do you see instead?

Effect logs a message about a Fiber terminating with an unhandled error.

timestamp=2024-05-09T21:07:53.132Z level=DEBUG fiber=#2 message="Fiber terminated with an unhandled error"
timestamp=2024-05-09T21:07:53.136Z level=ERROR fiber=#1 message=failure

Additional information

In case the timeout itself kicks in, the message about an unhandled error is also not coming up. Only if the wrapped effect fails before the timeout kicks in.