Skip to content

Commit df17863

Browse files
fix for #240, based on investigation from @mrinaldi in #241 and suggestion from @dsevastianov to clear the cache instead of disposing it.
1 parent 16c2ce6 commit df17863

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/SqlClient/SingleRootTypeProvider.fs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ type SingleRootTypeProvider(config: TypeProviderConfig, providerName, parameters
1414
//cache
1515
let changeMonitors = ConcurrentBag()
1616
[<VolatileField>]
17-
let mutable disposingCache = false
17+
let mutable clearingCache = false
1818
let cache = new MemoryCache(providerName)
1919

20-
let disposeCache() =
21-
disposingCache <- true
20+
let clearCache() =
21+
clearingCache <- true
2222
while not changeMonitors.IsEmpty do
2323
let removed, (monitor: CacheEntryChangeMonitor) = changeMonitors.TryTake()
2424
if removed then monitor.Dispose()
25-
cache.Dispose()
25+
26+
// http://stackoverflow.com/a/29916907
27+
cache
28+
|> Seq.map (fun e -> e.Key)
29+
|> Seq.toArray
30+
|> Array.iter (cache.Remove >> ignore)
2631

2732
let cacheGetOrAdd(key, value: Lazy<ProvidedTypeDefinition>, monitors) =
2833
let policy = CacheItemPolicy()
@@ -33,9 +38,9 @@ type SingleRootTypeProvider(config: TypeProviderConfig, providerName, parameters
3338
then
3439
let m = cache.CreateCacheEntryChangeMonitor [ key ]
3540
m.NotifyOnChanged(fun _ ->
36-
if not disposingCache
41+
if not clearingCache
3742
then
38-
disposeCache()
43+
clearCache()
3944
this.Invalidate()
4045
)
4146
changeMonitors.Add(m)
@@ -62,6 +67,6 @@ type SingleRootTypeProvider(config: TypeProviderConfig, providerName, parameters
6267
this.AddNamespace( nameSpace, [ providerType ])
6368

6469
do
65-
this.Disposing.Add <| fun _ -> disposeCache()
70+
this.Disposing.Add <| fun _ -> clearCache()
6671

6772
abstract CreateRootType: assemblyName: Assembly * nameSpace: string * typeName: string * args: obj[] -> Lazy<ProvidedTypeDefinition> * obj[] // ChangeMonitor[] underneath but there is a problem https://github.com/fsprojects/FSharp.Data.SqlClient/issues/234#issuecomment-240694390

0 commit comments

Comments
 (0)