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

Cancellable: fix leaking cancellation token #18295

Merged
merged 8 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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/9.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix missing nullness warning when static upcast dropped nullness ([Issue #18232](https://github.com/dotnet/fsharp/issues/18232), [PR #18261](https://github.com/dotnet/fsharp/pull/18261))
* Cancellable: only cancel on OCE with own token ([PR #18277](https://github.com/dotnet/fsharp/pull/18277))
* Cancellable: set token in more places ([PR #18283](https://github.com/dotnet/fsharp/pull/18283))
* Cancellable: fix leaking cancellation token ([PR #18295](https://github.com/dotnet/fsharp/pull/18295))
* Fix NRE when accessing nullable fields of types within their equals/hash/compare methods ([PR #18296](https://github.com/dotnet/fsharp/pull/18296))

### Added
Expand Down
2 changes: 0 additions & 2 deletions src/Compiler/Driver/CompilerImports.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,6 @@ and [<Sealed>] TcImports
r: AssemblyResolution
) : Async<(_ * (unit -> AvailableImportedAssembly list)) option> =
async {
do! Cancellable.UseToken()
CheckDisposed()
let m = r.originalReference.Range
let fileName = r.resolvedPath
Expand Down Expand Up @@ -2327,7 +2326,6 @@ and [<Sealed>] TcImports
async {
CheckDisposed()


let tcConfig = tcConfigP.Get ctok

let runMethod =
Expand Down
12 changes: 6 additions & 6 deletions src/Compiler/Service/BackgroundCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ type internal BackgroundCompiler
}

let getOrCreateBuilder (options, userOpName) : Async<IncrementalBuilder option * FSharpDiagnostic[]> =
match tryGetBuilder options with
| Some getBuilder ->
async {
do! Cancellable.UseToken()
async {
use! _holder = Cancellable.UseToken()

match tryGetBuilder options with
| Some getBuilder ->
match! getBuilder with
| builderOpt, creationDiags when builderOpt.IsNone || not builderOpt.Value.IsReferencesInvalidated ->
return builderOpt, creationDiags
Expand All @@ -520,8 +520,8 @@ type internal BackgroundCompiler
checkFileInProjectCache.RemoveAnySimilar(ltok, key)))

return! createAndGetBuilder (options, userOpName)
}
| _ -> createAndGetBuilder (options, userOpName)
| _ -> return! createAndGetBuilder (options, userOpName)
}

let getSimilarOrCreateBuilder (options, userOpName) =
match tryGetSimilarBuilder options with
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Service/ServiceAnalysis.fs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ module UnusedOpens =
/// Async to allow cancellation.
let getUnusedOpens (checkFileResults: FSharpCheckFileResults, getSourceLineStr: int -> string) : Async<range list> =
async {
do! Cancellable.UseToken()
use! _holder = Cancellable.UseToken()

if checkFileResults.OpenDeclarations.Length = 0 then
return []
Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/Service/TransparentCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,8 @@ type internal TransparentCompiler
caches.ParseAndCheckFileInProject.Get(
projectSnapshot.FileKeyWithExtraFileSnapshotVersion fileName,
async {
use! _holder = Cancellable.UseToken()

use _ =
Activity.start "ComputeParseAndCheckFileInProject" [| Activity.Tags.fileName, fileName |> Path.GetFileName |> (!!) |]

Expand Down Expand Up @@ -1861,6 +1863,7 @@ type internal TransparentCompiler
caches.AssemblyData.Get(
projectSnapshot.SignatureKey,
async {
use! _holder = Cancellable.UseToken()

try

Expand Down Expand Up @@ -1908,6 +1911,7 @@ type internal TransparentCompiler
caches.ParseAndCheckProject.Get(
projectSnapshot.FullKey,
async {
use! _holder = Cancellable.UseToken()

match! ComputeBootstrapInfo projectSnapshot with
| None, creationDiags ->
Expand Down Expand Up @@ -1980,6 +1984,8 @@ type internal TransparentCompiler

let tryGetSink (fileName: string) (projectSnapshot: ProjectSnapshot) =
async {
use! _holder = Cancellable.UseToken()

match! ComputeBootstrapInfo projectSnapshot with
| None, _ -> return None
| Some bootstrapInfo, _creationDiags ->
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Utilities/Cancellable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Cancellable =
static member UseToken() =
async {
let! ct = Async.CancellationToken
tokenHolder.Value <- ValueSome ct
return Cancellable.UsingToken ct
}

static member UsingToken(ct) =
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Utilities/Cancellable.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open System.Threading

[<Sealed>]
type Cancellable =
static member internal UseToken: unit -> Async<unit>
static member internal UseToken: unit -> Async<IDisposable>

/// For use in testing only. Cancellable.token should be set only by the cancellable computation.
static member internal UsingToken: CancellationToken -> IDisposable
Expand Down
Loading