Skip to content

Fix: return Task.CompletedTask when Saga not found in TimeoutMessage handlers #1386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Damix48
Copy link

@Damix48 Damix48 commented Apr 24, 2025

I was trying the Saga example provided in the docs but the generation doesn't not compile because there an empty return instead of a return Task.Complete.

I'm not 100% sure that this is the correct fix.

    // START: OrderTimeoutHandler477439855
    public class OrderTimeoutHandler477439855 : Wolverine.Runtime.Handlers.MessageHandler
    {
        private readonly Microsoft.Extensions.Logging.ILogger<SagaTest.Order> _logger;
        private readonly Wolverine.Persistence.Sagas.InMemorySagaPersistor _inMemorySagaPersistor;

        public OrderTimeoutHandler477439855(Microsoft.Extensions.Logging.ILogger<SagaTest.Order> logger, Wolverine.Persistence.Sagas.InMemorySagaPersistor inMemorySagaPersistor)
        {
            _logger = logger;
            _inMemorySagaPersistor = inMemorySagaPersistor;
        }



        public override async System.Threading.Tasks.Task HandleAsync(Wolverine.Runtime.MessageContext context, System.Threading.CancellationToken cancellation)
        {
            // The actual message body
            var orderTimeout = (SagaTest.OrderTimeout)context.Envelope.Message;

            string sagaId = context.Envelope.SagaId ?? orderTimeout.Id;
            if (string.IsNullOrEmpty(sagaId)) throw new Wolverine.Persistence.Sagas.IndeterminateSagaStateIdException(context.Envelope);
            var order = _inMemorySagaPersistor.Load<SagaTest.Order>(sagaId);
            if (order == null)
            {
                return; <-- HERE should return Task.Completed
            }

            else
            {

                // The actual message execution
                await order.Handle(orderTimeout, _logger).ConfigureAwait(false);

                // Delete the saga if completed, otherwise update it
                if (order.IsCompleted())
                {
                    _inMemorySagaPersistor.Delete<SagaTest.Order>(sagaId);
                }

                else
                {
                    _inMemorySagaPersistor.Store<SagaTest.Order>(order);
                }

                // No unit of work
            }

        }

    }

    // END: OrderTimeoutHandler477439855

@Damix48 Damix48 changed the title Fix: return Task.Completed when Saga not found in TimeoutMessage Fix: return Task.Completed when Saga not found in TimeoutMessage handlers Apr 24, 2025
@jeremydmiller
Copy link
Member

@Damix48 Thanks for taking that on, but it's more complicated than that. that change might fix that particular case, but could easily break others. I'll take a look at it by 3.13. Which example are you talking about?

You have to look up the stack of Frame operations to "know" if there are any async operations that would make the method async/await or if you should return Task.CompletedTask

@Damix48
Copy link
Author

Damix48 commented Apr 25, 2025

This example: https://wolverinefx.net/guide/durability/sagas.html#your-first-saga

I just copy paste but without the Marten stuff

@Damix48
Copy link
Author

Damix48 commented Apr 25, 2025

The handler generated derives from MessageHandler that has

public abstract Task HandleAsync(MessageContext context, CancellationToken cancellation);

so when is overridden, I think it should always return a Task.CompletedTask. But as you said probably this fix only this case.

@Damix48 Damix48 changed the title Fix: return Task.Completed when Saga not found in TimeoutMessage handlers Fix: return Task.CompletedTask when Saga not found in TimeoutMessage handlers Apr 25, 2025
@jeremydmiller jeremydmiller modified the milestone: 4.0 May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants