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

Second half of fix for https://github.com/Microsoft/visualfsharp/issues/106 #591

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion src/fsharp/CompileOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3846,11 +3846,20 @@ type TcImports(tcConfigP:TcConfigProvider, initialResolutions:TcAssemblyResoluti
referencedAssemblies = [| for r in resolutions.GetAssemblyResolutions() -> r.resolvedPath |]
temporaryFolder = Path.GetTempPath() }

// The type provider should not hold strong references to disposed
// TcImport objects. So the callbacks provided in the type provider config
// dispatch via a thunk which gets set to a non-resource-capturing
// failing function when the object is disposed.
let systemRuntimeContainsType =
let systemRuntimeContainsTypeRef = ref tcImports.SystemRuntimeContainsType
tcImports.AttachDisposeAction(fun () -> systemRuntimeContainsTypeRef := (fun _ -> raise (System.ObjectDisposedException("The type provider has been disposed"))))
fun arg -> systemRuntimeContainsTypeRef.Value arg

let providers =
[ for assemblyName in providerAssemblies do
yield ExtensionTyping.GetTypeProvidersOfAssembly(displayPSTypeProviderSecurityDialogBlockingUI, tcConfig.validateTypeProviders, tpApprovals,
fileNameOfRuntimeAssembly, ilScopeRefOfRuntimeAssembly, assemblyName, typeProviderEnvironment,
tcConfig.isInvalidationSupported, tcConfig.isInteractive, tcImports.SystemRuntimeContainsType, systemRuntimeAssemblyVersion, m) ]
tcConfig.isInvalidationSupported, tcConfig.isInteractive, systemRuntimeContainsType, systemRuntimeAssemblyVersion, m) ]
let wasApproved = providers |> List.forall (fun (ok,_) -> ok)
let providers = providers |> List.map snd |> List.concat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,25 @@ module internal TPModule =
module GlobalCounters =
let mutable creations = 0
let mutable disposals = 0
let mutable configs = ([]: TypeProviderConfig list)
let GetTotalCreations() = creations
let GetTotalDisposals() = disposals
let CheckAllConfigsDisposed() =
for c in configs do
try
c.SystemRuntimeContainsType("System.Object") |> ignore
failwith "expected configuration object to be disposed"
with :? System.ObjectDisposedException ->
()



[<TypeProvider>]
type HelloWorldProvider() =
type HelloWorldProvider(config: TypeProviderConfig) =
inherit TypeProviderForNamespaces(TPModule.namespaceName,TPModule.types)
do GlobalCounters.creations <- GlobalCounters.creations + 1
let mutable disposed = false
do GlobalCounters.configs <- config :: GlobalCounters.configs
interface System.IDisposable with
member x.Dispose() =
System.Diagnostics.Debug.Assert(not disposed)
Expand Down
4 changes: 4 additions & 0 deletions vsintegration/src/unittests/Tests.LanguageService.Script.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,8 @@ type ScriptTests() as this =
Assert.IsNotNull(totalCreationsMeth, "totalCreationsMeth should not be null")
let totalDisposalsMeth = providerCounters.GetMethod("GetTotalDisposals")
Assert.IsNotNull(totalDisposalsMeth, "totalDisposalsMeth should not be null")
let checkConfigsMeth = providerCounters.GetMethod("CheckAllConfigsDisposed")
Assert.IsNotNull(checkConfigsMeth, "checkConfigsMeth should not be null")

let providerCounters2 = providerAssembly.GetType("Microsoft.FSharp.TypeProvider.Emit.GlobalCountersForInvalidation")
Assert.IsNotNull(providerCounters2, "provider counters #2 module should not be null")
Expand All @@ -1638,6 +1640,7 @@ type ScriptTests() as this =

let totalCreations() = totalCreationsMeth.Invoke(null, [| |]) :?> int
let totalDisposals() = totalDisposalsMeth.Invoke(null, [| |]) :?> int
let checkConfigsDisposed() = checkConfigsMeth.Invoke(null, [| |]) |> ignore
let totalInvaldiationHandlersAdded() = totalInvaldiationHandlersAddedMeth.Invoke(null, [| |]) :?> int
let totalInvaldiationHandlersRemoved() = totalInvaldiationHandlersRemovedMeth.Invoke(null, [| |]) :?> int

Expand Down Expand Up @@ -1693,6 +1696,7 @@ type ScriptTests() as this =
ClearLanguageServiceRootCachesAndCollectAndFinalizeAllTransients(this.VS)
Assert.IsTrue(countDisposals() = 50, "Check6b, at end, countDisposals() = 50 after explicit clearing")
Assert.IsTrue(countInvaldiationHandlersAdded() - countInvaldiationHandlersRemoved() = 0, "Check6b2, at end, all invalidation handlers removed after explicit cleraring")
checkConfigsDisposed()

[<Test>]
[<Category("TypeProvider")>]
Expand Down