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

Use reset event in Kestrel heartbeat to quickly stop thread #47619

Merged
merged 3 commits into from
Apr 13, 2023

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Apr 10, 2023

Fixes #47600

Replace Thread.Sleep(1000) with ManualResetEventSlim.

@JamesNK JamesNK changed the title Use WaitHandle in Kestrel heartbeat to quickly exit on stop Use WaitHandle in Kestrel heartbeat to quickly exit thread on stop Apr 10, 2023
@JamesNK
Copy link
Member Author

JamesNK commented Apr 11, 2023

@mconnew This PR updates Kestrel heartbeat to exit immediately and stop before Kestrel starts shutting down. That removes the need to use _stopped to suppress error from heartbeat that you added.

Can you take a look and check that your original issue is still solved?

@JamesNK JamesNK requested a review from Tratcher April 11, 2023 03:05
@JamesNK
Copy link
Member Author

JamesNK commented Apr 11, 2023

@Tratcher I moved heartbeat stop to the start of shutdown. Can you double check this is ok?

@mconnew
Copy link
Member

mconnew commented Apr 11, 2023

@JamesNK, it looks like it should resolve it as Dispose() won't return until it will no longer call OnHeartbeat. There's nothing preventing a later change bringing the issue back though as OnHeartbeat is internal so callable. In the catch block you could call WaitOne with a timeout of 0. You can then check the true/false value that's immediately returned to know if it's been set and still avoid logging. That would also enable you to keep the unit test you've removed.

Also, why not ManualResetEventSlim? ManualResetEvent allocates a native win32 handle and the Slim variant is all managed.

@JamesNK
Copy link
Member Author

JamesNK commented Apr 11, 2023

OnHeartbeat is internal so callable

OnHeartbeat is only internal for unit tests. It won't be called after the heartbeat is disposed.

Also, why not ManualResetEventSlim? ManualResetEvent allocates a native win32 handle and the Slim variant is all managed.

I read that slim implementation offers an advantage for short wait times, which doesn't apply here.

@danmoseley
Copy link
Member

In .NET Core and .NET 5+, the default spin-waiting duration is short: on the order of 10s of microseconds, depending on platform and processor. If you expect wait times to be much longer than that, you can still use this class instead of ManualResetEvent (perhaps configured with less or no spin-waiting). However, the performance benefit would likely be only marginal.

So it sounds like it doesn't matter that much, but indeed it should avoid creating the OS object unless you explicitly fetch WaitObject on it, as far as I can see.

@JamesNK JamesNK force-pushed the jamesnk/kestrel-heartbeat-stop branch from 4f57f79 to a502d1d Compare April 12, 2023 01:30
@JamesNK
Copy link
Member Author

JamesNK commented Apr 12, 2023

PR updated to use ManualResetEventSlim. Set spin count to zero as we're waiting a long time (1 sec) between heartbeats. Spinning would be wasted CPU.

Added a test for the heartbeat loop. Previously wasn't tested.

@JamesNK JamesNK force-pushed the jamesnk/kestrel-heartbeat-stop branch from 9d8cff5 to b4a6c37 Compare April 12, 2023 10:12
@JamesNK JamesNK changed the title Use WaitHandle in Kestrel heartbeat to quickly exit thread on stop Use reset event in Kestrel heartbeat to quickly stop thread Apr 13, 2023
@JamesNK JamesNK merged commit 063f21e into main Apr 13, 2023
@JamesNK JamesNK deleted the jamesnk/kestrel-heartbeat-stop branch April 13, 2023 03:08
@ghost ghost added this to the 8.0-preview4 milestone Apr 13, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exit Kestrel heartbeat thread immediately on stop
5 participants