Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* `GetProjectSnapshotFromScript` uses the document source of the checker when none is passed, also for the TransparentCompiler's `GetProjectOptionsFromScript`. A file from a `DocumentSource.Custom` keeps its snapshot version until its text changes, and a file for which the source returns `None` is read from disk. `FSharpFileSnapshot.CreateFromDocumentSource` now returns `Async<FSharpFileSnapshot>`. `SourceTextNew.ofISourceText` returns an `ISourceTextNew` unchanged, and otherwise computes the checksum from the text instead of from `ToString()`. ([Issue #20750](https://github.com/dotnet/fsharp/issues/20750), [PR #20751](https://github.com/dotnet/fsharp/pull/20751))
* Build parser diagnostic messages without runtime printf specialization or reflective function construction, so Native AOT hosts report indentation and invalid-character diagnostics instead of failing during formatting. Preserve `#r` reference tooltips while script project options remain alive, including after garbage collection or cache eviction. ([PR #20658](https://github.com/dotnet/fsharp/pull/20658))
* Preserve tupled lambda argument groups when reducing state-machine applications, avoiding invalid argument substitutions and runtime failures with inline formatters. ([Issue #20675](https://github.com/dotnet/fsharp/issues/20675), [PR #20700](https://github.com/dotnet/fsharp/pull/20700))
* Transparent compiler: a cached computation that was canceled and restarted no longer takes the result of the canceled run while the new run is still going. ([PR #20644](https://github.com/dotnet/fsharp/pull/20644))
* Transparent compiler: `ParseFile` no longer imports the references of the project, which also type checked the projects it references. Parsing only needs the compiler options of the project. ([PR #20704](https://github.com/dotnet/fsharp/pull/20704))
* Preserve SRTP extension scopes across source files and competing witnesses through imported inline calls. Compile generic extension forwarding, including quotations, in unoptimized builds. Preserve applicable built-in operator priority in imported inline quotations. Do not reuse SRTP specializations across different selected witnesses. ([Issue #20683](https://github.com/dotnet/fsharp/issues/20683), [Issue #20684](https://github.com/dotnet/fsharp/issues/20684), [Issue #20685](https://github.com/dotnet/fsharp/issues/20685), [PR #20717](https://github.com/dotnet/fsharp/pull/20717))
* Transparent compiler: with `enablePartialTypeChecking`, a project that is only referenced by another project is type checked from its signatures. Implementation files that have a signature file are no longer parsed or type checked to produce the referenced assembly. `ParseAndCheckProject` still checks all files. ([PR #20703](https://github.com/dotnet/fsharp/pull/20703))
Expand Down
7 changes: 4 additions & 3 deletions src/Compiler/Facilities/AsyncMemoize.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ type AsyncLazy<'t> private (initial: AsyncLazyState<'t>, cancelUnawaited: bool,
return! work |> Async.AwaitTask
}

let onComplete (t: Task<'t>) =
// Only complete the run that produced this result. The run may have been canceled and replaced by a new one.
let onComplete (runCts: CancellationTokenSource) (t: Task<'t>) =
updateState
<| function
| Running(computation, _, _, _) ->
| Running(computation, _, cts, _) when obj.ReferenceEquals(cts, runCts) ->
try
Completed t.Result
with exn ->
Expand All @@ -80,7 +81,7 @@ type AsyncLazy<'t> private (initial: AsyncLazyState<'t>, cancelUnawaited: bool,
let work =
Async
.StartAsTask(computation, cancellationToken = cts.Token)
.ContinueWith(onComplete, TaskContinuationOptions.NotOnCanceled)
.ContinueWith(onComplete cts, TaskContinuationOptions.NotOnCanceled)

Running(computation, work, cts, 1), detachable work
| Running(c, work, cts, count) -> Running(c, work, cts, count + 1), detachable work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,76 @@ let ``Job is actually cancelled and restarted`` () : Task =
Assert.Equal(1, finishedCount)
}

/// Queues tasks until RunPending is called.
type ManualTaskScheduler() =
inherit TaskScheduler()

let queue = System.Collections.Concurrent.ConcurrentQueue<Task>()

member _.QueuedCount = queue.Count

member _.RunPending() =
let mutable task = null
while queue.TryDequeue &task do
base.TryExecuteTask task |> ignore

override _.QueueTask task = queue.Enqueue task
override _.TryExecuteTaskInline(task, _) = base.TryExecuteTask task
override _.GetScheduledTasks() = queue.ToArray()

[<Fact>]
let ``Completion of a canceled run does not complete the run that replaced it`` () : Task =
task {
let mutable runs = 0
let runCanComplete = [| TaskCompletionSource<unit>(); TaskCompletionSource<unit>() |]

let computation = async {
let run = Interlocked.Increment &runs
do! Async.AwaitTask runCanComplete[run - 1].Task
return run
}

let memoize = AsyncMemoize<_, int, _>()
let events = observe memoize
let key = 1

// Request the job from a task on our own scheduler. The handler that records the result of the
// first run is queued on it, so it only runs when we let it.
let scheduler = ManualTaskScheduler()
use cts1 = new CancellationTokenSource()

let startRequest1 =
Task.Factory.StartNew(
(fun () -> Async.StartImmediateAsTask(memoize.Get(wrapKey key, computation), cts1.Token)),
CancellationToken.None,
TaskCreationOptions.None,
scheduler)

scheduler.RunPending()
let request1 = startRequest1.Result
do! waitUntil events (received Started)

// The first run finishes, but the handler that records its result stays queued.
runCanComplete[0].SetResult()
Assert.True(SpinWait.SpinUntil((fun () -> scheduler.QueuedCount = 1), TimeSpan.FromSeconds 10.))

// The only request is canceled, so the first run is abandoned.
cts1.Cancel()
do! assertTaskCanceled request1

// A new request starts a second run.
let request2 = Async.StartAsTask(memoize.Get(wrapKey key, computation))
do! waitUntil events (countOf Started >> (=) 2)

// Now the handler of the abandoned first run gets to run. The second run is still going.
scheduler.RunPending()
Assert.Equal(None, memoize.TryGet(key, fun _ -> true))

runCanComplete[1].SetResult()
let! result2 = request2
Assert.Equal(2, result2)
}

[<Fact>]
let ``Job keeps running if only one requestor cancels`` () : Task =
task {
Expand Down
Loading