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

Avoid wasting a whole thread watching for a filesystem change 😬 #18119

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

JasonElkin
Copy link
Contributor

Prerequisites

  • I have added steps to test this contribution in the description below

Description

I was profiling an Umbraco v15 solution last week and noticed that about 1% of the CPU time was spent... sleeping 😱

image

Turns out a whole thread was spending most of their time sleeping on the job! Woken up every 2 seconds to do a miniscule amount of work, then sent back to sleep.

That thread's clearly not pulling their weight - so I figured I'd send them to the pool to hang out with their friends and maybe do something else useful in the meantime. Serve some webpages, run a scheduled task or two, anything except sleeping!

Testing

Profile the site (I used DotTrace) before and after the change and notice how much CPU time is spent sleeping...

Before

Idling on my test site - 2.6% 😴

image

After

Absolutely none 😃

image

3298fr


As an aside, the approach in this method isn't ideal. There are more efficient and robust ways to handle this than a while loop, e.g. System.Threading.Timer or a FileSystemWatcher but I didn't want to embark on a refactoring without some HQ guidance and Task.Delay at least solves the immediate performance problem.

Sorry, something went wrong.

Copy link

github-actions bot commented Jan 26, 2025

Hi there @JasonElkin, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@emmagarland
Copy link
Contributor

Hi @JasonElkin

Good spot and description! One of your fellow Core Collabs will take a close up look soon.

Thanks,
Emma

@Matthew-Wise Matthew-Wise self-assigned this Jan 27, 2025
@Matthew-Wise Matthew-Wise merged commit 1395d49 into umbraco:contrib Jan 27, 2025
28 checks passed
@Matthew-Wise Matthew-Wise added community/pr release/15.3.0 category/performance Fixes for performance (generally cpu or memory) fixes labels Jan 27, 2025
@JasonElkin JasonElkin deleted the perf/wake-up-sleepy-thread branch January 27, 2025 11:55
@@ -132,7 +132,7 @@ private void ListeningLoop()
break;
}

Thread.Sleep(_globalSettings.CurrentValue.MainDomReleaseSignalPollingInterval);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is merged already, but this should really pass in the CancellationToken.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PhyxionNL Thanks for the feedback 👍

Feel free to create your own PR and we'll gladly review it 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy, we've chosen the red pill here...

Let me ask you: "What cancellation token"!?!?!

The one that comes from the private readonly CancellationTokenSource...?
The one that's not referenced from anything else... so is never actually cancelled!?!

AndyButland added a commit that referenced this pull request Jan 29, 2025
…temMainDomLock (from PRs #18119 and #18147)
AndyButland added a commit that referenced this pull request Feb 12, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…temMainDomLock (#18151)

* Backport use of thread delay over sleep and handle dispose in FileSystemMainDomLock (from PRs #18119 and #18147)

* Applied suggestion from code review.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category/performance Fixes for performance (generally cpu or memory) fixes community/pr release/13.7.0 release/15.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants