-
Notifications
You must be signed in to change notification settings - Fork 789
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
Refactor AsyncMemoize, introduce AsyncLazy #18002
Conversation
aef4303
to
00a5602
Compare
|
89cbde6
to
e1acf39
Compare
Now, that rings a bell. I'll make sure it still works. |
Not yet, but we plan to start using it when it's considered stable/recommended. |
@auduchinok, this change here should affect only TransparentCompiler, nothing else. OCEs originating from compiler tasks cached by AsyncMemoize should propagate like any other exception, without special treatment. I think this should be ok in this regard. |
But we never want to cache OCEs, do we? Or is it less relevant with the transparent compiler, because the change in files will invalidate things in a natural way? |
I'm not super familiar with this part, but IIUC AsyncMemoize is keyed by partial project snapshots. Ideally a snapshot should encode all of the deps, such as to make AsyncMemoize.Get a pure function. In practice many things can happen, so we should probably evict exceptional results pretty quickly from the cache. Again, I still don't know much about it, I don't want to spout nonsense :) |
Cancellable does not throw OCE on the async boundary, it just calls cancel continuation, very neat: fsharp/src/Compiler/Utilities/Cancellable.fs Lines 72 to 82 in fb69e58
|
c8bc3e7
to
6fbfa44
Compare
c78c4e6
to
34c8640
Compare
Nice! Can we run some benchmarks of before and after to see if anything changed? (hopefully improved)
I haven't studied this yet, but intuitively I would say OCEs might deserve special treatment. User might open a file, checking starts, they decide to close it so it gets cancelled. Then without changing anything that affects it they open it again. Same snapshot should be used and we need to restart the computation. We should probably still restart even in case of a failed job, but there theoretically we could decide not to, so we should be able to tell the difference between failed and cancelled. |
How does it look with stack traces here? Are we able to preserve them over the request? Would be nice if we could, though I didn't figure it out before so if not it's not a downgrade... |
It is possible to store exception in fsharp/src/FSharp.Core/async.fs Line 44 in 3dff60a
if you catch an exception in async CE and then rethrow it in another async, it should restore the stacktrace from ExceptionDispatchInfo cached in that ConditionalWeakTable .
I think we need a test case just for this. It would allow for some experiments. |
This is kind of what this PR already do: Failure is a failure - currently it's just cached as (Result.Error(exn), logger) value. Cancellation happens when the client cancels the request. We detach from the job and it is configurable whether is runs to completion unawaited or is itself canceled. What is still unclear to me is the situation with TBH I already built a vsix out of this and I'm using it for some time. Never saw any exceptions so I hope this is a non-issue. On the plus side, I had codefix analyzers throwing each time I switched branches in VS and with this PR this never happens, although it may be unrelated. |
Previously, the stack trace was always cut by a jump to the thread pool. You could always only see up to the point where a worker thread picked it up from there.
That sounds promising! Maybe regular operation is already covered and any other OCEs are actual errors, i.e. they shouldn't have been thrown. However, there is always some risk that some future changes will introduce an issue by throwing somewhere we don't expect it and by then no one will remember this 😂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks much better than before!
Co-authored-by: Petr Pokorny <[email protected]>
…into refactor-asyncmemo
Something to investigate. Script load closure tests don't like this version for some reason: member _.TryGet(key: 'TKey, predicate: 'TVersion -> bool) : 'TValue option =
lock cache <| fun () ->
cache.GetAll(key)
|> Seq.tryPick (fun (version, job) ->
match predicate version, job.TryResult with
| true, Some(Ok result, _) -> Some result
| _ -> None) I had to take a snapshot with toList and do tryPick outside of the lock instead. |
That's how I thought it should work, i.e. we should not cache the cancellations and recalculate it in future if needed, even on the same snapshots. I hope we get more
@majocha Is there any chance you could add tests covering this behavior if there aren't any already? |
Yes, this would be great! Currently, because of this code: fsharp/src/Compiler/Utilities/Cancellable.fs Lines 9 to 11 in a95a9ea
If there is no ambient Cancellable, |
Added back | Completed of result: 't
| Faulted of exn to the state machine. Also basic optimizations: computation and task will deallocate on completion and only the result is stored. |
@majocha - great job here (as usual), also thanks for a solid PR description and GH gist, it helps reviewing things a lot. The more tests the better here, but this can be done as a follow up - as you wish. |
I don't think that's necessary at the moment. It can be considered an implementation detail of AsyncMemoize. It would make sense if we find we need it also somewhere else. |
Yeah, fair, can be done later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
* Add -sign to build.sh (#18024) * Fix singing problems (#18040) * Skip flaky test (#18051) * Remove support for `System.AggressiveAttributeTrimming` feature switch. (#18039) Co-authored-by: Kevin Ransom (msft) <[email protected]> Co-authored-by: Petr <[email protected]> * Update some internal guidelines (#18052) * simplify temp dirs in service (#18046) Co-authored-by: Kevin Ransom (msft) <[email protected]> * Add Contributing.md (#18043) * contributing * Update CONTRIBUTING.md * Update CONTRIBUTING.md Co-authored-by: Tomas Grosup <[email protected]> * Update CONTRIBUTING.md Co-authored-by: Vlad Zarytovskii <[email protected]> * Update CONTRIBUTING.md Co-authored-by: Petr <[email protected]> * Update CONTRIBUTING.md --------- Co-authored-by: Tomas Grosup <[email protected]> Co-authored-by: Vlad Zarytovskii <[email protected]> Co-authored-by: Petr <[email protected]> * [main] Update dependencies from dotnet/arcade (#17729) * Update dependencies from https://github.com/dotnet/arcade build 20240913.2 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24462.3 -> To Version 9.0.0-beta.24463.2 * Update dependencies from https://github.com/dotnet/arcade build 20240916.2 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24463.2 -> To Version 9.0.0-beta.24466.2 * Update dependencies from https://github.com/dotnet/arcade build 20240923.1 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24466.2 -> To Version 9.0.0-beta.24473.1 * Update dependencies from https://github.com/dotnet/arcade build 20241001.3 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24473.1 -> To Version 9.0.0-beta.24501.3 * Update dependencies from https://github.com/dotnet/arcade build 20241003.2 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24501.3 -> To Version 9.0.0-beta.24503.2 * Update dependencies from https://github.com/dotnet/arcade build 20241008.3 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24503.2 -> To Version 9.0.0-beta.24508.3 * Update dependencies from https://github.com/dotnet/arcade build 20241009.3 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24508.3 -> To Version 9.0.0-beta.24509.3 * Update dependencies from https://github.com/dotnet/arcade build 20241016.2 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24509.3 -> To Version 9.0.0-beta.24516.2 * Update dependencies from https://github.com/dotnet/arcade build 20241112.13 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24462.3 -> To Version 9.0.0-beta.24562.13 * Update dependencies from https://github.com/dotnet/arcade build 20241122.2 Microsoft.SourceBuild.Intermediate.arcade , Microsoft.DotNet.Arcade.Sdk From Version 9.0.0-beta.24462.3 -> To Version 9.0.0-beta.24572.2 --------- Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com> Co-authored-by: Vlad Zarytovskii <[email protected]> * Refactor AsyncMemoize, introduce AsyncLazy (#18002) * refactor AsyncMemoize, introduce AsyncLazy * use lock free mb in tests * remove unnecessary prop * update il baselines * rename * add requestCount name Co-authored-by: Petr Pokorny <[email protected]> * improve as reviewed * lock for TryGet * more locks * ilverify * deallocate computation when completed * ilverify * style * typo --------- Co-authored-by: Petr Pokorny <[email protected]> * Fix crashdump paths in CI (#18065) * Various updates to ILVerify (#18060) * Update azure-pipelines-PR.yml * Add null annotation to Async.SwitchToContext (#18059) --------- Co-authored-by: Ella Hathaway <[email protected]> Co-authored-by: Petr <[email protected]> Co-authored-by: Petr Pokorny <[email protected]> Co-authored-by: Theodore Tsirpanis <[email protected]> Co-authored-by: Kevin Ransom (msft) <[email protected]> Co-authored-by: Jakub Majocha <[email protected]> Co-authored-by: Tomas Grosup <[email protected]> Co-authored-by: Vlad Zarytovskii <[email protected]> Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com> Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com> Co-authored-by: Petr Pokorny <[email protected]> Co-authored-by: Ch13 <[email protected]>
Maintainability is the goal here. The code is smaller now for sure but that not necessarily implies more understandable, so let's see how it goes.
The state machine of running / tracking requests / canceling jobs is now moved to a separate
AsyncLazy
type.This is not just a refactor because cancellation behavior changed:
A running job that is awaited by a client will never get canceled "from the inside" - from the requestor's pov it'd be just an exception thrown from the job, so this is not allowed.
Completion behavior is configurable now. If all clients cancel their requests,
AsyncLazy
can either cancel the job or follow to completion and store result anyway. This may or may not be useful.The LRU cache now just holds
AsyncLazy
values. State transitions happen inside theAsyncLazy
so there is no correspondingcache.Set
like before.Gist of the file for easier reading is here: https://gist.github.com/majocha/6c075dcdabf45c3fd92b03e2cf3a07eb
Todo:
Investigate passing
ExceptionDispatchInfo
from the job instead of reraising exns.