Actress icon indicating copy to clipboard operation
Actress copied to clipboard

PostAndReply throws timeout exception for default values (int = 0 for example)

Open DanielLidstromTicket opened this issue 5 years ago • 3 comments

This code fails with TimeoutException MailboxProcessor PostAndReply timed out:

void Main()
{
    var printerAgent = MailboxProcessor.Start<Message>(async mb =>
    {
        var sharedResource = 0;
        while (true)
        {
            var value = await mb.Receive();
            value.Dump("Received");
            if (value is Fetch f)
            {
                f.Channel.Reply(sharedResource);
            }
        }
    });
    
    using (printerAgent)
    {
        printerAgent.DefaultTimeout = int.MaxValue;
        printerAgent.PostAndReply<int>(channel => new Fetch(channel)).Dump("Reply");
    }
}

// Define other methods and classes here
public abstract class Message
{
}

public class Fetch : Message
{
    public Fetch(IReplyChannel<int> channel)
    {
        Channel = channel;
    }
    
    public IReplyChannel<int> Channel { get; }
}

I am not sure why the exception is thrown. Why are default values not accepted?

DanielLidstromTicket avatar Mar 30 '20 13:03 DanielLidstromTicket

This is an unfortunate translation of the original F# code. The F# version uses the option type to indicate whether the message was handled and replied to within the given timeout. There's no such (built-in) type in .NET other than Nullable<T>, which only works for value types.

@kthompson Are you OK with changing the signature of TryPostAndReply? We could make it more "C# like" and return a bool and put the return value into an out variable.

inosik avatar Mar 30 '20 15:03 inosik

I don't actively work on this but would be happy to accept any PRs. If that is something you are interested in let me know.

kthompson avatar Mar 30 '20 15:03 kthompson

I believe the problem is the following.

johnpage-agixis avatar Feb 22 '21 05:02 johnpage-agixis