ServiceBrokerListener icon indicating copy to clipboard operation
ServiceBrokerListener copied to clipboard

ObjectDisposedException while calling Stop()

Open fknx opened this issue 7 years ago • 3 comments

Hi,

I'm using the following code to react to the NotificationProcessStopped event:

// Listener contains the SqlDependencyEx instance
Listener.NotificationProcessStopped += (sender, args) =>
{
   Task.Run(async () =>
   {
      await Task.Delay(5000);
      AttemptReconnect();
   });
};

private void AttemptReconnect()
{
   try
   {
      Listener.Stop();
      Listener.Start();
   }
   catch
   {
      Task.Run(async () =>
      {
         await Task.Delay(5000);
         AttemptReconnect();
      });
   }
}

If I run this code and stop the SQL Server to simulate a disconnect, I am receiving an ObjectDisposedException: The CancellationTokenSource has been disposed in the Stop() method when the _threadSource.Token.IsCancellationRequested property is read in line 567 on the second reconnect attempt (and every reconnect attempt thereafter).

When I change this to _threadSource.IsCancellationRequested everything works fine.

fknx avatar Nov 15 '17 14:11 fknx

So first time the token stops, then second time there is no token because it was disposed and now you want to use the token source to check if cancellation is requested?

From this if ((_threadSource == null) || (_threadSource.Token.IsCancellationRequested)) to this if ((_threadSource == null) || (_threadSource.IsCancellationRequested))

Siliconrob avatar Nov 15 '17 15:11 Siliconrob

Yes that was the idea, since accessing the _threadSource.Token immediately causes the ObjectDisposedException.

fknx avatar Nov 15 '17 16:11 fknx

I would suggest you submit a pull request with this change, and tag @dyatchenko . They are the only one that can make edits.

Siliconrob avatar Nov 15 '17 17:11 Siliconrob