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

Fix eagerly executed task in CustomSettingService #15942

Merged
merged 1 commit into from
May 2, 2024
Merged

Conversation

sebastienros
Copy link
Member

@sebastienros sebastienros commented May 2, 2024

Fix #15794
Fix #15628

How I found it:

  • I have a local patched version of YesSql that can detect when the same session has been used by two threads by counting when it enters/leaves Save/Flush
  • I trigger the debugger when this happens (System.Diagnostics.Debugger.Break) which open VS at that point in debug mode.
  • I opened the "Parallel Tasks" window which shows this:

image

On the right the DocumentManager saved a document (Roles) and this is rooted by an HttpRequest. On the left there is no specific root, which seems weird. So I looked at this CustomSettingService.GetContentTypeAsync. Weirdly, it's invoked in a Lazy<> in the constructor ...

_settingsTypes = new Lazy<Task<IDictionary<string, ContentTypeDefinition>>>(GetContentTypeAsync());

and this is it, the lazy is initialized with the running Task because the method is invoked directly: GetContentTypeAsync()

It used to work, before this commit as the code was

_settingsTypes = new Lazy<Task<IDictionary<string, ContentTypeDefinition>>>(async () => await GetContentTypeAsync());

The important thing is not async/await it's the fact that it's a lambda, so Lazy<> will invoke it only when .Value is called. After the recent change it was actually invoking the Lazy<T>(T value) overload which is not lazy at all.

The solution is to have a lambda or a method group, which this PR is doing.

Summary by CodeRabbit

  • Refactor
    • Improved the initialization process in settings management to enhance performance.

Copy link
Contributor

coderabbitai bot commented May 2, 2024

Walkthrough

Walkthrough

The update in the CustomSettingsService class involves refining the initialization of the _settingsTypes variable. Previously, the GetContentTypeAsync method was incorrectly invoked during the instantiation of a Lazy object. The correction ensures that the method is passed as a delegate instead, optimizing the lazy loading mechanism by deferring the method execution until it's actually needed.

Changes

File Path Change Summary
.../OrchardCore.CustomSettings/Services/CustomSettingsService.cs Modified the initialization of _settingsTypes to pass GetContentTypeAsync as a delegate, rather than invoking it directly.

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between c967b5d and 65e2176.
Files selected for processing (1)
  • src/OrchardCore.Modules/OrchardCore.CustomSettings/Services/CustomSettingsService.cs (1 hunks)
Additional comments not posted (2)
src/OrchardCore.Modules/OrchardCore.CustomSettings/Services/CustomSettingsService.cs (2)

36-36: Initialization of _settingsTypes now correctly uses a method group to ensure lazy loading.


36-36: Ensure that all accesses to _settingsTypes use .Value to maintain lazy loading behavior.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sebastienros
Copy link
Member Author

sebastienros commented May 2, 2024

Improved the initialization process in settings management to enhance performance.

Bad rabbit! Not for performance at all.

@MikeAlhayek
Copy link
Member

@sebastienros indeed this fixed the issue! Great find!

@MikeAlhayek MikeAlhayek merged commit 097a053 into main May 2, 2024
7 checks passed
@MikeAlhayek MikeAlhayek deleted the sebros/eagertask branch May 2, 2024 15:08
@Piedone
Copy link
Member

Piedone commented May 2, 2024

Good job! Shouldn't YesSql have a config that throws an exception on parallel queries? So, what you have locally, but don't break but throw an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants