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

Add AsyncLazy<T>.SuppressRelevance() method #1254

Merged
merged 1 commit into from
Oct 31, 2023

Conversation

AArnott
Copy link
Member

@AArnott AArnott commented Oct 30, 2023

This allows value factories to identify their own code blocks that won't be awaited on and therefore shouldn't trigger reentrancy detection.

This allows value factories to identify their own code blocks that won't be awaited on and therefore shouldn't trigger reentrancy detection.
@AArnott AArnott added this to the v17.9 milestone Oct 30, 2023
Copy link
Member

@lifengl lifengl left a comment

Choose a reason for hiding this comment

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

The PR looks good and would provide a way to resolve this problem for some scenarios. It might be hard to use in other situations.

What we ran into (inside CPS), is that lazy initialization may trigger another sets of lazy initializations (for example, one set of data sources would trigger its own dependencies to be initialized). During a chains of initialization to happen, one base lazy computation might spin off a task capturing the execution context (for example, link to a dataflow with data ready). The captured evaluation context could have a whole stack of AsyncLazy context. This API is actually fairly difficult to use due to multiple layers of code.

Also, evaluation context can be captured in so many ways, adding a continuation of a task, or registering a cancellationToken could do, which makes it hard to know where to suppress them for many developers.

On the other side, this does provide a way to suppress the check completely, if the developer chooses to do that. I am not sure the check or suppressing the check too broadly is a good thing or not. But the developer has at least a chance to make the call based on the situation.

@AArnott
Copy link
Member Author

AArnott commented Oct 31, 2023

Good points, @lifengl. If you have any ideas for how to improve on this for the scenario you described, I'm interested in further improvement in this area.

@AArnott AArnott merged commit 12e1dd1 into microsoft:main Oct 31, 2023
6 checks passed
@AArnott AArnott deleted the asyncLazySuppressRelevance branch October 31, 2023 17:23
@lifengl
Copy link
Member

lifengl commented Dec 10, 2023

Good points, @lifengl. If you have any ideas for how to improve on this for the scenario you described, I'm interested in further improvement in this area.

after thinking about that, I think maybe it should provide an option to disable the check completely. Yes, when AsyncLazy has a path to depend on itself, it will hang. It could cause product to hang, but with modern debugger features, it is actually much easier to investigate the hang with a dump, than the exception from the detection logic as it today. It is actually almost impossible to understand what is going on from the exception, and also this work comes with overhead.

Also, in the history, almost all issues caught by this logic in our usages are false alarm from ExecutionContext leaks (during the loading phase, when there are many lazy initial logic, it is much easier to leak it in a way very hard to figure out, and the exception doesn't help to find the source in almost all the cases). A normal JTF.RunAsync might form a loop as well, if it eventually awaits on itself, and there is no logic to detect that -- and, in reality, it almost never happens. Maybe it I happens more often in the AsyncLazy case, but I feel maybe a hang is actually better result.

@AArnott
Copy link
Member Author

AArnott commented Dec 12, 2023

You make excellent points. I'm working on a way to disable it for the whole instance.

@AArnott
Copy link
Member Author

AArnott commented Dec 12, 2023

@lifengl see #1265

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

Successfully merging this pull request may close these issues.

4 participants