Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSE subscription makes HotChocolate not being shutdown gracefully #6698

Open
1 task done
sunghwan2789 opened this issue Nov 14, 2023 · 7 comments · May be fixed by #7007
Open
1 task done

SSE subscription makes HotChocolate not being shutdown gracefully #6698

sunghwan2789 opened this issue Nov 14, 2023 · 7 comments · May be fixed by #7007
Labels
Area: Subscriptions Issue is related to Subscriptions or a Subscription Provider 🐛 bug Something isn't working 🌶️ hot chocolate repro-validated

Comments

@sunghwan2789
Copy link
Contributor

sunghwan2789 commented Nov 14, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Product

Hot Chocolate

Describe the bug

If a SSE subscription alives, HotChocolate does not shutdown gracefully.

Sending Ctrl+C (SIGINT), Kestrel requests connections closed, but HotChocolate keeps connections open and RequestAborted is false.

image

Steps to reproduce

  1. git clone https://github.com/sunghwan2789/hotchocolate.git --branch repro/sse-hangs --single-branch
  2. dotnet run --project Server
  3. Wait subscription started, and send Ctrl+C (SIGINT)

Relevant log output

No response

Additional Context?

No response

Version

14.0.0-p.9

@sunghwan2789 sunghwan2789 added the 🐛 bug Something isn't working label Nov 14, 2023
@michaelstaib
Copy link
Member

Thank you for reporting this.

@michaelstaib
Copy link
Member

Can you create a repro?

@michaelstaib michaelstaib added 🌶️ hot chocolate Area: Subscriptions Issue is related to Subscriptions or a Subscription Provider labels Nov 14, 2023
@sunghwan2789
Copy link
Contributor Author

Updated reproduction, thanks

@cmeeren
Copy link
Contributor

cmeeren commented Feb 20, 2024

Also being hit by this. This means we can't reliably update our API, because shutdown hangs whenever there's an active connection. Hoping this will get fixed soon!

@sunghwan2789
Copy link
Contributor Author

sunghwan2789 commented Mar 20, 2024

Now, I think this is not a bug, but a user-level mistake: the resolver is responsible for completing the subscription on application stopping.

Kestrel also does not abort the client connection immediately:

using System.Runtime.CompilerServices;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpContextAccessor();

var app = builder.Build();

app.MapGet("/", Bar);

_ = DoSubscription(app.Lifetime.ApplicationStopped);

app.Run();

async Task DoSubscription(CancellationToken stopped) {
    var client = new HttpClient();

    while (!stopped.IsCancellationRequested) {
        try {
            await foreach (var response in client.GetFromJsonAsAsyncEnumerable<int>("http://localhost:5241/", stopped)) {
                Console.WriteLine(response);
            }
        } catch (Exception ex) {
            Console.WriteLine("retry... " + ex);
            await Task.Delay(1000, stopped);
        }
    }
}

async IAsyncEnumerable<int> Bar(IHttpContextAccessor contextAccessor, [EnumeratorCancellation] CancellationToken cancellationToken) {
    var context = contextAccessor.HttpContext;
    Console.WriteLine(context?.TraceIdentifier);
    var i = 0; 
    while (!cancellationToken.IsCancellationRequested) {
        yield return i++;
        await Task.Delay(1000, cancellationToken);
    }
}

@cmeeren
Copy link
Contributor

cmeeren commented Mar 20, 2024

the resolver is responsible for completing the subscription on application stopping

How can this be done? I can't find any information about that in the HC subscription docs.

@sunghwan2789
Copy link
Contributor Author

sunghwan2789 commented Mar 21, 2024

Updated the reproduction and found that HC does not send a complete event and keep the connection open when an exception is thrown.

Now, I am sure it is a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Subscriptions Issue is related to Subscriptions or a Subscription Provider 🐛 bug Something isn't working 🌶️ hot chocolate repro-validated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants