Skip to content

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Jul 13, 2025

  • This is to prevent generated parameters from being accessed concurrently and generating different values.

Found in a new flaky test after the async parameter PR:

image

Throwing agent mode at test logs is 👨🏾‍🍳

PS: This was a problem before but is more prevalent now because we introduced asynchrony in parameter resolution.

…eval

- This is to prevent generated parameters from being accessed concurrently and generating different values. This is more of a concern now because of asynchrony.
@Copilot Copilot AI review requested due to automatic review settings July 13, 2025 03:40
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the ParameterResource class to use lazy initialization for parameter value retrieval to prevent race conditions when generated parameters are accessed concurrently. The change addresses a flaky test issue that became more prevalent after introducing asynchrony in parameter resolution.

  • Replaces manual caching logic with Lazy<string> for thread-safe value initialization
  • Removes the custom _hasValue flag and _value field in favor of the built-in lazy initialization pattern
  • Simplifies the ValueInternal property implementation

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jul 13, 2025
@davidfowl davidfowl requested a review from mitchdenny July 13, 2025 03:41
@davidfowl davidfowl added this to the 9.4 milestone Jul 13, 2025
ArgumentNullException.ThrowIfNull(callback);

_valueGetter = callback;
_lazyValue = new Lazy<string>(() => _valueGetter(Default));
Copy link
Member

Choose a reason for hiding this comment

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

What is the default safety mode when not specified explicitly? Is it thread safe?

I think it would be clearer to specify it explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

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

ExecutionAndPublication, the one that you always want by default 😄

@JamesNK JamesNK merged commit d04a2d4 into main Jul 14, 2025
277 checks passed
@JamesNK JamesNK deleted the davidfowl/concurrent-value-access branch July 14, 2025 04:43
davidfowl added a commit that referenced this pull request Jul 15, 2025
* Force async execution of resource startup - Previously we didn't preemptively dispatch to the threadpool per resource on startup. That causes issues blocking other resource's startup because of a single blocking call. This change dispatching preemptively before creating the dcp resource.

* Refactor ParameterResource to use Lazy initialization for value retrieval (#10361)

---------

Co-authored-by: David Fowler <davidfowl@gmail.com>
@github-actions github-actions bot locked and limited conversation to collaborators Aug 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants