ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.25k stars 744 forks source link

v10 - Inconsistent cancellation of CancellationToken in Subscriptions #4857

Closed cts-tradeit closed 2 years ago

cts-tradeit commented 2 years ago

Is there an existing issue for this?

Describe the bug

Hi, we are using HC v10 (10.5.3) and we came across a strange behaviour.

Imagine this:

[SubscribeAndResolve]
public async Task<IAsyncEnumerable<Book>> GetBooksAsync( 
      /* other services */
      [Service] IMessageSubscriber subscriber,
      CancellationToken token) 
{
      /*omitted code using await*/  

       token.Register(() => { 
          /*omitted code that logs cancellation and does other things*/ 
       });

      IAsyncEnumerabel<Book> bookStream = await subscriber.SubscribeAsync<string, Book>("foo", token);  
      return bookStream;
}

If VS2022 debugger is attached to the donet.exe process running given DLL. Action in Register gets called (and breakpoint stops in there). However, when VS2022 debugger is not atteched, action is never called.

Steps to reproduce

  1. Have a resolver using [SubscribeAndResolve] that takes CancellationToken
  2. Register callback to the token
  3. Trigger subscription using your client (we are using Apollo)
  4. Terminate the subscription client side
  5. Callback never gets called

Relevant log output

No response

Additional Context?

If fixed in subsequent releases, as this is large enterprise project we cannot upgrade to v11 or v12 easily.

Product

Hot Chocolate

Version

10.3.5

michaelstaib commented 2 years ago

Hey there,

Hot Chocolate 10 is no longer supported by us. Version 11 and 12 are already out and we are at the moment completely rehauling subscriptions with Version 13 which will be coming out this year.

I recommend upgrading to version 12 latest.

cts-tradeit commented 2 years ago

Hi Michael, this is not an option for us at this moment. Is there any better way to detect termination of the subscription?

michaelstaib commented 2 years ago

I do not know. We did a break with 10 and everything from 11 on is essentially based on the same API layer. 10 is not and I do not have the APIs anymore on my stack so to say.

So, I cannot help with 10.

As a project, we can only help with 11, 12, and 13.

But you can have a look at the old implementation here: https://github.com/ChilliCream/hotchocolate/tree/main-version-10

michaelstaib commented 2 years ago

I think actually that this was a bug in 10 that we fixed with 11. Let me check.

michaelstaib commented 2 years ago

Jup, there are a couple of issues around cancellation that were fixed in 11. Just double checked.

cts-tradeit commented 2 years ago

Okay, and speaking of v11 and newer, can you post a link to sample or some kind of guide on how to properly detect disconection inside resolver? I had a swift look on subscription docu page for v11 and did not see anything related to tokens (or other kind of mechanism).

michaelstaib commented 2 years ago

In 11 and beyond you can use the cancellation token.

michaelstaib commented 2 years ago

Also in 11 and 12, you have the socket interceptor to detect connect and disconnect. In 13 we have reworked the interceptor to track even more events. Have a look at this: https://github.com/ChilliCream/hotchocolate/blob/main/src/HotChocolate/AspNetCore/src/AspNetCore/ISocketSessionInterceptor.cs