Skip to content

Commit 5af63aa

Browse files
auduchinokpsfinaki
andauthored
Cancellable: fix leaking cancellation token (#18295)
* Cancellable: fix leaking cancellation token * Release notes Update docs/release-notes/.FSharp.Compiler.Service/9.0.300.md * Set Cancellable.Token in transparent compiler * Review fixes * Move setting the token * Fantomas * Transparent compiler * il bsl --------- Co-authored-by: Petr <[email protected]>
1 parent 5d0812f commit 5af63aa

11 files changed

+20
-15
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* 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))
88
* Cancellable: only cancel on OCE with own token ([PR #18277](https://github.com/dotnet/fsharp/pull/18277))
99
* Cancellable: set token in more places ([PR #18283](https://github.com/dotnet/fsharp/pull/18283))
10+
* Cancellable: fix leaking cancellation token ([PR #18295](https://github.com/dotnet/fsharp/pull/18295))
1011
* Fix NRE when accessing nullable fields of types within their equals/hash/compare methods ([PR #18296](https://github.com/dotnet/fsharp/pull/18296))
1112

1213
### Added

src/Compiler/Driver/CompilerImports.fs

-2
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,6 @@ and [<Sealed>] TcImports
22552255
r: AssemblyResolution
22562256
) : Async<(_ * (unit -> AvailableImportedAssembly list)) option> =
22572257
async {
2258-
do! Cancellable.UseToken()
22592258
CheckDisposed()
22602259
let m = r.originalReference.Range
22612260
let fileName = r.resolvedPath
@@ -2327,7 +2326,6 @@ and [<Sealed>] TcImports
23272326
async {
23282327
CheckDisposed()
23292328

2330-
23312329
let tcConfig = tcConfigP.Get ctok
23322330

23332331
let runMethod =

src/Compiler/Service/BackgroundCompiler.fs

+6-6
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,11 @@ type internal BackgroundCompiler
500500
}
501501

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

506+
match tryGetBuilder options with
507+
| Some getBuilder ->
508508
match! getBuilder with
509509
| builderOpt, creationDiags when builderOpt.IsNone || not builderOpt.Value.IsReferencesInvalidated ->
510510
return builderOpt, creationDiags
@@ -520,8 +520,8 @@ type internal BackgroundCompiler
520520
checkFileInProjectCache.RemoveAnySimilar(ltok, key)))
521521

522522
return! createAndGetBuilder (options, userOpName)
523-
}
524-
| _ -> createAndGetBuilder (options, userOpName)
523+
| _ -> return! createAndGetBuilder (options, userOpName)
524+
}
525525

526526
let getSimilarOrCreateBuilder (options, userOpName) =
527527
match tryGetSimilarBuilder options with

src/Compiler/Service/ServiceAnalysis.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ module UnusedOpens =
302302
/// Async to allow cancellation.
303303
let getUnusedOpens (checkFileResults: FSharpCheckFileResults, getSourceLineStr: int -> string) : Async<range list> =
304304
async {
305-
do! Cancellable.UseToken()
305+
use! _holder = Cancellable.UseToken()
306306

307307
if checkFileResults.OpenDeclarations.Length = 0 then
308308
return []

src/Compiler/Service/TransparentCompiler.fs

+6
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,8 @@ type internal TransparentCompiler
16101610
caches.ParseAndCheckFileInProject.Get(
16111611
projectSnapshot.FileKeyWithExtraFileSnapshotVersion fileName,
16121612
async {
1613+
use! _holder = Cancellable.UseToken()
1614+
16131615
use _ =
16141616
Activity.start "ComputeParseAndCheckFileInProject" [| Activity.Tags.fileName, fileName |> Path.GetFileName |> (!!) |]
16151617

@@ -1861,6 +1863,7 @@ type internal TransparentCompiler
18611863
caches.AssemblyData.Get(
18621864
projectSnapshot.SignatureKey,
18631865
async {
1866+
use! _holder = Cancellable.UseToken()
18641867

18651868
try
18661869

@@ -1908,6 +1911,7 @@ type internal TransparentCompiler
19081911
caches.ParseAndCheckProject.Get(
19091912
projectSnapshot.FullKey,
19101913
async {
1914+
use! _holder = Cancellable.UseToken()
19111915

19121916
match! ComputeBootstrapInfo projectSnapshot with
19131917
| None, creationDiags ->
@@ -1980,6 +1984,8 @@ type internal TransparentCompiler
19801984

19811985
let tryGetSink (fileName: string) (projectSnapshot: ProjectSnapshot) =
19821986
async {
1987+
use! _holder = Cancellable.UseToken()
1988+
19831989
match! ComputeBootstrapInfo projectSnapshot with
19841990
| None, _ -> return None
19851991
| Some bootstrapInfo, _creationDiags ->

src/Compiler/Utilities/Cancellable.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Cancellable =
2121
static member UseToken() =
2222
async {
2323
let! ct = Async.CancellationToken
24-
tokenHolder.Value <- ValueSome ct
24+
return Cancellable.UsingToken ct
2525
}
2626

2727
static member UsingToken(ct) =

src/Compiler/Utilities/Cancellable.fsi

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open System.Threading
55

66
[<Sealed>]
77
type Cancellable =
8-
static member internal UseToken: unit -> Async<unit>
8+
static member internal UseToken: unit -> Async<IDisposable>
99

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

tests/ILVerify/ilverify_FSharp.Compiler.Service_Debug_net9.0.bsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x00000082][found Char] Unexpected type on the stack.
2222
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x0000008B][found Char] Unexpected type on the stack.
2323
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+MagicAssemblyResolution::ResolveAssemblyCore([FSharp.Compiler.Service]Internal.Utilities.Library.CompilationThreadToken, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, [FSharp.Compiler.Service]FSharp.Compiler.CompilerConfig+TcConfigBuilder, [FSharp.Compiler.Service]FSharp.Compiler.CompilerImports+TcImports, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompiler, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiConsoleOutput, string)][offset 0x00000015][found Char] Unexpected type on the stack.
24-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-789::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001E5][found Char] Unexpected type on the stack.
24+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-802::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001E5][found Char] Unexpected type on the stack.
2525
[IL]: Error [UnmanagedPointer]: : FSharp.Compiler.Interactive.Shell+Utilities+pointerToNativeInt@110::Invoke(object)][offset 0x00000007] Unmanaged pointers are not a verifiable type.
2626
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults+dataTipOfReferences@2205::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000084][found Char] Unexpected type on the stack.
2727
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@921-509::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000032][found Char] Unexpected type on the stack.

tests/ILVerify/ilverify_FSharp.Compiler.Service_Debug_netstandard2.0.bsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x0000008B][found Char] Unexpected type on the stack.
2929
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+FsiStdinSyphon::GetLine(string, int32)][offset 0x00000039][found Char] Unexpected type on the stack.
3030
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+MagicAssemblyResolution::ResolveAssemblyCore([FSharp.Compiler.Service]Internal.Utilities.Library.CompilationThreadToken, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, [FSharp.Compiler.Service]FSharp.Compiler.CompilerConfig+TcConfigBuilder, [FSharp.Compiler.Service]FSharp.Compiler.CompilerImports+TcImports, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompiler, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiConsoleOutput, string)][offset 0x00000015][found Char] Unexpected type on the stack.
31-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-789::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001E5][found Char] Unexpected type on the stack.
31+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-802::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001E5][found Char] Unexpected type on the stack.
3232
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+FsiInteractionProcessor::CompletionsForPartialLID([FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompilerState, string)][offset 0x0000001B][found Char] Unexpected type on the stack.
3333
[IL]: Error [UnmanagedPointer]: : FSharp.Compiler.Interactive.Shell+Utilities+pointerToNativeInt@110::Invoke(object)][offset 0x00000007] Unmanaged pointers are not a verifiable type.
3434
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults+dataTipOfReferences@2205::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000084][found Char] Unexpected type on the stack.

tests/ILVerify/ilverify_FSharp.Compiler.Service_Release_net9.0.bsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x00000082][found Char] Unexpected type on the stack.
2222
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x0000008B][found Char] Unexpected type on the stack.
2323
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+MagicAssemblyResolution::ResolveAssemblyCore([FSharp.Compiler.Service]Internal.Utilities.Library.CompilationThreadToken, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, [FSharp.Compiler.Service]FSharp.Compiler.CompilerConfig+TcConfigBuilder, [FSharp.Compiler.Service]FSharp.Compiler.CompilerImports+TcImports, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompiler, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiConsoleOutput, string)][offset 0x00000015][found Char] Unexpected type on the stack.
24-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-833::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001C7][found Char] Unexpected type on the stack.
24+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-846::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001C7][found Char] Unexpected type on the stack.
2525
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults+GetReferenceResolutionStructuredToolTipText@2205::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000076][found Char] Unexpected type on the stack.
2626
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@921-530::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000032][found Char] Unexpected type on the stack.
2727
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@921-530::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x0000003B][found Char] Unexpected type on the stack.

tests/ILVerify/ilverify_FSharp.Compiler.Service_Release_netstandard2.0.bsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CodeAnalysis.Hosted.CompilerHelpers::fscCompile([FSharp.Compiler.Service]FSharp.Compiler.CodeAnalysis.LegacyReferenceResolver, string, string[])][offset 0x0000008B][found Char] Unexpected type on the stack.
2929
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+FsiStdinSyphon::GetLine(string, int32)][offset 0x00000032][found Char] Unexpected type on the stack.
3030
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+MagicAssemblyResolution::ResolveAssemblyCore([FSharp.Compiler.Service]Internal.Utilities.Library.CompilationThreadToken, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, [FSharp.Compiler.Service]FSharp.Compiler.CompilerConfig+TcConfigBuilder, [FSharp.Compiler.Service]FSharp.Compiler.CompilerImports+TcImports, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompiler, [FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiConsoleOutput, string)][offset 0x00000015][found Char] Unexpected type on the stack.
31-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-833::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001C7][found Char] Unexpected type on the stack.
31+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+clo@3516-846::Invoke([S.P.CoreLib]System.Tuple`3<char[],int32,int32>)][offset 0x000001C7][found Char] Unexpected type on the stack.
3232
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Interactive.Shell+FsiInteractionProcessor::CompletionsForPartialLID([FSharp.Compiler.Service]FSharp.Compiler.Interactive.Shell+FsiDynamicCompilerState, string)][offset 0x00000024][found Char] Unexpected type on the stack.
3333
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults+GetReferenceResolutionStructuredToolTipText@2205::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000076][found Char] Unexpected type on the stack.
3434
[IL]: Error [StackUnexpected]: : FSharp.Compiler.EditorServices.AssemblyContent+traverseMemberFunctionAndValues@176::Invoke([FSharp.Compiler.Service]FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue)][offset 0x0000002B][found Char] Unexpected type on the stack.

0 commit comments

Comments
 (0)