SetCancelCallback passes in a callback to be called when the RPC is canceled for whatever reason (streaming calls have OnCancel instead).
This is an advanced and uncommon use with several important restrictions. This function may not be called more than once on the same RPC.
If code calls SetCancelCallback on an RPC, it must also call ClearCancelCallback before calling Finish on the RPC controller. That method makes sure that no cancellation callback is executed for this RPC beyond the point of its return. ClearCancelCallback may be called even if SetCancelCallback was not called for this RPC, and it may be called multiple times. It must be called if SetCancelCallback was called for this RPC.
The callback should generally be lightweight and nonblocking and primarily concerned with clearing application state related to the RPC or causing operations (such as cancellations) to happen on dependent RPCs.
If the RPC is already canceled at the time that SetCancelCallback is called, the callback is invoked immediately.
The cancellation callback may be executed concurrently with the method handler that invokes it but will certainly not issue or execute after the return of ClearCancelCallback. If ClearCancelCallback is invoked while the callback is already executing, the callback will complete its execution before ClearCancelCallback takes effect.
To preserve the orderings described above, the callback may be called under a lock that is also used for ClearCancelCallback and ServerContext::IsCancelled, so the callback CANNOT call either of those operations on this RPC or any other function that causes those operations to be called before the callback completes.