Skip to content

[mono] Generic Default Interface Method crash with Environment.StackTrace #60486

Closed
@bholmes

Description

@bholmes

Description

Calling Environment.StackTrace from a default interface method of a generic interface causes a crash in the Mono runtime.

Reproduction Steps

Compile and run the following program with the Mono runtime...

using System;

namespace DefItf
{
    public interface IPublisher<out TData>
    {
        event Action<TData> OnPublish;
    }

    public interface ISubscriber<T>
    {
        void OnReceiveSubscription(T data);

        void Subscribe(IPublisher<T> publisher)
        {
            InternalSubscribe(this, publisher);
        }

        void Unsubscribe(IPublisher<T> publisher)
        {
            InternalUnsubscribe(this, publisher);
        }

        protected static void InternalSubscribe(ISubscriber<T> subscriber, IPublisher<T> publisher)
        {
            publisher.OnPublish += subscriber.OnReceiveSubscription;
            Console.WriteLine(Environment.StackTrace.ToString());
        }

        protected static void InternalUnsubscribe(ISubscriber<T> subscriber, IPublisher<T> publisher)
        {
            publisher.OnPublish -= subscriber.OnReceiveSubscription;
            Console.WriteLine(Environment.StackTrace.ToString());
        }
    }

    public class PubTest :IPublisher<InputData>
    {
        public event Action<InputData> OnPublish;

        public void Call() => OnPublish?.Invoke(new InputData());
    }

    public class InputData
    {
        public int i;
    }

    public class Program : ISubscriber<InputData>
    {
        static void Main(string[] args)
        {
            new Program().Start();
        }
        
        // Start is called before the first frame update
        public void Start()
        {
            var pub = new PubTest();
            var sub = (ISubscriber<InputData>)this;
            sub.Subscribe(pub);
            pub.Call();
            sub.Unsubscribe(pub);
        }

        public void Subscribe(IPublisher<InputData> publisher)
        {
            ISubscriber<InputData>.InternalSubscribe(this, publisher);
        }

        public void OnReceiveSubscription(InputData data)
        {
            Console.WriteLine($"do something");
        }
    }
}

Expected behavior

Program completes without crashing and the output is written to the screen.

Actual behavior

Program crashes.

Regression?

Not a regression

Known Workarounds

No known workarounds.

Configuration

  • Tested with Mono runtime build from code, main branch 56d807d.
  • Verified on Windows and Linux x64

Other information

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions