0

In the RunAsync(CancellationToken) background method, current code is spawning new thread and also having some long running tasks inside while(true) loop.

The web api controller is exposed to provide cancel request and then using enqueue / dequeue, this request is accessed in the RunAsync(cancellationToken) while(true) loop.

I just can't make connection between the web api controller receiving this cancel request with the cancellationtoken passed down to the thread running inside runasync method.

RunAsync(cancellationToken) 
{ 
   while(True)
   { 
     new thread(cancellationtoken)
   } 
} 

One thing I am pretty sure is that there is no connection between the cancel request somehow invoked by user and the cancellationToken as argument of RunAsync() as shown in the code above. It seems they are not connected. we don't want to get out of the forever loop in RunAsync() background upon user cancel request, which is only for the specific thread run.
Please guide me to correct direction to design cancel request terminating the thread.

swcraft
  • 2,014
  • 3
  • 22
  • 43
  • 1
    The cancellation token passed to RunAsync is used by Service Fabric to signal that the service instance is about to shutdown so you can take action based on that. You cannot control that. If you want to act on external events in RunAsync you will have to create a mechanism for that yourself. Without additional details from your side it is hard to give some ideas. – Peter Bons Oct 27 '18 at 07:40
  • 1
    You should link your other question, so people have enough information to help you – Diego Mendes Oct 27 '18 at 14:18
  • @PeterBons Please read the previous question linked which I asked to more detail, thanks for the information you provided above..that confirmed one of the questions I had in mind. I wish Service Fabric provides more thorough document where I can learn such.. https://stackoverflow.com/questions/52998828/store-cancellation-tokens-in-service-fabric-services – swcraft Oct 28 '18 at 14:59
  • @JP_medevice it is documented in the section about [the services lifecycle](https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-lifecycle) – Peter Bons Oct 29 '18 at 07:23

1 Answers1

1

As suggested by Peter Bons, the cancellation token passed to the RunAsync, is created and managed by Service Fabric to tell a service that it is being shut down. You should watch for this cancellation to make a graceful shutdown of your services when service fabric wants to to upgrade or move the service between nodes.

Other point is, you don't cancel CancellationToken, you cancel CancellationTokenSource, so in this case, any thread created by your code, should create a their ownCancellationTokenSource for each thread to be cancelled individually and the token generated by this CancellationTokenSource must be provided to the thread so it knows when it has been cancelled.

Another point is, if you want make it smooth, you should create a linked CancellationTokenSource using CancellationTokenSource.CreateLinkedTokenSource(SFTokenPassedOnRunAsync) so that when Service Fabric wants to shutdown the service, the main cancellation token created will cancel any child operations, otherwise you have to handle that from your code.

Regarding the main question, You can only cancel an Operation within the same process that created the CancellationTokenSource, The easier way is expose the an Endpoint in the Service (Via remoting or via Rest API) that will receive a call, find the token and cancel the operation.

Would be something like:

  • The service create the CancellationTokenSource and start the new thread with the token generated
  • The CancellationTokenSource will be stored in a static variable visible within the sameprocess, so that the API can see it
  • The Api call will get this CancellationTokenSource and call Cancel()

In case it is a list of running operations(multiple threads), you can store the CTS in a Dictionary and give IDs to each operation, then you can find the CTS based on the ID of the operation.

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
  • Thanks for the thorough comment on this topic.. one thing I am not so sure yet is when multiple nodes run, even if API end point is singleton, how I can route the cancellation request to the right process in specific partition as you mentioned in the other thread..https://stackoverflow.com/questions/52998828/store-cancellation-tokens-in-service-fabric-services – swcraft Oct 29 '18 at 14:15