How to correctly unsubscribe from EventStoreStreamCatchUpSubscription

I am trying to unsubscribe from the stream using subscription handler Stop method:

EventStoreCatchUpSubscription subscription = null;

        subscription = connection.SubscribeToStreamFrom(stream, position, false, (s, e) =>

            {

               if (IsTheOne(e))

               {

                   DoSomething(e);

                   try {

                      subscription.Stop(TimeSpan.FromSeconds(30));

                   } catch (Exception exc) {

                       Log(exc);

                   }

               }

            });

But every time after the wait timer elapses in Stop method i receive ‘System.TimeoutException: Couldn’t stop EventStoreStreamCatchUpSubscription in time’. What am I doing wrong?

If you call it from outside of your handler does it stop properly? Just curious.

Greg

Also do you know if you’re in the “live” subscription part of your stream or reading history?

yes, that was it. Transferred Stop part to another thread, and it works now. Don’t know if this is ok.

EventStoreCatchUpSubscription subscription = null;

        subscription = connection.SubscribeToStreamFrom(stream, position, false, (s, e) =>

            {

               if (IsTheOne(e))

               {

                   DoSomething(e);

                   Task.Factory.StartNew(() => {

                                try {

                                    subscription.Stop(TimeSpan.FromSeconds(10));

                                } catch (TimeoutException exc) {

                                    log.Error(exc);

                                }});

               }

            });

My guess is its waiting on your handler to finish to actually stop :slight_smile:

yes, I am in a live subscription. I simply wait for the answer to the command I’ve sent.

The relevant code is here: https://github.com/EventStore/EventStore/blob/dev/src/EventStore.ClientAPI/EventStoreCatchUpSubscription.cs#L141

And yes its waiting for your handler to end. I will add an overload that marks stop but does not block on it that you can use in your handler.

Cheers,

Greg

yes, that would be much better.

Done. https://github.com/EventStore/EventStore/pull/153

cool, thanks. cloning…

returning to that question.

Is this really safe to stop the subscription from the handler? Won’t there be a situation when the subscription variable will be null? I mean the situation when handler will be called earlier than the SubscribeToStreamFrom will return.

EventStoreCatchUpSubscription subscription = null;

        subscription = connection.SubscribeToStreamFrom(stream, position, false, (s, e) =>

            {

               if (IsTheOne(e))

               {

                   DoSomething(e);

                   subscription.Stop();

               }

            });

discard this question :slight_smile: just saw the EventStoreCatchUpSubscription is passed to the handler… don’t know how I missed that :slight_smile:

what about disconnecting from catch-up mode (reading history)? Stop methods seems to work only for live subscription.

It appears a check got taken out in a refactor somewhere along the way. This PR should resolve the issue.

https://github.com/EventStore/EventStore/pull/160

Someone else should approve it quickly to dev (we generally don’t self accept prs :)). Feel free to pull it and test though.

Cheers,

Greg

Doesn’t build :). _stoped is private and cannot be accessed in inherited class.

check now not sure how half the change was there.

still requires some changes:

  • wrong loop condition, should be - while (!done && !_stop) :slight_smile:

  • this should be also applied for EventStoreAllCatchUpSubscription class as well, not only EventStoreStreamCatchUpSubscription

R.

Hi Raimondas,

See commit 84ad492 - this should now be doing what you need.

Cheers,

James

Great, thanks. Will check it.