Skip to content

3.0.2 Unable to retry singleton handler constructor after exception #291

Description

@sa-vg

When a handler constructor throws an exception, a subsequent request hangs indefinitely.

Mediator 3.0.2 dotnet 10

Steps to reproduce

  1. Handler constructor throws an exception on the first attempt.
  2. Send another request through Mediator.

Expected behavior

The next request should retry handler creation or return an exception.

Actual behavior

The second attempt enters an infinite spin wait.

This appears to be related to FastLazyValue remaining in the INITING state after an exception.

Reproduction

#:property TargetFramework=net10.0
#:package Mediator.Abstractions@3.0.2
#:package Mediator.SourceGenerator@3.0.2

#:package Microsoft.Extensions.DependencyInjection@10.0.0

using Mediator;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddMediator();

await using var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();

Console.WriteLine("first call");
try
{
    await mediator.Send(new Request());
}
catch (Exception ex)
{
    Console.WriteLine($"first call failed: {ex.GetType().Name}: {ex.Message}");
}

Console.WriteLine("second call");

await mediator.Send(new Request()); //hangs forever

Console.WriteLine("second call finished - ok");

public sealed record Request() : IRequest<string>;

public sealed class Handler : IRequestHandler<Request, string>
{
    private static int _constructorAttempts;

    public Handler()
    {
        var attempt = Interlocked.Increment(ref _constructorAttempts);
        Console.WriteLine($"Handler constructor attempt - {attempt}");

        if (attempt == 1)
        {
            throw new InvalidOperationException("Failing first constructor call");
        }

        Console.WriteLine("Handler constructor succeeded");
    }

    public ValueTask<string> Handle(Request request, CancellationToken cancellationToken)
        => ValueTask.FromResult("result");
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions