Skip to content

Exit Kestrel heartbeat thread immediately on stop #47600

@JamesNK

Description

@JamesNK

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Noticed in #47580

Kestrel creates a thread and sleeps for a second in its heartbeat:

https://github.com/dotnet/aspnetcore/blob/f295d1f75cd79c6b3abd4f27b8f4b14a58186e7a/src/Servers/Kestrel/Core/src/Internal/Infrastructure/Heartbeat.cs

When Kestrel stops there isn't a way to immediately stop the thread. Thread.Sleep doesn't have cancellation.

We could change it to an AutoResetEvent + OneWait(interval). When Kestrel stops the event can be triggered and exit immediately.

However, one thing to consider is making the heartbeat as reliable as possible. Thread.Sleep vs waiting on and event handle + timeout might have different behavior if the server is under heavy load.

Expected Behavior

The heartbeat thread immediately exits when Kestrel is stopped.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions