Code-Sharp / WampSharp

A C# implementation of WAMP (The Web Application Messaging Protocol)
http://wampsharp.net
Other
385 stars 84 forks source link

Unknown subscription #199

Closed YohskDista closed 7 years ago

YohskDista commented 7 years ago

Hi again,

I had a strange problem on some machine. I have a local connection WampSharp on my PC between two software (I will call it A and B). The software "A" is a router who create the DefaultWampHost and register himself as a callee :

if (Host != null)
    Host.Dispose();

// Creation of the WampHost with authentication
Host = new DefaultWampHost(ServerAddress);
Host.Open();

Logger.Trace("Connection wamp host ---> {0}", ServerAddress);

Realm = Host.RealmContainer.GetRealmByName(REALM_NAME);

RegisterCallee = Realm.Services.RegisterCallee(this).Result;

Realm.SessionCreated += Realm_SessionCreated;
Realm.SessionClosed += Realm_SessionClosed;

this is the code when it's close :

RegisterCallee.DisposeAsync();

if (Realm != null)
{
     Realm.SessionCreated -= Realm_SessionCreated;
      Realm.SessionClosed -= Realm_SessionClosed;
}

Host?.Dispose();
Host = null;
Realm = null;

Now the "B" application connect to this WampHost and subscribe to different topic. The problem is when I close the WampHost the software "B" must be exit too. Then, I got an error before the closing of the software "B" :

<InnerException>
    <ExceptionType>System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
    <Message>Unknown subscription: 950784773309608</Message>
   <StackTrace>   at System.Threading.Tasks.TaskExceptionHolder.Finalize()  
00000123    0.41678721  [2580] 
    </StackTrace>
    <ExceptionString>System.Exception: Unknown subscription: 950784773309608</ExceptionString></InnerException>

When i got the connection broken I make different actions in this order :

Do you have some idea what could it be ?

Thank you Leo

darkl commented 7 years ago

Some code for reproducing this would be helpful.

Elad

YohskDista commented 7 years ago

Here is the code for the program "B" who connects to the program "A"

Creation of the channel

            try
            {

                // Create channel
                Channel = new DefaultWampChannelFactory().CreateJsonChannel(SERVER_ADDRESS, REALM_NAME);
                Channel.RealmProxy.Monitor.ConnectionError += Monitor_ConnectionError;
                Channel.RealmProxy.Monitor.ConnectionBroken += Monitor_ConnectionBroken;
                Channel.RealmProxy.Monitor.ConnectionEstablished += Monitor_ConnectionEstablished;

                // Try connection
                Channel.Open().Wait(5000);

                // Connection OK => Get proxy / Create service
                Service = Channel.RealmProxy?.Services?.GetCalleeProxy<IWampMediator>();

                this.Subscribes = new List<IDisposable>()
                {
                    Channel.RealmProxy.Services
                        .GetSubject<Data>(path1)
                        .Subscribe(data =>
                        {
                            lock (this.ServiceLock)
                            {
                                if (this.IsServiceReady)
                                    this.ChangeDataEvent1?.Invoke(this, new DataEventArgs1(data));
                            }
                        }, OnSubscribeError),

                    Channel.RealmProxy.Services
                        .GetSubject<Data>(path2)
                        .Subscribe(data =>
                       {
                            lock (this.ServiceLock)
                            {
                                if (this.IsServiceReady)
                                    this.ChangeDataEvent2?.Invoke(this, new DataEventArgs2(data));
                            }
                        }, OnSubscribeError)
                };
            }
            catch (Exception e)
            {
        System.Diagnostics.Trace.TraceError("The connection could not be etablished : " + e.StackTrace);
            }

Event of connection :

 private void Monitor_ConnectionBroken(object sender, WampSharp.V2.Realm.WampSessionCloseEventArgs e)
        {
            DisposeConnection();
        }

        private void Monitor_ConnectionError(object sender, WampSharp.Core.Listener.WampConnectionErrorEventArgs e)
        {
        System.Diagnostics.Trace.TraceError("The connection has an error");
        }

Dispose the connection when it is broken :

public void DisposeConnection()
        {
            lock(this.ServiceLock)
            {
                try
                {
                    if (this.Subscribes?.Count > 0)
                        this.Subscribes.ForEach(T => T.Dispose());

                    if (Channel != null)
                    {
                        Channel.RealmProxy.Monitor.ConnectionError -= Monitor_ConnectionError;
                        Channel.RealmProxy.Monitor.ConnectionBroken -= Monitor_ConnectionBroken;
                        Channel.RealmProxy.Monitor.ConnectionEstablished -= Monitor_ConnectionEstablished;
                        Channel.Close("Disconnect", new GoodbyeDetails());
                        Channel = null;
                    }

                    Service = null;

                    System.Diagnostics.Trace.TraceError("********* Disposed ********");
                }
                catch (NullReferenceException)
                {
                    System.Diagnostics.Trace.TraceError("Dispose() => is closed");
                }
                catch (WampConnectionBrokenException)
                {
                    System.Diagnostics.Trace.TraceError("Dispose() => The connection is broked");
                }
            }
        }

Thank you Leo

darkl commented 7 years ago

Are you using Framework 4.0?

YohskDista commented 7 years ago

Yes I do, excuse me I forgot to communicate this thing

darkl commented 7 years ago

I just published a new version to NuGet in which this issue should be resolved. Can you please check it? Elad

YohskDista commented 7 years ago

I have tested this morning and it seems to be ok, I haven't the exception anymore. I continue the tests and I let you know if I find something else.

Thank you for your quick response ! Leo