Skip to content

Commit 4665133

Browse files
Thoriumclaude
andcommitted
Design-time cache: serve one stable generation per static parameters
Root-caused the intermittent FS0193/FS0001 "type X is not compatible with type X" CI failures: the F# compiler consumes target-model provided types that the TypeProvider SDK memoizes keyed on the identity of our source types (fixed separately in the SDK, see fsprojects/FSharp.TypeProviders.SDK#521). That makes source-root identity per static-parameter set a correctness contract for the design-time cache, not just a caching policy. Two mechanisms in the cache could violate it: - The stale-while-revalidate background refresh rebuilt the root and swapped it under a live key, so a compilation running longer than the staleness window could observe two generations of the same erased types. It also opened one design-time connection per refresh that was only disposed at TP unload. Removed; schema changes are picked up by the idle-eviction rebuild or explicitly via ClearDatabaseSchemaCache. - Idle eviction at 3 minutes could evict between two requests of one long compilation, with the same two-generation effect. Raised to 30 minutes; memory/connection cleanup and eventual schema pickup are preserved. Kept unchanged: GetOrAdd + Lazy (one build per key, concurrent requests share it), Interlocked LastAccess, the idle watcher, and the identity-checked faulted-entry eviction (transient connection failures retry instead of serving the Lazy's cached exception). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 551c7b3 commit 4665133

1 file changed

Lines changed: 26 additions & 49 deletions

File tree

‎src/SQLProvider.DesignTime/SqlDesignTime.fs‎

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,20 +1288,23 @@ type SqlRuntimeInfo (config : TypeProviderConfig) =
12881288
//| Choice2Of2(paths, errors) -> Assembly.GetExecutingAssembly()
12891289
member __.RuntimeAssembly = runtimeAssembly
12901290

1291-
/// One generation of provided types for one set of static parameters.
1292-
/// Reference equality so that ConcurrentDictionary.TryUpdate swaps compare generations,
1293-
/// not their (concurrently mutated) field values.
1291+
/// One generation of provided types for one set of static parameters. Once built, a generation is
1292+
/// served unchanged for the lifetime of the entry. This is a correctness requirement, not a cache
1293+
/// policy: the compiler-facing (target-model) provided types are memoized by the TypeProvider SDK
1294+
/// keyed on the identity of these source types, so producing a second source root for the same
1295+
/// static parameters while any compilation may still re-request it makes the compiler see two
1296+
/// unrelated erased types (intermittent FS0193/FS0001 "type X is not compatible with type X").
1297+
/// The entry is therefore only ever dropped whole (idle expiration or explicit cache clear),
1298+
/// never rebuilt and swapped in place underneath a live compilation.
1299+
/// Reference equality so the identity-checked eviction matches the exact entry instance, not its
1300+
/// (concurrently mutated) field values.
12941301
[<ReferenceEquality>]
12951302
type DesignCacheEntry =
1296-
{ /// The provided root type, when it was built, and how old the build may get before a
1297-
/// background refresh is started (adapted to build duration on slow systems).
1298-
Root : Lazy<ProvidedTypeDefinition * DateTime * TimeSpan>
1303+
{ /// The provided root type, built lazily once per set of static parameters.
1304+
Root : Lazy<ProvidedTypeDefinition>
12991305
/// Last access in UTC ticks. Updated lock-free on every instantiation request;
13001306
/// entries idle longer than the expiration are dropped to free memory.
1301-
mutable LastAccess : int64
1302-
/// 1 while a background refresh build is in flight: at most one refresh per entry,
1303-
/// no matter how many Visual Studio threads request the type concurrently.
1304-
mutable Refreshing : int }
1307+
mutable LastAccess : int64 }
13051308

13061309
module DesignTimeCache =
13071310
let cache = System.Collections.Concurrent.ConcurrentDictionary<DesignCacheKey,DesignCacheEntry>()
@@ -1523,28 +1526,25 @@ type public SqlTypeProvider(config: TypeProviderConfig) as this =
15231526
args.[12] :?> string, // SSDT Path
15241527
typeName)
15251528

1526-
// Entries idle longer than this are dropped to free memory. An actively used entry is
1527-
// never dropped on a timer; instead it is refreshed in the background once its build is
1528-
// older than its staleness interval, so database schema changes are still picked up
1529-
// without IntelliSense ever stalling on a synchronous rebuild.
1530-
let idleExpiration = TimeSpan.FromMinutes 3.0
1529+
// Entries idle longer than this are dropped to free memory (and the design-time connection),
1530+
// so a later access rebuilds from scratch and picks up any database schema changes. The
1531+
// window is deliberately long: a single large compilation can have multi-minute gaps
1532+
// between two requests for the same instantiation, and evicting inside such a gap would
1533+
// hand the compiler two different generations of the same erased types (FS0193, see
1534+
// DesignCacheEntry). An actively used entry is served as-is and never rebuilt underneath
1535+
// a live compilation; explicit refresh is available via ClearDatabaseSchemaCache.
1536+
let idleExpiration = TimeSpan.FromMinutes 30.0
15311537

15321538
let buildRoot (args:DesignCacheKey) =
15331539
let struct(_,_,_,_,_,_,_,_,_,_,_,_,_, rootTypeName) = args
1534-
let buildStarted = DateTime.UtcNow
15351540

15361541
let rootType = ProvidedTypeDefinition(sqlRuntimeInfo.RuntimeAssembly,FSHARP_DATA_SQL,rootTypeName,Some typeof<obj>, isErased=true)
15371542
let serviceType = ProvidedTypeDefinition( "dataContext", Some typeof<obj>, isErased=true)
15381543
let readServiceType = ProvidedTypeDefinition( "readDataContext", Some typeof<obj>, isErased=true)
15391544

15401545
createTypes rootType serviceType readServiceType config sqlRuntimeInfo invalidate registerDispose args
15411546
createConstructors config (rootType, serviceType, readServiceType, args)
1542-
1543-
// Refresh no sooner than the idle expiration, and on a slow system no sooner than
1544-
// 5x the time a build takes, so refreshes cannot put the system under pressure.
1545-
let buildDuration = DateTime.UtcNow - buildStarted
1546-
let staleAfter = max idleExpiration (TimeSpan.FromTicks(buildDuration.Ticks * 5L))
1547-
rootType, DateTime.UtcNow, staleAfter
1547+
rootType
15481548

15491549
let dropDesignTimeDcProvider (key:DesignCacheKey) =
15501550
// Release the design-time data context provider (used by the Individuals feature)
@@ -1572,40 +1572,17 @@ type public SqlTypeProvider(config: TypeProviderConfig) as this =
15721572
let generation = buildRoot key
15731573
idleWatcher key |> Async.Start
15741574
generation
1575-
LastAccess = DateTime.UtcNow.Ticks
1576-
Refreshing = 0 }
1575+
LastAccess = DateTime.UtcNow.Ticks }
15771576

15781577
let mutable builtEntry = Unchecked.defaultof<DesignCacheEntry>
15791578
try
15801579
let entry = DesignTimeCache.cache.GetOrAdd(arguments, addCache)
15811580
builtEntry <- entry
15821581
System.Threading.Interlocked.Exchange(&entry.LastAccess, DateTime.UtcNow.Ticks) |> ignore
15831582

1584-
// Stale-while-revalidate: always serve the current tree; if it has gone stale,
1585-
// rebuild it once in the background and swap the entry atomically. Callers never
1586-
// wait on a refresh, and concurrent VS threads can never start a second one.
1587-
if entry.Root.IsValueCreated then
1588-
let _, builtAt, staleAfter = entry.Root.Value
1589-
if DateTime.UtcNow - builtAt >= staleAfter
1590-
&& System.Threading.Interlocked.CompareExchange(&entry.Refreshing, 1, 0) = 0 then
1591-
async {
1592-
try
1593-
try
1594-
let freshGeneration = buildRoot arguments
1595-
let freshEntry =
1596-
{ Root = Lazy<_>.CreateFromValue freshGeneration
1597-
LastAccess = DateTime.UtcNow.Ticks
1598-
Refreshing = 0 }
1599-
if DesignTimeCache.cache.TryUpdate(arguments, freshEntry, entry) then
1600-
dropDesignTimeDcProvider arguments
1601-
with
1602-
| _ -> () // keep serving the previous generation; retried on a later access
1603-
finally
1604-
System.Threading.Interlocked.Exchange(&entry.Refreshing, 0) |> ignore
1605-
} |> Async.Start
1606-
1607-
let root, _, _ = entry.Root.Value
1608-
root
1583+
// One stable generation per set of static parameters: see DesignCacheEntry for why
1584+
// the built root must never be rebuilt and swapped underneath a live compilation.
1585+
entry.Root.Value
16091586
with
16101587
| e ->
16111588
// Evict only this exact faulted generation by reference identity, never a fresh entry

0 commit comments

Comments
 (0)